Skip to content
Draft
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
53 changes: 29 additions & 24 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,30 +181,50 @@ pub fn exit_with_error(err: CliError, shell: &mut Shell) -> ! {

/// Displays an error, and all its causes, to stderr.
pub fn display_error(err: &Error, shell: &mut Shell) {
use annotate_snippets::*;

debug!("display_error; err={:?}", err);
_display_error(err, shell, true);

let mut errs = error_chain(err, shell.verbosity());
let Some(first) = errs.next() else {
return;
};
let mut group = Group::with_title(Level::ERROR.primary_title(first.to_string()));
for err in errs {
group = group.element(Level::ERROR.with_name("caused by").message(err.to_string()));
}

if err
.chain()
.any(|e| e.downcast_ref::<InternalError>().is_some())
{
drop(shell.note("this is an unexpected cargo internal error"));
drop(
shell.note(
group = group.elements([
Level::NOTE.message("this is an unexpected cargo internal error"),
Level::NOTE.message(
"we would appreciate a bug report: https://github.com/rust-lang/cargo/issues/",
),
);
drop(shell.note(format!("cargo {}", version())));
Level::NOTE.message(format!("cargo {}", version())),
]);
// Once backtraces are stabilized, this should print out a backtrace
// if it is available.
}

drop(shell.print_report(&[group], true));
}

/// Displays a warning, with an error object providing detailed information
/// and context.
pub fn display_warning_with_error(warning: &str, err: &Error, shell: &mut Shell) {
drop(shell.warn(warning));
drop(writeln!(shell.err()));
_display_error(err, shell, false);
use annotate_snippets::*;

let mut group = Group::with_title(Level::WARNING.primary_title(warning));

let errs = error_chain(err, shell.verbosity());
for err in errs {
group = group.element(Level::ERROR.with_name("caused by").message(err.to_string()));
}

drop(shell.print_report(&[group], true));
}

fn error_chain(err: &Error, verbosity: Verbosity) -> impl Iterator<Item = &dyn std::fmt::Display> {
Expand All @@ -219,18 +239,3 @@ fn error_chain(err: &Error, verbosity: Verbosity) -> impl Iterator<Item = &dyn s
.take_while(|err| !err.is::<AlreadyPrintedError>())
.map(|err| err as &dyn std::fmt::Display)
}

fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) {
for (i, err) in error_chain(err, shell.verbosity()).enumerate() {
if i == 0 {
if as_err {
drop(shell.error(&err));
} else {
drop(writeln!(shell.err(), "{}", err));
}
} else {
drop(writeln!(shell.err(), "\nCaused by:"));
drop(write!(shell.err(), "{}", indented_lines(&err.to_string())));
}
}
}
45 changes: 19 additions & 26 deletions tests/testsuite/alt_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,8 @@ fn registry_incompatible_with_git() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
dependency (bar) specification is ambiguous. Only one of `git` or `registry` is allowed.
|
= caused by: dependency (bar) specification is ambiguous. Only one of `git` or `registry` is allowed.

"#]])
.run();
Expand Down Expand Up @@ -289,11 +288,10 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {
.with_stderr_data(str![[r#"
[UPDATING] crates.io index
[ERROR] failed to verify manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
crates cannot be published to crates.io with dependencies sourced from other
registries. `bar` needs to be published to crates.io before publishing this crate.
(crate `bar` is pulled from registry `alternative`)
|
= caused by: crates cannot be published to crates.io with dependencies sourced from other
registries. `bar` needs to be published to crates.io before publishing this crate.
(crate `bar` is pulled from registry `alternative`)

"#]])
.run();
Expand All @@ -308,11 +306,10 @@ Caused by:
.with_stderr_data(str![[r#"
[UPDATING] crates.io index
[ERROR] failed to verify manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
crates cannot be published to crates.io with dependencies sourced from other
registries. `bar` needs to be published to crates.io before publishing this crate.
(crate `bar` is pulled from registry `alternative`)
|
= caused by: crates cannot be published to crates.io with dependencies sourced from other
registries. `bar` needs to be published to crates.io before publishing this crate.
(crate `bar` is pulled from registry `alternative`)

"#]])
.run();
Expand Down Expand Up @@ -469,7 +466,7 @@ fn block_publish_due_to_no_token() {
.with_stderr_data(str![[r#"
[UPDATING] `alternative` index
[ERROR] no token found for `alternative`, please run `cargo login --registry alternative`
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN

"#]])
.run();
Expand All @@ -496,7 +493,7 @@ fn cargo_registries_crates_io_protocol() {
.with_stderr_data(str![[r#"
[UPDATING] `alternative` index
[ERROR] no token found for `alternative`, please run `cargo login --registry alternative`
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN

"#]])
.run();
Expand Down Expand Up @@ -674,9 +671,8 @@ fn passwords_in_registries_index_url_forbidden() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] invalid index URL for registry `alternative` defined in [ROOT]/home/.cargo/config.toml

Caused by:
registry URLs may not contain passwords
|
= caused by: registry URLs may not contain passwords

"#]])
.run();
Expand Down Expand Up @@ -1711,12 +1707,9 @@ fn registries_index_relative_path_not_allowed() {
p.cargo("check")
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
invalid index URL for registry `relative` defined in [ROOT]/.cargo/config.toml

Caused by:
invalid url `alternative-registry`: relative URL without a base
|
= caused by: invalid index URL for registry `relative` defined in [ROOT]/.cargo/config.toml
= caused by: invalid url `alternative-registry`: relative URL without a base

"#]])
.with_status(101)
Expand Down Expand Up @@ -1885,7 +1878,7 @@ fn warn_for_unused_fields() {
[UPDATING] `alternative` index
[WARNING] unused config key `registries.alternative.unexpected-field` in `[ROOT]/foo/.cargo/config.toml`
[ERROR] no token found for `alternative`, please run `cargo login --registry alternative`
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN

"#]])
.run();
Expand All @@ -1900,7 +1893,7 @@ or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
[UPDATING] crates.io index
[WARNING] unused config key `registry.unexpected-field` in `[ROOT]/foo/.cargo/config.toml`
[ERROR] no token found, please run `cargo login`
or use environment variable CARGO_REGISTRY_TOKEN
or use environment variable CARGO_REGISTRY_TOKEN

"#]])
.run();
Expand Down
59 changes: 28 additions & 31 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ fn check_with_invalid_artifact_dependency() {
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
'unknown' is not a valid artifact specifier
|
= caused by: 'unknown' is not a valid artifact specifier

"#]])
.with_status(101)
Expand Down Expand Up @@ -82,9 +81,8 @@ Caused by:
cargo
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
'lib' specifier cannot be used without an 'artifact = …' value (bar)
|
= caused by: 'lib' specifier cannot be used without an 'artifact = …' value (bar)

"#]])
.with_status(101)
Expand Down Expand Up @@ -114,9 +112,8 @@ Caused by:
cargo
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
'target' specifier cannot be used without an 'artifact = …' value (bar)
|
= caused by: 'target' specifier cannot be used without an 'artifact = …' value (bar)

"#]])
.with_status(101)
Expand Down Expand Up @@ -150,12 +147,14 @@ fn check_with_invalid_target_triple() {
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_data(str![[r#"
[ERROR] failed to run `rustc` to learn about target-specific information

Caused by:
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target unknown-target-triple [..]` ([EXIT_STATUS]: 1)
--- stderr
...

|
= caused by: process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target unknown-target-triple --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` ([EXIT_STATUS]: 1)
--- stderr
[ERROR] error loading target specification: could not find specification for target "unknown-target-triple"
|
= [HELP] run `rustc --print target-list` for a list of built-in targets



"#]])
.with_status(101)
Expand Down Expand Up @@ -187,9 +186,8 @@ fn build_without_nightly_aborts_with_error() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
`artifact = …` requires `-Z bindeps` (bar)
|
= caused by: `artifact = …` requires `-Z bindeps` (bar)

"#]])
.run();
Expand Down Expand Up @@ -2089,9 +2087,8 @@ fn check_target_equals_target_in_non_build_dependency_errors() {
.with_status(101)
.with_stderr_data(str![[r#"
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`

Caused by:
`target = "target"` in normal- or dev-dependencies has no effect (bar)
|
= caused by: `target = "target"` in normal- or dev-dependencies has no effect (bar)

"#]])
.run();
Expand Down Expand Up @@ -3305,16 +3302,16 @@ fn check_transitive_artifact_dependency_with_different_target() {
.with_stderr_data(str![[r#"
[LOCKING] 2 packages to latest compatible versions
[ERROR] failed to determine target information for target `custom-target`.
Artifact dependency `baz` in package `bar v0.0.0 ([ROOT]/foo/bar)` requires building for `custom-target`

Caused by:
failed to run `rustc` to learn about target-specific information

Caused by:
process didn't exit successfully: `rustc [..] ([EXIT_STATUS]: 1)
--- stderr
...

Artifact dependency `baz` in package `bar v0.0.0 ([ROOT]/foo/bar)` requires building for `custom-target`
|
= caused by: failed to run `rustc` to learn about target-specific information
= caused by: process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target custom-target --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` ([EXIT_STATUS]: 1)
--- stderr
[ERROR] error loading target specification: could not find specification for target "custom-target"
|
= [HELP] run `rustc --print target-list` for a list of built-in targets

"#]])
.with_status(101)
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/artifact_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ fn artifact_dir_is_a_file() {
.with_stderr_data(str![[r#"
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[ERROR] failed to create directory `[ROOT]/foo/out`

Caused by:
|
= caused by: File exists (os error 17)
...

"#]])
Expand Down
Loading
Loading