Skip to content

Commit

Permalink
Auto merge of #13735 - linyihai:package-no-match, r=epage
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
bors committed Apr 12, 2024
2 parents 6208c52 + decbadb commit 7ac5d58
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor}
use crate::core::manifest::Target;
use crate::core::resolver::CliFeatures;
use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
use crate::core::{Feature, Shell, Verbosity, Workspace};
use crate::core::{Feature, PackageIdSpecQuery, Shell, Verbosity, Workspace};
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
use crate::sources::PathSource;
use crate::util::cache_lock::CacheLockMode;
Expand Down Expand Up @@ -176,10 +176,15 @@ pub fn package_one(
}

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

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

Expand Down
49 changes: 49 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,55 @@ src/main.rs
p.cargo("package --allow-dirty").run();
}

#[cargo_test]
fn package_in_workspace_not_found() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["bar", "baz"]
"#,
)
.file("src/main.rs", "fn main() {}")
.file(
"bar/Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
edition = "2015"
authors = []
license = "MIT"
description = "bar"
"#,
)
.file("bar/src/main.rs", "fn main() {}")
.file(
"baz/Cargo.toml",
r#"
[package]
name = "baz"
version = "0.0.1"
edition = "2015"
authors = []
license = "MIT"
description = "baz"
"#,
)
.file("baz/src/main.rs", "fn main() {}")
.build();

p.cargo("package -p doesnt-exist")
.with_status(101)
.with_stderr_contains(
"\
[ERROR] package ID specification `doesnt-exist` did not match any packages
",
)
.run();
}

#[cargo_test]
fn in_workspace() {
let p = project()
Expand Down

0 comments on commit 7ac5d58

Please sign in to comment.