From 3ffcdee1ce917aa80371fff053f8efcb123c2c5f Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Wed, 9 Oct 2024 14:01:25 +0700 Subject: [PATCH 1/3] Add failing test --- tests/testsuite/publish.rs | 105 +++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 82b689e034e..4b1dbc45bdd 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -3364,6 +3364,111 @@ You may press ctrl-c to skip waiting; the crate should be available shortly. .run(); } +#[cargo_test] +fn package_selection() { + let registry = registry::RegistryBuilder::new().http_api().build(); + let p = project() + .file( + "Cargo.toml", + r#" + [workspace] + members = ["a", "b"] + "#, + ) + .file("a/Cargo.toml", &basic_manifest("a", "0.1.0")) + .file("a/src/lib.rs", "#[test] fn a() {}") + .file("b/Cargo.toml", &basic_manifest("b", "0.1.0")) + .file("b/src/lib.rs", "#[test] fn b() {}") + .build(); + + p.cargo("publish --no-verify --dry-run -Zpackage-workspace --workspace") + .replace_crates_io(registry.index_url()) + .masquerade_as_nightly_cargo(&["package-workspace"]) + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] unexpected argument '--workspace' found + +Usage: cargo publish --no-verify --dry-run -Z + +For more information, try '--help'. + +"#]]) + .with_stdout_data(str![[r#""#]]) + .run(); + + p.cargo("publish --no-verify --dry-run -Zpackage-workspace --package a --package b") + .replace_crates_io(registry.index_url()) + .masquerade_as_nightly_cargo(&["package-workspace"]) + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] the argument '--package []' cannot be used multiple times + +Usage: cargo publish [OPTIONS] + +For more information, try '--help'. + +"#]]) + .with_stdout_data(str![[r#""#]]) + .run(); + + p.cargo("publish --no-verify --dry-run -Zpackage-workspace --workspace --exclude b") + .replace_crates_io(registry.index_url()) + .masquerade_as_nightly_cargo(&["package-workspace"]) + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] unexpected argument '--workspace' found + +Usage: cargo publish --no-verify --dry-run -Z + +For more information, try '--help'. + +"#]]) + .with_stdout_data(str![[r#""#]]) + .run(); + + p.cargo("publish --no-verify --dry-run --package a --package b") + .replace_crates_io(registry.index_url()) + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] the argument '--package []' cannot be used multiple times + +Usage: cargo publish [OPTIONS] + +For more information, try '--help'. + +"#]]) + .with_stdout_data(str![[r#""#]]) + .run(); + + p.cargo("publish --no-verify --dry-run --workspace") + .replace_crates_io(registry.index_url()) + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] unexpected argument '--workspace' found + +Usage: cargo publish --no-verify --dry-run + +For more information, try '--help'. + +"#]]) + .with_stdout_data(str![[r#""#]]) + .run(); + + p.cargo("publish --no-verify --dry-run --exclude b") + .replace_crates_io(registry.index_url()) + .with_status(1) + .with_stderr_data(str![[r#" +[ERROR] unexpected argument '--exclude' found + +Usage: cargo publish --no-verify --dry-run + +For more information, try '--help'. + +"#]]) + .with_stdout_data(str![[r#""#]]) + .run(); +} + #[cargo_test] fn wait_for_git_publish() { // Slow publish to an index with a git index. From 4e051c47bee0a940d760baea768a26b692c8deb5 Mon Sep 17 00:00:00 2001 From: Joe Neeman Date: Thu, 10 Oct 2024 09:19:21 +0700 Subject: [PATCH 2/3] Support package selection in cargo publish --- src/bin/cargo/commands/publish.rs | 23 +++++- .../cargo_publish/help/stdout.term.svg | 48 ++++++----- tests/testsuite/publish.rs | 81 ++++++++++--------- 3 files changed, 93 insertions(+), 59 deletions(-) diff --git a/src/bin/cargo/commands/publish.rs b/src/bin/cargo/commands/publish.rs index df1c4654ffe..42b6377c7e4 100644 --- a/src/bin/cargo/commands/publish.rs +++ b/src/bin/cargo/commands/publish.rs @@ -18,7 +18,11 @@ pub fn cli() -> Command { "Allow dirty working directories to be packaged", )) .arg_silent_suggestion() - .arg_package("Package to publish") + .arg_package_spec_no_all( + "Package(s) to publish", + "Publish all packages in the workspace (unstable)", + "Don't publish specified packages (unstable)", + ) .arg_features() .arg_parallel() .arg_target_triple("Build for the target triple") @@ -41,6 +45,23 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { .into()); } + let unstable = gctx.cli_unstable(); + let enabled = unstable.package_workspace; + if args.get_flag("workspace") { + unstable.fail_if_stable_opt_custom_z("--workspace", 10948, "package-workspace", enabled)?; + } + if args._value_of("exclude").is_some() { + unstable.fail_if_stable_opt_custom_z("--exclude", 10948, "package-workspace", enabled)?; + } + if args._values_of("package").len() > 1 { + unstable.fail_if_stable_opt_custom_z( + "--package (multiple occurrences)", + 10948, + "package-workspace", + enabled, + )?; + } + ops::publish( &ws, &PublishOpts { diff --git a/tests/testsuite/cargo_publish/help/stdout.term.svg b/tests/testsuite/cargo_publish/help/stdout.term.svg index c678fe8f83c..40981eb68db 100644 --- a/tests/testsuite/cargo_publish/help/stdout.term.svg +++ b/tests/testsuite/cargo_publish/help/stdout.term.svg @@ -1,4 +1,4 @@ - + crates-io. ### Package Selection -By default, the package in the current working directory is selected. The `-p` -flag can be used to choose a different package in a workspace. +By default, when no package selection options are given, the packages selected +depend on the selected manifest file (based on the current working directory if +`--manifest-path` is not given). If the manifest is the root of a workspace then +the workspaces default members are selected, otherwise only the package defined +by the manifest will be selected. + +The default members of a workspace can be set explicitly with the +`workspace.default-members` key in the root manifest. If this is not set, a +virtual workspace will include all workspace members (equivalent to passing +`--workspace`), and a non-virtual workspace will include only the root crate itself. + +Selecting more than one package is unstable and available only on the +[nightly channel](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) +and requires the `-Z package-workspace` flag to enable. +See for more information. +
-
-p spec
-
--package spec
-
The package to publish. See cargo-pkgid(1) for the SPEC -format.
+
-p spec
+
--package spec
+
Publish only the specified packages. See cargo-pkgid(1) for the +SPEC format. This flag may be specified multiple times and supports common Unix +glob patterns like *, ? and []. However, to avoid your shell accidentally +expanding glob patterns before Cargo handles them, you must use single quotes or +double quotes around each pattern.
+ + +Selecting more than one package with this option is unstable and available only +on the +[nightly channel](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) +and requires the `-Z package-workspace` flag to enable. +See for more information. + +
--workspace
+
Publish all members in the workspace.

+

This option is unstable and available only on the +nightly channel +and requires the -Z package-workspace flag to enable. +See https://github.com/rust-lang/cargo/issues/10948 for more information.

+ + +
--exclude SPEC
+
Exclude the specified packages. Must be used in conjunction with the +--workspace flag. This flag may be specified multiple times and supports +common Unix glob patterns like *, ? and []. However, to avoid your shell +accidentally expanding glob patterns before Cargo handles them, you must use +single quotes or double quotes around each pattern.

+

This option is unstable and available only on the +nightly channel +and requires the -Z package-workspace flag to enable. +See https://github.com/rust-lang/cargo/issues/10948 for more information.

diff --git a/src/etc/man/cargo-publish.1 b/src/etc/man/cargo-publish.1 index c73dc28e8d5..8a33750d8be 100644 --- a/src/etc/man/cargo-publish.1 +++ b/src/etc/man/cargo-publish.1 @@ -89,14 +89,59 @@ Otherwise it will use the default registry, which is defined by the which defaults to \fBcrates\-io\fR\&. .RE .SS "Package Selection" -By default, the package in the current working directory is selected. The \fB\-p\fR -flag can be used to choose a different package in a workspace. -.sp -\fB\-p\fR \fIspec\fR, -\fB\-\-package\fR \fIspec\fR -.RS 4 -The package to publish. See \fBcargo\-pkgid\fR(1) for the SPEC -format. +By default, when no package selection options are given, the packages selected +depend on the selected manifest file (based on the current working directory if +\fB\-\-manifest\-path\fR is not given). If the manifest is the root of a workspace then +the workspaces default members are selected, otherwise only the package defined +by the manifest will be selected. +.sp +The default members of a workspace can be set explicitly with the +\fBworkspace.default\-members\fR key in the root manifest. If this is not set, a +virtual workspace will include all workspace members (equivalent to passing +\fB\-\-workspace\fR), and a non\-virtual workspace will include only the root crate itself. +.sp +Selecting more than one package is unstable and available only on the +\fInightly channel\fR +and requires the \fB\-Z package\-workspace\fR flag to enable. +See for more information. +.sp +\fB\-p\fR \fIspec\fR\[u2026], +\fB\-\-package\fR \fIspec\fR\[u2026] +.RS 4 +Publish only the specified packages. See \fBcargo\-pkgid\fR(1) for the +SPEC format. This flag may be specified multiple times and supports common Unix +glob patterns like \fB*\fR, \fB?\fR and \fB[]\fR\&. However, to avoid your shell accidentally +expanding glob patterns before Cargo handles them, you must use single quotes or +double quotes around each pattern. +.RE +Selecting more than one package with this option is unstable and available only +on the +[nightly channel](https://doc.rust-lang.org/book/appendix-07-nightly-rust.html) +and requires the `-Z package-workspace` flag to enable. +See for more information. +.sp +\fB\-\-workspace\fR +.RS 4 +Publish all members in the workspace. +.sp +This option is unstable and available only on the +\fInightly channel\fR +and requires the \fB\-Z package\-workspace\fR flag to enable. +See for more information. +.RE +.sp +\fB\-\-exclude\fR \fISPEC\fR\[u2026] +.RS 4 +Exclude the specified packages. Must be used in conjunction with the +\fB\-\-workspace\fR flag. This flag may be specified multiple times and supports +common Unix glob patterns like \fB*\fR, \fB?\fR and \fB[]\fR\&. However, to avoid your shell +accidentally expanding glob patterns before Cargo handles them, you must use +single quotes or double quotes around each pattern. +.sp +This option is unstable and available only on the +\fInightly channel\fR +and requires the \fB\-Z package\-workspace\fR flag to enable. +See for more information. .RE .SS "Compilation Options" .sp