Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve error and feedback when target does not exist #1961

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/pixi_consts/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub const PREFIX_FILE_NAME: &str = "pixi_env_prefix";
pub const ENVIRONMENTS_DIR: &str = "envs";
pub const SOLVE_GROUP_ENVIRONMENTS_DIR: &str = "solve-group-envs";
pub const PYPI_DEPENDENCIES: &str = "pypi-dependencies";
pub const DEPENDENCIES: &str = "dependencies";
pub const TASK_CACHE_DIR: &str = "task-cache-v0";
pub const PIXI_UV_INSTALLER: &str = "uv-pixi";
pub const CONDA_PACKAGE_CACHE_DIR: &str = rattler_cache::PACKAGE_CACHE_DIR;
Expand Down
53 changes: 39 additions & 14 deletions crates/pixi_manifest/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,13 @@ impl Manifest {
// Remove the dependency from the manifest
match self
.target_mut(platform, feature_name)
.ok_or_else(|| Self::handle_target_missing(platform.as_ref(), feature_name))?
.ok_or_else(|| {
Self::handle_target_missing(
platform.as_ref(),
feature_name,
consts::DEPENDENCIES,
)
})?
.remove_dependency(dep, spec_type)
{
Ok(_) => (),
Expand All @@ -447,7 +453,13 @@ impl Manifest {
// Remove the dependency from the manifest
match self
.target_mut(platform, feature_name)
.ok_or_else(|| Self::handle_target_missing(platform.as_ref(), feature_name))?
.ok_or_else(|| {
Self::handle_target_missing(
platform.as_ref(),
feature_name,
consts::PYPI_DEPENDENCIES,
)
})?
.remove_pypi_dependency(dep)
{
Ok(_) => (),
Expand All @@ -463,22 +475,35 @@ impl Manifest {
Ok(())
}

/// Handles the target missing error cases
fn handle_target_missing(
platform: Option<&Platform>,
feature_name: &FeatureName,
section: &str,
tdejager marked this conversation as resolved.
Show resolved Hide resolved
) -> miette::Report {
match platform {
None => {
miette!("No target for feature `{}`", feature_name)
}
Some(platform) => {
miette!(
"No target for feature `{}` on platform `{}`",
feature_name,
platform
)
}
}
let platform = platform.copied().unwrap_or_else(Platform::current);

let help = if feature_name.is_default() {
format!(
r#"Expected target for `{name}`, e.g.: `[target.{platform}.{section}]`"#,
name = feature_name,
platform = platform,
section = section
)
} else {
format!(
r#"Expected target for `{name}`, e.g.: `[feature.{name}.target.{platform}.{section}]`"#,
name = feature_name,
platform = platform,
section = section
)
};
miette!(
help = &help,
"No target for feature `{name}` found on platform `{platform}`",
name = feature_name,
platform = platform
)
}

/// Returns true if any of the features has pypi dependencies defined.
Expand Down
35 changes: 24 additions & 11 deletions src/cli/remove.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Parser;
use miette::Context;

use crate::environment::update_prefix;
use crate::DependencyType;
Expand Down Expand Up @@ -41,21 +42,33 @@ pub async fn execute(args: Args) -> miette::Result<()> {
match dependency_type {
DependencyType::PypiDependency => {
for name in dependency_config.pypi_deps(&project)?.keys() {
project.manifest.remove_pypi_dependency(
name,
&dependency_config.platform,
&dependency_config.feature_name(),
)?;
project
.manifest
.remove_pypi_dependency(
name,
&dependency_config.platform,
&dependency_config.feature_name(),
)
.wrap_err(format!(
"failed to remove PyPI dependency: '{}'",
name.as_source()
))?;
}
}
DependencyType::CondaDependency(spec_type) => {
for name in dependency_config.specs()?.keys() {
project.manifest.remove_dependency(
name,
spec_type,
&dependency_config.platform,
&dependency_config.feature_name(),
)?;
project
.manifest
.remove_dependency(
name,
spec_type,
&dependency_config.platform,
&dependency_config.feature_name(),
)
.wrap_err(format!(
"failed to remove dependency: '{}'",
name.as_source()
))?;
}
}
};
Expand Down
Loading