Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: warning --package will be ignore if --workspace presents #15071

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 52 additions & 3 deletions src/cargo/ops/cargo_compile/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ pub enum Packages {
Default,
/// Opt in all packages.
///
/// As of the time of this writing, it only works on opting in all workspace members.
/// As of the time of this writing, it only works on opting in all workspace members.
All,
/// This equivalent to `All` but keeps the packages passed in.
AllWithPackages(Vec<String>),
/// Opt out of packages passed in.
///
/// As of the time of this writing, it only works on opting out workspace members.
Expand All @@ -36,14 +38,41 @@ impl Packages {
(false, 0, 0) => Packages::Default,
(false, 0, _) => Packages::Packages(package),
(false, _, _) => anyhow::bail!("--exclude can only be used together with --workspace"),
(true, 0, _) => Packages::All,
(true, 0, 0) => Packages::All,
(true, 0, _) => Packages::AllWithPackages(package),
(true, _, _) => Packages::OptOut(exclude),
})
}

/// Converts selected packages to [`PackageIdSpec`]s.
pub fn to_package_id_specs(&self, ws: &Workspace<'_>) -> CargoResult<Vec<PackageIdSpec>> {
let specs = match self {
Packages::AllWithPackages(packages) => {
let (mut patterns, mut ids) = opt_patterns_and_ids(packages)?;
let _: Vec<_> = ws
.members()
.filter(|pkg| {
let id = ids.iter().find(|id| id.matches(pkg.package_id())).cloned();
if let Some(id) = &id {
ids.remove(id);
}
!id.is_some() && !match_patterns(pkg, &mut patterns)
})
.map(Package::package_id)
.map(|id| id.to_spec())
.collect();
let warn = |e| ws.gctx().shell().warn(e);
let names = ids
.into_iter()
.map(|id| id.to_string())
.collect::<BTreeSet<_>>();
emit_package_not_found(ws, names, false).or_else(warn)?;
emit_pattern_not_found(ws, patterns, false).or_else(warn)?;
ws.members()
.map(Package::package_id)
.map(|id| id.to_spec())
.collect()
}
Packages::All => ws
.members()
.map(Package::package_id)
Expand Down Expand Up @@ -111,6 +140,26 @@ impl Packages {
pub fn get_packages<'ws>(&self, ws: &'ws Workspace<'_>) -> CargoResult<Vec<&'ws Package>> {
let packages: Vec<_> = match self {
Packages::Default => ws.default_members().collect(),
Packages::AllWithPackages(packages) => {
let (mut patterns, mut ids) = opt_patterns_and_ids(packages)?;
let _: Vec<_> = ws
.members()
.filter(|pkg| {
let id = ids.iter().find(|id| id.matches(pkg.package_id())).cloned();
if let Some(id) = &id {
ids.remove(id);
}
!id.is_some() && !match_patterns(pkg, &mut patterns)
})
.collect();
let names = ids
.into_iter()
.map(|id| id.to_string())
.collect::<BTreeSet<_>>();
emit_package_not_found(ws, names, false)?;
emit_pattern_not_found(ws, patterns, false)?;
ws.members().collect()
}
Packages::All => ws.members().collect(),
Packages::OptOut(opt_out) => {
let (mut patterns, mut ids) = opt_patterns_and_ids(opt_out)?;
Expand Down Expand Up @@ -161,7 +210,7 @@ impl Packages {
pub fn needs_spec_flag(&self, ws: &Workspace<'_>) -> bool {
match self {
Packages::Default => ws.default_members().count() > 1,
Packages::All => ws.members().count() > 1,
Packages::All | Packages::AllWithPackages(_) => ws.members().count() > 1,
Packages::Packages(_) => true,
Packages::OptOut(_) => true,
}
Expand Down
112 changes: 112 additions & 0 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2624,3 +2624,115 @@ foo v0.1.0 ([ROOT]/foo/sub/foo)
"#]])
.run();
}

#[cargo_test]
fn warn_nonexistence_package_togother_with_workspace() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
authors = []
edition = "2021"

[workspace]
members = ["baz"]
"#,
)
.file("src/lib.rs", "")
.file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
.file("baz/src/lib.rs", "");

let p = p.build();

p.cargo("check --package nonexistence")
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] package ID specification `nonexistence` did not match any packages

"#]])
.run();

// baz is present in the workspace and it is a real existing package.
p.cargo("check --package baz --workspace")
.with_stderr_data(
str![[r#"
[CHECKING] baz v0.1.0 ([ROOT]/foo/baz)
[CHECKING] foo v0.1.0 ([ROOT]/foo)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]]
.unordered(),
)
.run();

p.cargo("check --package nonexistence --package nonpattern* --workspace")
.with_stderr_data(str![[r#"
[WARNING] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
[WARNING] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

p.cargo("package --package nonexistence --package nonpattern* --workspace")
.with_stderr_data(str![[r#"
[WARNING] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
[WARNING] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[VERIFYING] baz v0.1.0 ([ROOT]/foo/baz)
[COMPILING] baz v0.1.0 ([ROOT]/foo/target/package/baz-0.1.0)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

"#]])
.run();

p.cargo("publish --dry-run --package nonexistence --package nonpattern* -Zpackage-workspace --workspace")
.with_stderr_data(str![[r#"
[WARNING] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
[WARNING] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
[UPDATING] crates.io index
[WARNING] crate [email protected] already exists on crates.io index
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
[VERIFYING] baz v0.1.0 ([ROOT]/foo/baz)
[COMPILING] baz v0.1.0 ([ROOT]/foo/target/package/baz-0.1.0)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[UPLOADING] baz v0.1.0 ([ROOT]/foo/baz)
[WARNING] aborting upload due to dry run
[UPLOADING] foo v0.1.0 ([ROOT]/foo)
[WARNING] aborting upload due to dry run

"#]])
.masquerade_as_nightly_cargo(&["package-workspace"])
.run();

p.cargo("tree --package nonexistence --package nonpattern* --workspace")
.with_stderr_data(str![[r#"
[WARNING] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
[WARNING] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`

"#]])
.run();
}
Loading