Skip to content

Commit

Permalink
fix(toml): Default package.publish based on presence of package.version
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
epage committed Oct 11, 2023
1 parent f49d3c4 commit 9a0f6e6
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/cargo/ops/registry/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&reg_name) {
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![]) {
Expand Down
19 changes: 9 additions & 10 deletions src/doc/src/reference/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 leave off 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
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,7 +1506,6 @@ fn versionless_package() {
[package]
name = "foo"
description = "foo"
publish = false
"#,
)
.file("src/lib.rs", "")
Expand Down
2 changes: 0 additions & 2 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4273,7 +4273,6 @@ fn versionless_packages() {
r#"
[package]
name = "bar"
publish = false
[dependencies]
foobar = "0.0.1"
Expand All @@ -4286,7 +4285,6 @@ fn versionless_packages() {
r#"
[package]
name = "baz"
publish = false
[dependencies]
foobar = "0.0.1"
Expand Down
1 change: 0 additions & 1 deletion tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3105,7 +3105,6 @@ fn versionless_package() {
[package]
name = "foo"
description = "foo"
publish = false
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
Expand Down
12 changes: 5 additions & 7 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 9a0f6e6

Please sign in to comment.