Skip to content

Commit 23ab2af

Browse files
committed
feat: package doesnt exist in workspace is regarded as an error
1 parent 69a3e0a commit 23ab2af

File tree

3 files changed

+64
-84
lines changed

3 files changed

+64
-84
lines changed

src/cargo/ops/cargo_compile/packages.rs

+41-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub enum Packages {
2020
/// Opt in all packages.
2121
///
2222
/// As of the time of this writing, it only works on opting in all workspace members.
23-
All,
23+
/// Keeps the packages passed in to verify that they exist in the workspace.
24+
All(Vec<String>),
2425
/// Opt out of packages passed in.
2526
///
2627
/// As of the time of this writing, it only works on opting out workspace members.
@@ -36,19 +37,21 @@ impl Packages {
3637
(false, 0, 0) => Packages::Default,
3738
(false, 0, _) => Packages::Packages(package),
3839
(false, _, _) => anyhow::bail!("--exclude can only be used together with --workspace"),
39-
(true, 0, _) => Packages::All,
40+
(true, 0, _) => Packages::All(package),
4041
(true, _, _) => Packages::OptOut(exclude),
4142
})
4243
}
4344

4445
/// Converts selected packages to [`PackageIdSpec`]s.
4546
pub fn to_package_id_specs(&self, ws: &Workspace<'_>) -> CargoResult<Vec<PackageIdSpec>> {
4647
let specs = match self {
47-
Packages::All => ws
48-
.members()
49-
.map(Package::package_id)
50-
.map(|id| id.to_spec())
51-
.collect(),
48+
Packages::All(packages) => {
49+
emit_packages_not_found_within_workspace(ws, packages)?;
50+
ws.members()
51+
.map(Package::package_id)
52+
.map(|id| id.to_spec())
53+
.collect()
54+
}
5255
Packages::OptOut(opt_out) => {
5356
let (mut patterns, mut ids) = opt_patterns_and_ids(opt_out)?;
5457
let specs = ws
@@ -111,7 +114,10 @@ impl Packages {
111114
pub fn get_packages<'ws>(&self, ws: &'ws Workspace<'_>) -> CargoResult<Vec<&'ws Package>> {
112115
let packages: Vec<_> = match self {
113116
Packages::Default => ws.default_members().collect(),
114-
Packages::All => ws.members().collect(),
117+
Packages::All(packages) => {
118+
emit_packages_not_found_within_workspace(ws, packages)?;
119+
ws.members().collect()
120+
}
115121
Packages::OptOut(opt_out) => {
116122
let (mut patterns, mut ids) = opt_patterns_and_ids(opt_out)?;
117123
let packages = ws
@@ -161,7 +167,7 @@ impl Packages {
161167
pub fn needs_spec_flag(&self, ws: &Workspace<'_>) -> bool {
162168
match self {
163169
Packages::Default => ws.default_members().count() > 1,
164-
Packages::All => ws.members().count() > 1,
170+
Packages::All(_) => ws.members().count() > 1,
165171
Packages::Packages(_) => true,
166172
Packages::OptOut(_) => true,
167173
}
@@ -207,6 +213,32 @@ fn emit_pattern_not_found(
207213
Ok(())
208214
}
209215

216+
fn emit_packages_not_found_within_workspace(
217+
ws: &Workspace<'_>,
218+
packages: &[String],
219+
) -> CargoResult<()> {
220+
let (mut patterns, mut ids) = opt_patterns_and_ids(packages)?;
221+
let _: Vec<_> = ws
222+
.members()
223+
.filter(|pkg| {
224+
let id = ids.iter().find(|id| id.matches(pkg.package_id())).cloned();
225+
if let Some(id) = &id {
226+
ids.remove(id);
227+
}
228+
!id.is_some() && !match_patterns(pkg, &mut patterns)
229+
})
230+
.map(Package::package_id)
231+
.map(|id| id.to_spec())
232+
.collect();
233+
let names = ids
234+
.into_iter()
235+
.map(|id| id.to_string())
236+
.collect::<BTreeSet<_>>();
237+
emit_package_not_found(ws, names, false)?;
238+
emit_pattern_not_found(ws, patterns, false)?;
239+
Ok(())
240+
}
241+
210242
/// Given a list opt-in or opt-out package selection strings, generates two
211243
/// collections that represent glob patterns and package id specs respectively.
212244
fn opt_patterns_and_ids(

src/cargo/ops/cargo_output_metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn build_resolve_graph(
136136
CompileKind::from_requested_targets(ws.gctx(), &metadata_opts.filter_platforms)?;
137137
let mut target_data = RustcTargetData::new(ws, &requested_kinds)?;
138138
// Resolve entire workspace.
139-
let specs = Packages::All.to_package_id_specs(ws)?;
139+
let specs = Packages::All(Vec::new()).to_package_id_specs(ws)?;
140140
let force_all = if metadata_opts.filter_platforms.is_empty() {
141141
crate::core::resolver::features::ForceAllTargets::Yes
142142
} else {

tests/testsuite/workspaces.rs

+22-74
Original file line numberDiff line numberDiff line change
@@ -2648,123 +2648,71 @@ fn nonexistence_package_togother_with_workspace() {
26482648
let p = p.build();
26492649

26502650
p.cargo("check --package nonexistence --workspace")
2651+
.with_status(101)
26512652
.with_stderr_data(
26522653
str![[r#"
2653-
[CHECKING] foo v0.1.0 ([ROOT]/foo)
2654-
[CHECKING] baz v0.1.0 ([ROOT]/foo/baz)
2655-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2654+
[ERROR] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
26562655
26572656
"#]]
26582657
.unordered(),
26592658
)
26602659
.run();
26612660
// With pattern *
26622661
p.cargo("check --package nonpattern* --workspace")
2662+
.with_status(101)
26632663
.with_stderr_data(str![[r#"
2664-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2664+
[ERROR] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
26652665
26662666
"#]])
26672667
.run();
26682668

26692669
p.cargo("package --package nonexistence --workspace")
2670+
.with_status(101)
26702671
.with_stderr_data(str![[r#"
2671-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2672-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2673-
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
2674-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2675-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2676-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2677-
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
2678-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2679-
[VERIFYING] baz v0.1.0 ([ROOT]/foo/baz)
2680-
[COMPILING] baz v0.1.0 ([ROOT]/foo/target/package/baz-0.1.0)
2681-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2682-
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
2683-
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
2684-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2672+
[ERROR] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
26852673
26862674
"#]])
26872675
.run();
26882676
// With pattern *
26892677
p.cargo("package --package nonpattern* --workspace")
2678+
.with_status(101)
26902679
.with_stderr_data(str![[r#"
2691-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2692-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2693-
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
2694-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2695-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2696-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2697-
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
2698-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2699-
[VERIFYING] baz v0.1.0 ([ROOT]/foo/baz)
2700-
[COMPILING] baz v0.1.0 ([ROOT]/foo/target/package/baz-0.1.0)
2701-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2702-
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
2703-
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
2704-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2680+
[ERROR] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
27052681
27062682
"#]])
27072683
.run();
27082684

27092685
p.cargo("publish --dry-run --package nonexistence -Zpackage-workspace --workspace")
2686+
.with_status(101)
27102687
.with_stderr_data(str![[r#"
2711-
[UPDATING] crates.io index
2712-
[WARNING] crate baz@0.1.0 already exists on crates.io index
2713-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2714-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2715-
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
2716-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2717-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2718-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2719-
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
2720-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2721-
[VERIFYING] baz v0.1.0 ([ROOT]/foo/baz)
2722-
[COMPILING] baz v0.1.0 ([ROOT]/foo/target/package/baz-0.1.0)
2723-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2724-
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
2725-
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
2726-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2727-
[UPLOADING] baz v0.1.0 ([ROOT]/foo/baz)
2728-
[WARNING] aborting upload due to dry run
2729-
[UPLOADING] foo v0.1.0 ([ROOT]/foo)
2730-
[WARNING] aborting upload due to dry run
2688+
[ERROR] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
27312689
27322690
"#]])
27332691
.masquerade_as_nightly_cargo(&["package-workspace"])
27342692
.run();
27352693
// With pattern *
27362694
p.cargo("publish --dry-run --package nonpattern* -Zpackage-workspace --workspace")
2695+
.with_status(101)
27372696
.with_stderr_data(str![[r#"
2738-
[UPDATING] crates.io index
2739-
[WARNING] crate baz@0.1.0 already exists on crates.io index
2740-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2741-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2742-
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
2743-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2744-
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
2745-
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
2746-
[PACKAGING] foo v0.1.0 ([ROOT]/foo)
2747-
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
2748-
[VERIFYING] baz v0.1.0 ([ROOT]/foo/baz)
2749-
[COMPILING] baz v0.1.0 ([ROOT]/foo/target/package/baz-0.1.0)
2750-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2751-
[VERIFYING] foo v0.1.0 ([ROOT]/foo)
2752-
[COMPILING] foo v0.1.0 ([ROOT]/foo/target/package/foo-0.1.0)
2753-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
2754-
[UPLOADING] baz v0.1.0 ([ROOT]/foo/baz)
2755-
[WARNING] aborting upload due to dry run
2756-
[UPLOADING] foo v0.1.0 ([ROOT]/foo)
2757-
[WARNING] aborting upload due to dry run
2697+
[ERROR] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
27582698
27592699
"#]])
27602700
.masquerade_as_nightly_cargo(&["package-workspace"])
27612701
.run();
27622702

27632703
p.cargo("tree --package nonexistence --workspace")
2764-
.with_stderr_data(str![])
2704+
.with_status(101)
2705+
.with_stderr_data(str![[r#"
2706+
[ERROR] package(s) `nonexistence` not found in workspace `[ROOT]/foo`
2707+
2708+
"#]])
27652709
.run();
27662710
// With pattern *
27672711
p.cargo("tree --package nonpattern* --workspace")
2768-
.with_stderr_data(str![])
2712+
.with_status(101)
2713+
.with_stderr_data(str![[r#"
2714+
[ERROR] package pattern(s) `nonpattern*` not found in workspace `[ROOT]/foo`
2715+
2716+
"#]])
27692717
.run();
27702718
}

0 commit comments

Comments
 (0)