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
This commit is contained in:
bors 2024-04-12 16:27:02 +00:00
commit 7ac5d58f36
2 changed files with 59 additions and 5 deletions

View File

@ -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;
@ -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());

View File

@ -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()