Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default cargo publish to the alt registry if it's the only allowed one #8571

Merged
merged 5 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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