Skip to content

Commit 8a8ef3e

Browse files
authored
feat (publish): deprecate --token option (#16046)
### What does this PR try to resolve? Closes #15274 This PR adds a warning when `--token` option is used with `cargo publish` command. This PR removes `--token` from help and docs for publish command. There's an `--index` option that still requires `--token` to be specified, but addressing that is not the part of this PR. ### How to test and review this PR? Please check changed test cases to see how behavior changes.
2 parents 801d9b4 + d4892c9 commit 8a8ef3e

File tree

10 files changed

+71
-94
lines changed

10 files changed

+71
-94
lines changed

src/bin/cargo/commands/publish.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use crate::command_prelude::*;
22

33
use cargo::ops::{self, PublishOpts};
4+
use cargo_credential::Secret;
45

56
pub fn cli() -> Command {
67
subcommand("publish")
78
.about("Upload a package to the registry")
89
.arg_dry_run("Perform all checks without uploading")
910
.arg_index("Registry index URL to upload the package to")
1011
.arg_registry("Registry to upload the package to")
11-
.arg(opt("token", "Token to use when uploading").value_name("TOKEN"))
12+
.arg(
13+
opt("token", "Token to use when uploading")
14+
.value_name("TOKEN")
15+
.hide(true),
16+
)
1217
.arg(flag(
1318
"no-verify",
1419
"Don't verify the contents by building them",
@@ -45,13 +50,16 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
4550
.into());
4651
}
4752

53+
let token = args.get_one::<String>("token").cloned().map(Secret::from);
54+
if token.is_some() {
55+
let _ = gctx.shell().warn("`cargo publish --token` is deprecated in favor of using `cargo login` and environment variables");
56+
}
57+
4858
ops::publish(
4959
&ws,
5060
&PublishOpts {
5161
gctx,
52-
token: args
53-
.get_one::<String>("token")
54-
.map(|s| s.to_string().into()),
62+
token,
5563
reg_or_index,
5664
verify: !args.flag("no-verify"),
5765
allow_dirty: args.flag("allow-dirty"),

src/doc/man/cargo-publish.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ following steps:
2828
and may timeout. In that case, you will need to check for completion
2929
manually. This timeout does not affect the upload.
3030

31-
This command requires you to be authenticated with either the `--token` option
32-
or using {{man "cargo-login" 1}}.
31+
This command requires you to be authenticated using {{man "cargo-login" 1}}
32+
or environment variables of the [`registry.token`](../reference/config.html#registrytoken)
33+
and [`registries.<name>.token`](../reference/config.html#registriesnametoken) config fields.
3334

3435
See [the reference](../reference/publishing.html) for more details about
3536
packaging and publishing.
@@ -44,8 +45,6 @@ packaging and publishing.
4445
Perform all checks without uploading.
4546
{{/option}}
4647

47-
{{> options-token }}
48-
4948
{{#option "`--no-verify`" }}
5049
Don't verify the contents by building them.
5150
{{/option}}

src/doc/man/generated_txt/cargo-publish.txt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ DESCRIPTION
2525
and may timeout. In that case, you will need to check for completion
2626
manually. This timeout does not affect the upload.
2727

28-
This command requires you to be authenticated with either the --token
29-
option or using cargo-login(1).
28+
This command requires you to be authenticated using cargo-login(1) or
29+
environment variables of the registry.token
30+
<https://doc.rust-lang.org/cargo/reference/config.html#registrytoken>
31+
and registries.<name>.token
32+
<https://doc.rust-lang.org/cargo/reference/config.html#registriesnametoken>
33+
config fields.
3034

3135
See the reference
3236
<https://doc.rust-lang.org/cargo/reference/publishing.html> for more
@@ -37,18 +41,6 @@ OPTIONS
3741
--dry-run
3842
Perform all checks without uploading.
3943

40-
--token token
41-
API token to use when authenticating. This overrides the token
42-
stored in the credentials file (which is created by cargo-login(1)).
43-
44-
Cargo config <https://doc.rust-lang.org/cargo/reference/config.html>
45-
environment variables can be used to override the tokens stored in
46-
the credentials file. The token for crates.io may be specified with
47-
the CARGO_REGISTRY_TOKEN environment variable. Tokens for other
48-
registries may be specified with environment variables of the form
49-
CARGO_REGISTRIES_NAME_TOKEN where NAME is the name of the registry
50-
in all capital letters.
51-
5244
--no-verify
5345
Don’t verify the contents by building them.
5446

src/doc/src/commands/cargo-publish.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ following steps:
2424
and may timeout. In that case, you will need to check for completion
2525
manually. This timeout does not affect the upload.
2626

27-
This command requires you to be authenticated with either the `--token` option
28-
or using [cargo-login(1)](cargo-login.html).
27+
This command requires you to be authenticated using [cargo-login(1)](cargo-login.html)
28+
or environment variables of the [`registry.token`](../reference/config.html#registrytoken)
29+
and [`registries.<name>.token`](../reference/config.html#registriesnametoken) config fields.
2930

3031
See [the reference](../reference/publishing.html) for more details about
3132
packaging and publishing.
@@ -40,17 +41,6 @@ packaging and publishing.
4041
<dd class="option-desc">Perform all checks without uploading.</dd>
4142

4243

43-
<dt class="option-term" id="option-cargo-publish---token"><a class="option-anchor" href="#option-cargo-publish---token"></a><code>--token</code> <em>token</em></dt>
44-
<dd class="option-desc">API token to use when authenticating. This overrides the token stored in
45-
the credentials file (which is created by <a href="cargo-login.html">cargo-login(1)</a>).</p>
46-
<p><a href="../reference/config.html">Cargo config</a> environment variables can be
47-
used to override the tokens stored in the credentials file. The token for
48-
crates.io may be specified with the <code>CARGO_REGISTRY_TOKEN</code> environment
49-
variable. Tokens for other registries may be specified with environment
50-
variables of the form <code>CARGO_REGISTRIES_NAME_TOKEN</code> where <code>NAME</code> is the name
51-
of the registry in all capital letters.</dd>
52-
53-
5444
<dt class="option-term" id="option-cargo-publish---no-verify"><a class="option-anchor" href="#option-cargo-publish---no-verify"></a><code>--no-verify</code></dt>
5545
<dd class="option-desc">Don’t verify the contents by building them.</dd>
5646

src/etc/_cargo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ _cargo() {
251251
'--index=[specify registry index]:index' \
252252
'--allow-dirty[allow dirty working directories to be packaged]' \
253253
"--no-verify[don't verify the contents by building them]" \
254-
'--token=[specify token to use when uploading]:token' \
255254
'--dry-run[perform all checks without uploading]'
256255
;;
257256

src/etc/cargo.bashcomp.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ _cargo()
7373
local opt__owner="$opt_common $opt_lock -a --add -r --remove -l --list --index --token --registry"
7474
local opt__package="$opt_common $opt_mani $opt_feat $opt_lock $opt_parallel --allow-dirty -l --list --no-verify --no-metadata --index --registry --target --target-dir"
7575
local opt__pkgid="$opt_common $opt_mani $opt_lock $opt_pkg"
76-
local opt__publish="$opt_common $opt_mani $opt_feat $opt_lock $opt_parallel --allow-dirty --dry-run --token --no-verify --index --registry --target --target-dir"
76+
local opt__publish="$opt_common $opt_mani $opt_feat $opt_lock $opt_parallel --allow-dirty --dry-run --no-verify --index --registry --target --target-dir"
7777
local opt__remove="$opt_common $opt_pkg $opt_lock $opt_mani --dry-run --dev --build --target"
7878
local opt__rm="$opt__remove"
7979
local opt__report="$opt_help $opt_verbose $opt_color future-incompat future-incompatibilities"

src/etc/man/cargo-publish.1

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ and may timeout. In that case, you will need to check for completion
3737
manually. This timeout does not affect the upload.
3838
.RE
3939
.sp
40-
This command requires you to be authenticated with either the \fB\-\-token\fR option
41-
or using \fBcargo\-login\fR(1).
40+
This command requires you to be authenticated using \fBcargo\-login\fR(1)
41+
or environment variables of the \fI\f(BIregistry.token\fI\fR <https://doc.rust\-lang.org/cargo/reference/config.html#registrytoken>
42+
and \fI\f(BIregistries.<name>.token\fI\fR <https://doc.rust\-lang.org/cargo/reference/config.html#registriesnametoken> config fields.
4243
.sp
4344
See \fIthe reference\fR <https://doc.rust\-lang.org/cargo/reference/publishing.html> for more details about
4445
packaging and publishing.
@@ -50,19 +51,6 @@ packaging and publishing.
5051
Perform all checks without uploading.
5152
.RE
5253
.sp
53-
\fB\-\-token\fR \fItoken\fR
54-
.RS 4
55-
API token to use when authenticating. This overrides the token stored in
56-
the credentials file (which is created by \fBcargo\-login\fR(1)).
57-
.sp
58-
\fICargo config\fR <https://doc.rust\-lang.org/cargo/reference/config.html> environment variables can be
59-
used to override the tokens stored in the credentials file. The token for
60-
crates.io may be specified with the \fBCARGO_REGISTRY_TOKEN\fR environment
61-
variable. Tokens for other registries may be specified with environment
62-
variables of the form \fBCARGO_REGISTRIES_NAME_TOKEN\fR where \fBNAME\fR is the name
63-
of the registry in all capital letters.
64-
.RE
65-
.sp
6654
\fB\-\-no\-verify\fR
6755
.RS 4
6856
Don\[cq]t verify the contents by building them.

tests/testsuite/alt_registry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ Caused by:
306306
.arg(crates_io.index_url().as_str())
307307
.with_status(101)
308308
.with_stderr_data(str![[r#"
309+
[WARNING] `cargo publish --token` is deprecated in favor of using `cargo login` and environment variables
309310
[UPDATING] crates.io index
310311
[ERROR] failed to verify manifest at `[ROOT]/foo/Cargo.toml`
311312

0 commit comments

Comments
 (0)