Skip to content
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
18 changes: 15 additions & 3 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ use crate::util::errors::{CargoResult, ManifestError};
use crate::util::interning::InternedString;
use crate::util::toml::{InheritableFields, read_manifest};
use crate::util::{
Filesystem, GlobalContext, IntoUrl, context::CargoResolverConfig, context::ConfigRelativePath,
context::IncompatibleRustVersions,
Filesystem, GlobalContext, IntoUrl, closest_msg, context::CargoResolverConfig,
context::ConfigRelativePath, context::IncompatibleRustVersions,
};

use cargo_util::paths;
Expand Down Expand Up @@ -1890,7 +1890,19 @@ impl<'gctx> Workspace<'gctx> {
&& !cli_features.all_features
&& cli_features.uses_default_features)
{
bail!("cannot specify features for packages outside of workspace");
let hint = specs
.iter()
.map(|spec| {
closest_msg(
spec.name(),
self.members(),
|m| m.name().as_str(),
"workspace member",
)
})
.find(|msg| !msg.is_empty())
.unwrap_or_default();
bail!("cannot specify features for packages outside of workspace{hint}");
}
// Add all members from the workspace so we can ensure `-p nonmember`
// is in the resolve graph.
Expand Down
42 changes: 42 additions & 0 deletions tests/testsuite/package_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ fn non_member() {
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();

Expand All @@ -755,6 +757,8 @@ fn non_member() {
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();

Expand All @@ -763,6 +767,8 @@ fn non_member() {
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();

Expand All @@ -779,6 +785,34 @@ fn non_member() {
.run();
}

#[cargo_test]
fn non_member_typo() {
// -p with a mistyped package name shows a helpful error hint
// about similarly named workspace members.
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["bar"]
resolver = "2"
"#,
)
.file("bar/Cargo.toml", &basic_manifest("bar", "1.0.0"))
.file("bar/src/lib.rs", "")
.build();

p.cargo("build -p barr --all-features")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `bar`

"#]])
.run();
}

#[cargo_test]
fn resolver1_member_features() {
// --features member-name/feature-name with resolver="1"
Expand Down Expand Up @@ -962,6 +996,8 @@ fn non_member_feature() {
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();

Expand All @@ -983,20 +1019,26 @@ fn non_member_feature() {
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();
p.cargo("check -p bar --features bar/jazz")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();
p.cargo("check -p bar --features foo/bar")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] cannot specify features for packages outside of workspace

[HELP] a workspace member with a similar name exists: `foo`

"#]])
.run();
}