Skip to content

Commit

Permalink
Auto merge of #8571 - canova:publish-default, r=ehuss
Browse files Browse the repository at this point in the history
Default cargo publish to the alt registry if it's the only allowed one

Hi, this PR fixes #8036. Previously if we had only one allowed registry in Cargo.toml file, `cargo publish` was failing. But with this PR, we are defaulting to the only allowed registry with printing a note if there is only one registry in that array. I believe this will make things easier for people who use an alternative registry all the time.
  • Loading branch information
bors committed Aug 6, 2020
2 parents 0aedda2 + a8ce734 commit 449743b
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ impl Package {
pub fn authors(&self) -> &Vec<String> {
&self.manifest().metadata().authors
}
/// Returns `true` if the package is set to publish.

/// Returns `None` if the package is set to publish.
/// Returns `Some(allowed_registries)` if publishing is limited to specified
/// registries or if package is set to not publish.
pub fn publish(&self) -> &Option<Vec<String>> {
self.manifest().publish()
}
Expand Down
21 changes: 18 additions & 3 deletions src/cargo/ops/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,25 @@ pub struct PublishOpts<'cfg> {

pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
let pkg = ws.current()?;
let mut publish_registry = opts.registry.clone();

if let Some(ref allowed_registries) = *pkg.publish() {
let reg_name = opts
.registry
if publish_registry.is_none() && allowed_registries.len() == 1 {
// If there is only one allowed registry, push to that one directly,
// even though there is no registry specified in the command.
let default_registry = &allowed_registries[0];
if default_registry != CRATES_IO_REGISTRY {
// Don't change the registry for crates.io and don't warn the user.
// crates.io will be defaulted even without this.
opts.config.shell().note(&format!(
"Found `{}` as only allowed registry. Publishing to it automatically.",
default_registry
))?;
publish_registry = Some(default_registry.clone());
}
}

let reg_name = publish_registry
.clone()
.unwrap_or_else(|| CRATES_IO_REGISTRY.to_string());
if !allowed_registries.contains(&reg_name) {
Expand All @@ -72,7 +87,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
opts.config,
opts.token.clone(),
opts.index.clone(),
opts.registry.clone(),
publish_registry,
true,
!opts.dry_run,
)?;
Expand Down
10 changes: 9 additions & 1 deletion src/doc/man/cargo-publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ Allow working directories with uncommitted VCS changes to be packaged.

{{> options-index }}

{{> options-registry }}
{{#option "`--registry` _registry_"}}
Name of the registry to publish to. Registry names are defined in [Cargo
config files](../reference/config.html). If not specified, and there is a
[`package.publish`](../reference/manifest.html#the-publish-field) field in
`Cargo.toml` with a single registry, then it will publish to that registry.
Otherwise it will use the default registry, which is defined by the
[`registry.default`](../reference/config.html#registry-default) config key
which defaults to `crates-io`.
{{/option}}

{{/options}}

Expand Down
13 changes: 9 additions & 4 deletions src/doc/man/generated_txt/cargo-publish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ OPTIONS
The URL of the registry index to use.

--registry registry
Name of the registry to use. Registry names are defined in Cargo
config files
Name of the registry to publish to. Registry names are defined in
Cargo config files
<https://doc.rust-lang.org/cargo/reference/config.html>. If not
specified, the default registry is used, which is defined by the
registry.default config key which defaults to crates-io.
specified, and there is a package.publish
<https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field>
field in Cargo.toml with a single registry, then it will publish to
that registry. Otherwise it will use the default registry, which is
defined by the registry.default
<https://doc.rust-lang.org/cargo/reference/config.html#registry-default>
config key which defaults to crates-io.

Compilation Options
--target triple
Expand Down
12 changes: 7 additions & 5 deletions src/doc/src/commands/cargo-publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ of the registry in all capital letters.</dd>


<dt class="option-term" id="option-cargo-publish---registry"><a class="option-anchor" href="#option-cargo-publish---registry"></a><code>--registry</code> <em>registry</em></dt>
<dd class="option-desc">Name of the registry to use. Registry names are defined in <a href="https://doc.rust-lang.org/cargo/reference/config.html">Cargo config
files</a>. If not specified, the default registry is used,
which is defined by the <code>registry.default</code> config key which defaults to
<code>crates-io</code>.</dd>

<dd class="option-desc">Name of the registry to publish to. Registry names are defined in <a href="https://doc.rust-lang.org/cargo/reference/config.html">Cargo
config files</a>. If not specified, and there is a
<a href="https://doc.rust-lang.org/cargo/reference/manifest.html#the-publish-field"><code>package.publish</code></a> field in
<code>Cargo.toml</code> with a single registry, then it will publish to that registry.
Otherwise it will use the default registry, which is defined by the
<a href="https://doc.rust-lang.org/cargo/reference/config.html#registry-default"><code>registry.default</code></a> config key
which defaults to <code>crates-io</code>.</dd>


</dl>
Expand Down
3 changes: 3 additions & 0 deletions src/doc/src/reference/manifest.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,9 @@ allowed to be published to.
publish = ["some-registry-name"]
```

If publish array contains a single registry, `cargo publish` command will use
it when `--registry` flag is not specified.

<a id="the-metadata-table-optional"></a>
#### The `metadata` table

Expand Down
11 changes: 7 additions & 4 deletions src/etc/man/cargo-publish.1
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@ The URL of the registry index to use.
.sp
\fB\-\-registry\fR \fIregistry\fR
.RS 4
Name of the registry to use. Registry names are defined in \fICargo config
files\fR <https://doc.rust\-lang.org/cargo/reference/config.html>\&. If not specified, the default registry is used,
which is defined by the \fBregistry.default\fR config key which defaults to
\fBcrates\-io\fR\&.
Name of the registry to publish to. Registry names are defined in \fICargo
config files\fR <https://doc.rust\-lang.org/cargo/reference/config.html>\&. If not specified, and there is a
\fI\f(BIpackage.publish\fI\fR <https://doc.rust\-lang.org/cargo/reference/manifest.html#the\-publish\-field> field in
\fBCargo.toml\fR with a single registry, then it will publish to that registry.
Otherwise it will use the default registry, which is defined by the
\fI\f(BIregistry.default\fI\fR <https://doc.rust\-lang.org/cargo/reference/config.html#registry\-default> config key
which defaults to \fBcrates\-io\fR\&.
.RE
.SS "Compilation Options"
.sp
Expand Down
76 changes: 76 additions & 0 deletions tests/testsuite/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,82 @@ fn publish_allowed_registry() {
);
}

#[cargo_test]
fn publish_implicitly_to_only_allowed_registry() {
registry::init();

let p = project().build();

let _ = repo(&paths::root().join("foo"))
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
documentation = "foo"
homepage = "foo"
repository = "foo"
publish = ["alternative"]
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("publish").run();

publish::validate_alt_upload(
CLEAN_FOO_JSON,
"foo-0.0.1.crate",
&[
"Cargo.lock",
"Cargo.toml",
"Cargo.toml.orig",
"src/main.rs",
".cargo_vcs_info.json",
],
);
}

#[cargo_test]
fn publish_fail_with_no_registry_specified() {
registry::init();

let p = project().build();

let _ = repo(&paths::root().join("foo"))
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
documentation = "foo"
homepage = "foo"
repository = "foo"
publish = ["alternative", "test"]
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

p.cargo("publish")
.with_status(101)
.with_stderr(
"\
[ERROR] `foo` cannot be published.
The registry `crates-io` is not listed in the `publish` value in Cargo.toml.
",
)
.run();
}

#[cargo_test]
fn block_publish_no_registry() {
registry::init();
Expand Down

0 comments on commit 449743b

Please sign in to comment.