Skip to content

Commit bfb5d1d

Browse files
committed
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`.
1 parent cc9be44 commit bfb5d1d

File tree

7 files changed

+17
-23
lines changed

7 files changed

+17
-23
lines changed

src/cargo/ops/registry/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
103103
if allowed_registries.is_empty() {
104104
bail!(
105105
"`{}` cannot be published.\n\
106-
`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing.",
106+
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.",
107107
pkg.name(),
108108
);
109109
} else if !allowed_registries.contains(&reg_name) {

src/cargo/util/toml/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,8 @@ impl TomlManifest {
10121012
let publish = match publish {
10131013
Some(VecStringOrBool::VecString(ref vecstring)) => Some(vecstring.clone()),
10141014
Some(VecStringOrBool::Bool(false)) => Some(vec![]),
1015-
None | Some(VecStringOrBool::Bool(true)) => None,
1015+
Some(VecStringOrBool::Bool(true)) => None,
1016+
None => version.is_none().then_some(vec![]),
10161017
};
10171018

10181019
if version.is_none() && publish != Some(vec![]) {

src/doc/src/reference/manifest.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ resolve dependencies, and for guidelines on setting your own version. See the
109109
[SemVer compatibility] chapter for more details on exactly what constitutes a
110110
breaking change.
111111

112-
This field is optional and defaults to `0.0.0`.
112+
This field is optional and defaults to `0.0.0`. The field is required for publishing packages.
113113

114114
[Resolver]: resolver.md
115115
[SemVer compatibility]: semver.md
@@ -472,23 +472,22 @@ if any of those files change.
472472

473473
### The `publish` field
474474

475-
The `publish` field can be used to prevent a package from being published to a
476-
package registry (like *crates.io*) by mistake, for instance to keep a package
477-
private in a company.
478-
475+
The `publish` field can be used to control which registries names the package
476+
may be published to:
479477
```toml
480478
[package]
481479
# ...
482-
publish = false
480+
publish = ["some-registry-name"]
483481
```
484482

485-
The value may also be an array of strings which are registry names that are
486-
allowed to be published to.
487-
483+
To prevent a package from being published to a registry (like crates.io) by mistake,
484+
for instance to keep a package private in a company,
485+
you can omit the [`version`](#the-version-field) field.
486+
If you'd like to be more explicit, you can disable publishing:
488487
```toml
489488
[package]
490489
# ...
491-
publish = ["some-registry-name"]
490+
publish = false
492491
```
493492

494493
If publish array contains a single registry, `cargo publish` command will use

tests/testsuite/check.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,6 @@ fn versionless_package() {
15061506
[package]
15071507
name = "foo"
15081508
description = "foo"
1509-
publish = false
15101509
"#,
15111510
)
15121511
.file("src/lib.rs", "")

tests/testsuite/metadata.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4273,7 +4273,6 @@ fn versionless_packages() {
42734273
r#"
42744274
[package]
42754275
name = "bar"
4276-
publish = false
42774276
42784277
[dependencies]
42794278
foobar = "0.0.1"
@@ -4286,7 +4285,6 @@ fn versionless_packages() {
42864285
r#"
42874286
[package]
42884287
name = "baz"
4289-
publish = false
42904288
42914289
[dependencies]
42924290
foobar = "0.0.1"

tests/testsuite/package.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,6 @@ fn versionless_package() {
31053105
[package]
31063106
name = "foo"
31073107
description = "foo"
3108-
publish = false
31093108
"#,
31103109
)
31113110
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)

tests/testsuite/publish.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ fn unpublishable_crate() {
420420
.with_stderr(
421421
"\
422422
[ERROR] `foo` cannot be published.
423-
`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing.
423+
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.
424424
",
425425
)
426426
.run();
@@ -794,7 +794,7 @@ fn publish_empty_list() {
794794
.with_stderr(
795795
"\
796796
[ERROR] `foo` cannot be published.
797-
`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing.
797+
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.
798798
",
799799
)
800800
.run();
@@ -1020,7 +1020,7 @@ fn block_publish_no_registry() {
10201020
.with_stderr(
10211021
"\
10221022
[ERROR] `foo` cannot be published.
1023-
`package.publish` is set to `false` or an empty list in Cargo.toml and prevents publishing.
1023+
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.
10241024
",
10251025
)
10261026
.run();
@@ -3027,10 +3027,8 @@ fn versionless_package() {
30273027
.with_status(101)
30283028
.with_stderr(
30293029
"\
3030-
error: failed to parse manifest at `[CWD]/Cargo.toml`
3031-
3032-
Caused by:
3033-
`package.publish` requires `package.version` be specified
3030+
error: `foo` cannot be published.
3031+
`package.publish` must be set to `true` or a non-empty list in Cargo.toml to publish.
30343032
",
30353033
)
30363034
.run();

0 commit comments

Comments
 (0)