Skip to content

Commit ee944b8

Browse files
committed
fix(core): add typo hint for -p with feature flags
1 parent b591eec commit ee944b8

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

src/cargo/core/workspace.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,24 @@ impl<'gctx> Workspace<'gctx> {
18901890
&& !cli_features.all_features
18911891
&& cli_features.uses_default_features)
18921892
{
1893-
bail!("cannot specify features for packages outside of workspace");
1893+
let hint = specs
1894+
.iter()
1895+
.find_map(|spec| {
1896+
self.members()
1897+
.filter_map(|member| {
1898+
edit_distance(spec.name(), member.name().as_str(), 2)
1899+
.map(|distance| (distance, member))
1900+
})
1901+
.min_by_key(|(distance, _)| *distance)
1902+
.map(|(_, member)| {
1903+
format!(
1904+
"\n\nhelp: a workspace member with a similar name exists: `{}`",
1905+
member.name()
1906+
)
1907+
})
1908+
})
1909+
.unwrap_or_default();
1910+
bail!("cannot specify features for packages outside of workspace{hint}");
18941911
}
18951912
// Add all members from the workspace so we can ensure `-p nonmember`
18961913
// is in the resolve graph.

tests/testsuite/package_features.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,8 @@ fn non_member() {
781781

782782
#[cargo_test]
783783
fn non_member_typo() {
784-
// -p with a mistyped package name currently shows a misleading error
785-
// without any hint about similar workspace member names.
784+
// -p with a mistyped package name shows a helpful error hint
785+
// about similarly named workspace members.
786786
let p = project()
787787
.file(
788788
"Cargo.toml",
@@ -801,6 +801,8 @@ fn non_member_typo() {
801801
.with_stderr_data(str![[r#"
802802
[ERROR] cannot specify features for packages outside of workspace
803803
804+
[HELP] a workspace member with a similar name exists: `bar`
805+
804806
"#]])
805807
.run();
806808
}

0 commit comments

Comments
 (0)