Skip to content

Commit a9e0b50

Browse files
committed
Update auth error message to specify args for cargo login.
1 parent 117eb4b commit a9e0b50

File tree

4 files changed

+93
-2
lines changed

4 files changed

+93
-2
lines changed

src/cargo/sources/registry/http_remote.rs

+1
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ impl<'cfg> RegistryData for HttpRegistry<'cfg> {
551551
if self.auth_required {
552552
return Poll::Ready(err.context(auth::AuthorizationError {
553553
sid: self.source_id.clone(),
554+
default_registry: self.config.default_registry()?,
554555
login_url: self.login_url.clone(),
555556
reason: auth::AuthorizationErrorReason::TokenRejected,
556557
}));

src/cargo/util/auth.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ impl fmt::Display for AuthorizationErrorReason {
347347
pub struct AuthorizationError {
348348
/// Url that was attempted
349349
pub sid: SourceId,
350+
/// The `registry.default` config value.
351+
pub default_registry: Option<String>,
350352
/// Url where the user could log in.
351353
pub login_url: Option<Url>,
352354
/// Specific reason indicating what failed
@@ -356,9 +358,14 @@ impl Error for AuthorizationError {}
356358
impl fmt::Display for AuthorizationError {
357359
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
358360
if self.sid.is_crates_io() {
361+
let args = if self.default_registry.is_some() {
362+
" --registry crates-io"
363+
} else {
364+
""
365+
};
359366
write!(
360367
f,
361-
"{}, please run `cargo login`\nor use environment variable CARGO_REGISTRY_TOKEN",
368+
"{}, please run `cargo login{args}`\nor use environment variable CARGO_REGISTRY_TOKEN",
362369
self.reason
363370
)
364371
} else if let Some(name) = self.sid.alt_registry_key() {
@@ -421,6 +428,7 @@ pub fn auth_token(
421428
Some(token) => Ok(token.expose()),
422429
None => Err(AuthorizationError {
423430
sid: sid.clone(),
431+
default_registry: config.default_registry()?,
424432
login_url: login_url.cloned(),
425433
reason: AuthorizationErrorReason::TokenMissing,
426434
}

tests/testsuite/registry.rs

+82
Original file line numberDiff line numberDiff line change
@@ -3192,3 +3192,85 @@ required by package `foo v0.0.1 ([ROOT]/foo)`
31923192
]
31933193
);
31943194
}
3195+
3196+
#[cargo_test]
3197+
fn default_auth_error() {
3198+
// Check for the error message for an authentication error when default is set.
3199+
let crates_io = RegistryBuilder::new().http_api().build();
3200+
let _alternative = RegistryBuilder::new().http_api().alternative().build();
3201+
3202+
paths::home().join(".cargo/credentials.toml").rm_rf();
3203+
3204+
let p = project()
3205+
.file(
3206+
"Cargo.toml",
3207+
r#"
3208+
[package]
3209+
name = "foo"
3210+
version = "0.1.0"
3211+
license = "MIT"
3212+
description = "foo"
3213+
"#,
3214+
)
3215+
.file("src/lib.rs", "")
3216+
.build();
3217+
3218+
// Test output before setting the default.
3219+
p.cargo("publish --no-verify")
3220+
.replace_crates_io(crates_io.index_url())
3221+
.with_stderr(
3222+
"\
3223+
[UPDATING] crates.io index
3224+
error: no token found, please run `cargo login`
3225+
or use environment variable CARGO_REGISTRY_TOKEN
3226+
",
3227+
)
3228+
.with_status(101)
3229+
.run();
3230+
3231+
p.cargo("publish --no-verify --registry alternative")
3232+
.replace_crates_io(crates_io.index_url())
3233+
.with_stderr(
3234+
"\
3235+
[UPDATING] `alternative` index
3236+
error: no token found for `alternative`, please run `cargo login --registry alternative`
3237+
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
3238+
",
3239+
)
3240+
.with_status(101)
3241+
.run();
3242+
3243+
// Test the output with the default.
3244+
cargo_util::paths::append(
3245+
&cargo_home().join("config"),
3246+
br#"
3247+
[registry]
3248+
default = "alternative"
3249+
"#,
3250+
)
3251+
.unwrap();
3252+
3253+
p.cargo("publish --no-verify")
3254+
.replace_crates_io(crates_io.index_url())
3255+
.with_stderr(
3256+
"\
3257+
[UPDATING] `alternative` index
3258+
error: no token found for `alternative`, please run `cargo login --registry alternative`
3259+
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
3260+
",
3261+
)
3262+
.with_status(101)
3263+
.run();
3264+
3265+
p.cargo("publish --no-verify --registry crates-io")
3266+
.replace_crates_io(crates_io.index_url())
3267+
.with_stderr(
3268+
"\
3269+
[UPDATING] crates.io index
3270+
error: no token found, please run `cargo login --registry crates-io`
3271+
or use environment variable CARGO_REGISTRY_TOKEN
3272+
",
3273+
)
3274+
.with_status(101)
3275+
.run();
3276+
}

tests/testsuite/registry_auth.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Tests for normal registry dependencies.
1+
//! Tests for registry authentication.
22
33
use cargo_test_support::registry::{Package, RegistryBuilder};
44
use cargo_test_support::{project, Execs, Project};

0 commit comments

Comments
 (0)