Skip to content

Commit

Permalink
make unknown features on cargo add more discoverable
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilgardis committed Sep 16, 2022
1 parent 362ce33 commit 59f5363
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
49 changes: 40 additions & 9 deletions src/cargo/ops/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
table_option.map_or(true, |table| is_sorted(table.iter().map(|(name, _)| name)))
});
for dep in deps {
print_msg(&mut options.config.shell(), &dep, &dep_table)?;
print_action_msg(&mut options.config.shell(), &dep, &dep_table)?;
if let Some(Source::Path(src)) = dep.source() {
if src.path == manifest.path.parent().unwrap_or_else(|| Path::new("")) {
anyhow::bail!(
Expand All @@ -124,7 +124,11 @@ pub fn add(workspace: &Workspace<'_>, options: &AddOptions<'_>) -> CargoResult<(
inherited_features.iter().map(|s| s.as_str()).collect();
unknown_features.extend(inherited_features.difference(&available_features).copied());
}

unknown_features.sort();

print_dep_table_msg(&mut options.config.shell(), &dep, &unknown_features)?;

if !unknown_features.is_empty() {
anyhow::bail!("unrecognized features: {unknown_features:?}");
}
Expand Down Expand Up @@ -697,7 +701,7 @@ fn populate_available_features(
Ok(dependency)
}

fn print_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> CargoResult<()> {
fn print_action_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> CargoResult<()> {
use std::fmt::Write;

if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) {
Expand Down Expand Up @@ -736,8 +740,17 @@ fn print_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> Cargo
};
write!(message, " {section}")?;
write!(message, ".")?;
shell.status("Adding", message)?;
shell.status("Adding", message)
}

fn print_dep_table_msg(
shell: &mut Shell,
dep: &DependencyUI,
unknown_features: &[&str],
) -> CargoResult<()> {
if matches!(shell.verbosity(), crate::core::shell::Verbosity::Quiet) {
return Ok(());
}
let mut activated: IndexSet<_> = dep.features.iter().flatten().map(|s| s.as_str()).collect();
if dep.default_features().unwrap_or(true) {
activated.insert("default");
Expand Down Expand Up @@ -792,15 +805,33 @@ fn print_msg(shell: &mut Shell, dep: &DependencyUI, section: &[String]) -> Cargo
format_args!("{}Features{}:\n", prefix, suffix),
&ColorSpec::new(),
)?;
let mut print_row =
|symbol: char, color: termcolor::Color, feat: &str| -> CargoResult<()> {
let pre: &str;
let (error_symbol, underline, err_color) = if unknown_features.contains(&feat) {
pre = &prefix[2..];
("! ", true, Some(Red))
} else {
pre = &prefix;
("", false, None)
};
shell.write_stderr(&pre, &ColorSpec::new())?;
shell.write_stderr(error_symbol, &ColorSpec::new().set_fg(err_color))?;
shell.write_stderr(
format_args!("{}", symbol),
&ColorSpec::new().set_bold(true).set_fg(Some(color)),
)?;
shell.write_stderr(" ", &ColorSpec::new())?;
shell.write_stderr(
format_args!("{}\n", feat),
&ColorSpec::new().set_underline(underline).set_fg(err_color),
)
};
for feat in activated {
shell.write_stderr(&prefix, &ColorSpec::new())?;
shell.write_stderr('+', &ColorSpec::new().set_bold(true).set_fg(Some(Green)))?;
shell.write_stderr(format_args!(" {}\n", feat), &ColorSpec::new())?;
print_row('+', Green, feat)?;
}
for feat in deactivated {
shell.write_stderr(&prefix, &ColorSpec::new())?;
shell.write_stderr('-', &ColorSpec::new().set_bold(true).set_fg(Some(Red)))?;
shell.write_stderr(format_args!(" {}\n", feat), &ColorSpec::new())?;
print_row('-', Red, feat)?;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testsuite/cargo_add/features_unknown/stderr.log
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Updating `dummy-registry` index
Adding your-face v99999.0.0 to dependencies.
Features:
+ noze
! + noze
- ears
- eyes
- mouth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
+ default-base
+ default-merge-base
+ default-test-base
+ not_recognized
! + not_recognized
+ test
+ test-base
- merge
Expand Down

0 comments on commit 59f5363

Please sign in to comment.