Skip to content

Commit b773de2

Browse files
committed
fix: cargo package -p includes all packages if no match is found
1 parent a5cfa23 commit b773de2

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
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: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,28 +2659,13 @@ fn package_in_workspace_no_found() {
26592659

26602660
p.cargo("package -p doesnt-exist")
26612661
.env("CARGO_LOG", "cargo::ops::cargo_compile=trace")
2662-
.with_stderr(
2662+
.with_status(101)
2663+
.with_stderr_contains(
26632664
"\
2664-
[WARNING] manifest has no documentation, [..]
2665-
See [..]
2666-
[PACKAGING] bar v0.0.1 ([CWD]/bar)
2667-
[VERIFYING] bar v0.0.1 ([CWD]/bar)
2668-
[COMPILING] bar v0.0.1 ([CWD][..])
2669-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
2670-
[PACKAGED] [..] files, [..] ([..] compressed)
2671-
[WARNING] manifest has no documentation, [..]
2672-
See [..]
2673-
[PACKAGING] baz v0.0.1 ([CWD]/baz)
2674-
[VERIFYING] baz v0.0.1 ([CWD]/baz)
2675-
[COMPILING] baz v0.0.1 ([CWD][..])
2676-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [..]
2677-
[PACKAGED] [..] files, [..] ([..] compressed)
2665+
[ERROR] package ID specification `doesnt-exist` did not match any packages
26782666
",
26792667
)
26802668
.run();
2681-
2682-
assert!(p.root().join("target/package/bar-0.0.1.crate").is_file());
2683-
assert!(p.root().join("target/package/baz-0.0.1.crate").is_file());
26842669
}
26852670

26862671
#[cargo_test]

0 commit comments

Comments
 (0)