Skip to content

Commit

Permalink
feat: Warning packages not found if --workspace present
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Jan 17, 2025
1 parent ec62374 commit 03b3ca7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
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
12 changes: 11 additions & 1 deletion tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2670,13 +2670,17 @@ fn warn_nonexistence_package_togother_with_workspace() {

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)
Expand All @@ -2697,6 +2701,8 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for

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.
Expand All @@ -2723,6 +2729,10 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
.run();

p.cargo("tree --package nonexistence --package nonpattern* --workspace")
.with_stderr_data(str![])
.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();
}

0 comments on commit 03b3ca7

Please sign in to comment.