Skip to content

Commit 7ac5d58

Browse files
committed
Auto merge of #13735 - linyihai:package-no-match, r=epage
`cargo package -p no-exist` emitt error when the -p `package` not found ### What does this PR try to resolve? Fixes #13719 If `-p` is used, and the spec doesn't match any member, we emit an error like `cargo publish -p` does. ### How should we test and review this PR? The first commit add a test to show the issue, the next commit add the check logic to fix it. ### Additional information
2 parents 6208c52 + decbadb commit 7ac5d58

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor}
1010
use crate::core::manifest::Target;
1111
use crate::core::resolver::CliFeatures;
1212
use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
13-
use crate::core::{Feature, Shell, Verbosity, Workspace};
13+
use crate::core::{Feature, PackageIdSpecQuery, Shell, Verbosity, Workspace};
1414
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
1515
use crate::sources::PathSource;
1616
use crate::util::cache_lock::CacheLockMode;
@@ -176,10 +176,15 @@ pub fn package_one(
176176
}
177177

178178
pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option<Vec<FileLock>>> {
179-
let pkgs = ws.members_with_features(
180-
&opts.to_package.to_package_id_specs(ws)?,
181-
&opts.cli_features,
182-
)?;
179+
let specs = &opts.to_package.to_package_id_specs(ws)?;
180+
// If -p is used, we should check spec is matched with the members (See #13719)
181+
if let ops::Packages::Packages(_) = opts.to_package {
182+
for spec in specs.iter() {
183+
let member_ids = ws.members().map(|p| p.package_id());
184+
spec.query(member_ids)?;
185+
}
186+
}
187+
let pkgs = ws.members_with_features(specs, &opts.cli_features)?;
183188

184189
let mut dsts = Vec::with_capacity(pkgs.len());
185190

tests/testsuite/package.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,6 +2618,55 @@ src/main.rs
26182618
p.cargo("package --allow-dirty").run();
26192619
}
26202620

2621+
#[cargo_test]
2622+
fn package_in_workspace_not_found() {
2623+
let p = project()
2624+
.file(
2625+
"Cargo.toml",
2626+
r#"
2627+
[workspace]
2628+
members = ["bar", "baz"]
2629+
"#,
2630+
)
2631+
.file("src/main.rs", "fn main() {}")
2632+
.file(
2633+
"bar/Cargo.toml",
2634+
r#"
2635+
[package]
2636+
name = "bar"
2637+
version = "0.0.1"
2638+
edition = "2015"
2639+
authors = []
2640+
license = "MIT"
2641+
description = "bar"
2642+
"#,
2643+
)
2644+
.file("bar/src/main.rs", "fn main() {}")
2645+
.file(
2646+
"baz/Cargo.toml",
2647+
r#"
2648+
[package]
2649+
name = "baz"
2650+
version = "0.0.1"
2651+
edition = "2015"
2652+
authors = []
2653+
license = "MIT"
2654+
description = "baz"
2655+
"#,
2656+
)
2657+
.file("baz/src/main.rs", "fn main() {}")
2658+
.build();
2659+
2660+
p.cargo("package -p doesnt-exist")
2661+
.with_status(101)
2662+
.with_stderr_contains(
2663+
"\
2664+
[ERROR] package ID specification `doesnt-exist` did not match any packages
2665+
",
2666+
)
2667+
.run();
2668+
}
2669+
26212670
#[cargo_test]
26222671
fn in_workspace() {
26232672
let p = project()

0 commit comments

Comments
 (0)