diff --git a/src/cargo/ops/registry/publish.rs b/src/cargo/ops/registry/publish.rs index 5009c0b67cc..201907bb268 100644 --- a/src/cargo/ops/registry/publish.rs +++ b/src/cargo/ops/registry/publish.rs @@ -103,7 +103,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> { if allowed_registries.is_empty() { bail!( "`{}` cannot be published.\n\ - `package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing.", + `package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.", pkg.name(), ); } else if !allowed_registries.contains(®_name) { diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 6c7add9e814..52f99bb59dc 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -1012,7 +1012,8 @@ impl TomlManifest { let publish = match publish { Some(VecStringOrBool::VecString(ref vecstring)) => Some(vecstring.clone()), Some(VecStringOrBool::Bool(false)) => Some(vec![]), - None | Some(VecStringOrBool::Bool(true)) => None, + Some(VecStringOrBool::Bool(true)) => None, + None => version.is_none().then_some(vec![]), }; if version.is_none() && publish != Some(vec![]) { diff --git a/src/doc/src/reference/manifest.md b/src/doc/src/reference/manifest.md index 2f4920c0ba5..e3168a47fdc 100644 --- a/src/doc/src/reference/manifest.md +++ b/src/doc/src/reference/manifest.md @@ -109,7 +109,7 @@ resolve dependencies, and for guidelines on setting your own version. See the [SemVer compatibility] chapter for more details on exactly what constitutes a breaking change. -This field is optional and defaults to `0.0.0`. +This field is optional and defaults to `0.0.0`. The field is required for publishing packages. [Resolver]: resolver.md [SemVer compatibility]: semver.md @@ -472,23 +472,22 @@ if any of those files change. ### The `publish` field -The `publish` field can be used to prevent a package from being published to a -package registry (like *crates.io*) by mistake, for instance to keep a package -private in a company. - +The `publish` field can be used to control which registries names the package +may be published to: ```toml [package] # ... -publish = false +publish = ["some-registry-name"] ``` -The value may also be an array of strings which are registry names that are -allowed to be published to. - +To prevent a package from being published to a registry (like crates.io) by mistake, +for instance to keep a package private in a company, +you can omit the [`version`](#the-version-field) field. +If you'd like to be more explicit, you can disable publishing: ```toml [package] # ... -publish = ["some-registry-name"] +publish = false ``` If publish array contains a single registry, `cargo publish` command will use diff --git a/tests/testsuite/check.rs b/tests/testsuite/check.rs index f7c865dedba..03611ae67e7 100644 --- a/tests/testsuite/check.rs +++ b/tests/testsuite/check.rs @@ -1506,7 +1506,6 @@ fn versionless_package() { [package] name = "foo" description = "foo" - publish = false "#, ) .file("src/lib.rs", "") diff --git a/tests/testsuite/metadata.rs b/tests/testsuite/metadata.rs index 223a8f70784..888cdce8c0e 100644 --- a/tests/testsuite/metadata.rs +++ b/tests/testsuite/metadata.rs @@ -4273,7 +4273,6 @@ fn versionless_packages() { r#" [package] name = "bar" - publish = false [dependencies] foobar = "0.0.1" @@ -4286,7 +4285,6 @@ fn versionless_packages() { r#" [package] name = "baz" - publish = false [dependencies] foobar = "0.0.1" diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index efaaa01ff61..4ec4fc0d6fc 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -3105,7 +3105,6 @@ fn versionless_package() { [package] name = "foo" description = "foo" - publish = false "#, ) .file("src/main.rs", r#"fn main() { println!("hello"); }"#) diff --git a/tests/testsuite/publish.rs b/tests/testsuite/publish.rs index 7517cea57ef..5d29ac88a91 100644 --- a/tests/testsuite/publish.rs +++ b/tests/testsuite/publish.rs @@ -420,7 +420,7 @@ fn unpublishable_crate() { .with_stderr( "\ [ERROR] `foo` cannot be published. -`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing. +`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish. ", ) .run(); @@ -794,7 +794,7 @@ fn publish_empty_list() { .with_stderr( "\ [ERROR] `foo` cannot be published. -`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing. +`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish. ", ) .run(); @@ -1020,7 +1020,7 @@ fn block_publish_no_registry() { .with_stderr( "\ [ERROR] `foo` cannot be published. -`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing. +`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish. ", ) .run(); @@ -3027,10 +3027,8 @@ fn versionless_package() { .with_status(101) .with_stderr( "\ -error: failed to parse manifest at `[CWD]/Cargo.toml` - -Caused by: - `package.publish` requires `package.version` be specified +error: `foo` cannot be published. +`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish. ", ) .run();