Skip to content

Commit 28d534b

Browse files
committed
feat: Warn --package will be ignore if it comes up with --workspace
1 parent 687108d commit 28d534b

File tree

7 files changed

+28
-11
lines changed

7 files changed

+28
-11
lines changed

src/bin/cargo/commands/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
185185
print_available_packages(&ws)?;
186186
}
187187

188-
let packages = args.packages_from_flags()?;
188+
let packages = args.packages_from_flags(gctx, false)?;
189189
let packages = packages.get_packages(&ws)?;
190190
let spec = match packages.len() {
191191
0 => {

src/bin/cargo/commands/package.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
6969
)
7070
.into());
7171
}
72-
let specs = args.packages_from_flags()?;
72+
let specs = args.packages_from_flags(gctx, true)?;
7373

7474
ops::package(
7575
&ws,

src/bin/cargo/commands/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
7272
reg_or_index,
7373
verify: !args.flag("no-verify"),
7474
allow_dirty: args.flag("allow-dirty"),
75-
to_publish: args.packages_from_flags()?,
75+
to_publish: args.packages_from_flags(gctx, true)?,
7676
targets: args.targets()?,
7777
jobs: args.jobs()?,
7878
keep_going: args.keep_going(),

src/bin/cargo/commands/remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
6666
print_available_packages(&workspace)?;
6767
}
6868

69-
let packages = args.packages_from_flags()?;
69+
let packages = args.packages_from_flags(gctx, false)?;
7070
let packages = packages.get_packages(&workspace)?;
7171
let spec = match packages.len() {
7272
0 => {

src/bin/cargo/commands/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
168168
.transpose()?
169169
.unwrap_or(DisplayDepth::MaxDisplayDepth(u32::MAX));
170170

171-
let packages = args.packages_from_flags()?;
171+
let packages = args.packages_from_flags(gctx, true)?;
172172
let mut invert = args
173173
.get_many::<String>("invert")
174174
.map_or_else(|| Vec::new(), |is| is.map(|s| s.to_string()).collect());

src/cargo/util/command_prelude.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,23 @@ Run `{cmd}` to see possible targets."
672672
Ok(InternedString::new(name))
673673
}
674674

675-
fn packages_from_flags(&self) -> CargoResult<Packages> {
676-
Packages::from_flags(
675+
fn packages_from_flags(
676+
&self,
677+
gctx: &GlobalContext,
678+
ignore_package: bool,
679+
) -> CargoResult<Packages> {
680+
let spec = Packages::from_flags(
677681
// TODO Integrate into 'workspace'
678682
self.flag("workspace") || self.flag("all"),
679683
self._values_of("exclude"),
680684
self._values_of("package"),
681-
)
685+
)?;
686+
if ignore_package && matches!(spec, Packages::All) && !self._values_of("package").is_empty()
687+
{
688+
gctx.shell()
689+
.warn("ignoring `--package` as the `--workspace` or `--all` flag is set")?;
690+
}
691+
Ok(spec)
682692
}
683693

684694
fn compile_options(
@@ -688,7 +698,7 @@ Run `{cmd}` to see possible targets."
688698
workspace: Option<&Workspace<'_>>,
689699
profile_checking: ProfileChecking,
690700
) -> CargoResult<CompileOptions> {
691-
let spec = self.packages_from_flags()?;
701+
let spec = self.packages_from_flags(gctx, true)?;
692702
let mut message_format = None;
693703
let default_json = MessageFormat::Json {
694704
short: false,

tests/testsuite/workspaces.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2658,22 +2658,25 @@ fn warn_package_togother_with_workspace() {
26582658
// baz is present in the workspace and it is a real existing package.
26592659
p.cargo("check --package baz --workspace")
26602660
.with_stderr_data(str![[r#"
2661-
[CHECKING] foo v0.1.0 ([ROOT]/foo)
2661+
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
26622662
[CHECKING] baz v0.1.0 ([ROOT]/foo/baz)
2663+
[CHECKING] foo v0.1.0 ([ROOT]/foo)
26632664
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
26642665
26652666
"#]])
26662667
.run();
26672668

26682669
p.cargo("check --package nonexistence --workspace")
26692670
.with_stderr_data(str![[r#"
2671+
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
26702672
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
26712673
26722674
"#]])
26732675
.run();
26742676

26752677
p.cargo("package --package nonexistence --workspace")
26762678
.with_stderr_data(str![[r#"
2679+
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
26772680
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
26782681
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
26792682
[PACKAGING] baz v0.1.0 ([ROOT]/foo/baz)
@@ -2694,6 +2697,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
26942697

26952698
p.cargo("publish --dry-run --package nonexistence -Zpackage-workspace --workspace")
26962699
.with_stderr_data(str![[r#"
2700+
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
26972701
[UPDATING] crates.io index
26982702
[WARNING] crate [email protected] already exists on crates.io index
26992703
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
@@ -2720,6 +2724,9 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
27202724
.run();
27212725

27222726
p.cargo("tree --package nonexistence --workspace")
2723-
.with_stderr_data(str![])
2727+
.with_stderr_data(str![[r#"
2728+
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
2729+
2730+
"#]])
27242731
.run();
27252732
}

0 commit comments

Comments
 (0)