From bfb5d1db0ecb50251d7d940e5fbf50c913e9ef41 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Fri, 6 Oct 2023 13:01:58 -0500 Subject: [PATCH] fix(toml): Default package.publish based on presence of package.version Before the default was hardcoded to `true`. The problem was that means that to remove the `package.version` boilerplate, you had to add `package.publish = false` boilerplate. To make the errors easier to understand in this situation, I err on the side of encouraging people to put `publish = true` in their manifests. By making this change, we also unblock "cargo script" / `Cargo.toml` unifying the handling of `package.publish`. --- src/cargo/ops/registry/publish.rs | 2 +- src/cargo/util/toml/mod.rs | 3 ++- src/doc/src/reference/manifest.md | 19 +++++++++---------- tests/testsuite/check.rs | 1 - tests/testsuite/metadata.rs | 2 -- tests/testsuite/package.rs | 1 - tests/testsuite/publish.rs | 12 +++++------- 7 files changed, 17 insertions(+), 23 deletions(-) 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();