Skip to content

Commit

Permalink
feat: Warn --package will be ignore if it comes up with --workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
linyihai committed Jan 16, 2025
1 parent 687108d commit 28d534b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
print_available_packages(&ws)?;
}

let packages = args.packages_from_flags()?;
let packages = args.packages_from_flags(gctx, false)?;
let packages = packages.get_packages(&ws)?;
let spec = match packages.len() {
0 => {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
)
.into());
}
let specs = args.packages_from_flags()?;
let specs = args.packages_from_flags(gctx, true)?;

ops::package(
&ws,
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
reg_or_index,
verify: !args.flag("no-verify"),
allow_dirty: args.flag("allow-dirty"),
to_publish: args.packages_from_flags()?,
to_publish: args.packages_from_flags(gctx, true)?,
targets: args.targets()?,
jobs: args.jobs()?,
keep_going: args.keep_going(),
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
print_available_packages(&workspace)?;
}

let packages = args.packages_from_flags()?;
let packages = args.packages_from_flags(gctx, false)?;
let packages = packages.get_packages(&workspace)?;
let spec = match packages.len() {
0 => {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
.transpose()?
.unwrap_or(DisplayDepth::MaxDisplayDepth(u32::MAX));

let packages = args.packages_from_flags()?;
let packages = args.packages_from_flags(gctx, true)?;
let mut invert = args
.get_many::<String>("invert")
.map_or_else(|| Vec::new(), |is| is.map(|s| s.to_string()).collect());
Expand Down
18 changes: 14 additions & 4 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,23 @@ Run `{cmd}` to see possible targets."
Ok(InternedString::new(name))
}

fn packages_from_flags(&self) -> CargoResult<Packages> {
Packages::from_flags(
fn packages_from_flags(
&self,
gctx: &GlobalContext,
ignore_package: bool,
) -> CargoResult<Packages> {
let spec = Packages::from_flags(
// TODO Integrate into 'workspace'
self.flag("workspace") || self.flag("all"),
self._values_of("exclude"),
self._values_of("package"),
)
)?;
if ignore_package && matches!(spec, Packages::All) && !self._values_of("package").is_empty()
{
gctx.shell()
.warn("ignoring `--package` as the `--workspace` or `--all` flag is set")?;
}
Ok(spec)
}

fn compile_options(
Expand All @@ -688,7 +698,7 @@ Run `{cmd}` to see possible targets."
workspace: Option<&Workspace<'_>>,
profile_checking: ProfileChecking,
) -> CargoResult<CompileOptions> {
let spec = self.packages_from_flags()?;
let spec = self.packages_from_flags(gctx, true)?;
let mut message_format = None;
let default_json = MessageFormat::Json {
short: false,
Expand Down
11 changes: 9 additions & 2 deletions tests/testsuite/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2658,22 +2658,25 @@ fn warn_package_togother_with_workspace() {
// 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] foo v0.1.0 ([ROOT]/foo)
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
[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
"#]])
.run();

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

p.cargo("package --package nonexistence --workspace")
.with_stderr_data(str![[r#"
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
[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 @@ -2694,6 +2697,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for

p.cargo("publish --dry-run --package nonexistence -Zpackage-workspace --workspace")
.with_stderr_data(str![[r#"
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
[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 @@ -2720,6 +2724,9 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
.run();

p.cargo("tree --package nonexistence --workspace")
.with_stderr_data(str![])
.with_stderr_data(str![[r#"
[WARNING] ignoring `--package` as the `--workspace` or `--all` flag is set
"#]])
.run();
}

0 comments on commit 28d534b

Please sign in to comment.