Skip to content

Commit eeb73b9

Browse files
committed
fix(cli): Use Shell::print_report for process-exit errors
1 parent 913d664 commit eeb73b9

File tree

159 files changed

+2754
-3176
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

159 files changed

+2754
-3176
lines changed

src/cargo/lib.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,32 +181,35 @@ pub fn exit_with_error(err: CliError, shell: &mut Shell) -> ! {
181181

182182
/// Displays an error, and all its causes, to stderr.
183183
pub fn display_error(err: &Error, shell: &mut Shell) {
184+
use annotate_snippets::*;
185+
184186
debug!("display_error; err={:?}", err);
185187

186188
let mut errs = error_chain(err, shell.verbosity());
187189
let Some(first) = errs.next() else {
188190
return;
189191
};
190-
drop(shell.error(&first));
192+
let mut group = Group::with_title(Level::ERROR.primary_title(first.to_string()));
191193
for err in errs {
192-
drop(writeln!(shell.err(), "\nCaused by:"));
193-
drop(write!(shell.err(), "{}", indented_lines(&err.to_string())));
194+
group = group.element(Level::ERROR.with_name("caused by").message(err.to_string()));
194195
}
195196

196197
if err
197198
.chain()
198199
.any(|e| e.downcast_ref::<InternalError>().is_some())
199200
{
200-
drop(shell.note("this is an unexpected cargo internal error"));
201-
drop(
202-
shell.note(
201+
group = group.elements([
202+
Level::NOTE.message("this is an unexpected cargo internal error"),
203+
Level::NOTE.message(
203204
"we would appreciate a bug report: https://github.com/rust-lang/cargo/issues/",
204205
),
205-
);
206-
drop(shell.note(format!("cargo {}", version())));
206+
Level::NOTE.message(format!("cargo {}", version())),
207+
]);
207208
// Once backtraces are stabilized, this should print out a backtrace
208209
// if it is available.
209210
}
211+
212+
drop(shell.print_report(&[group], true));
210213
}
211214

212215
/// Displays a warning, with an error object providing detailed information

tests/testsuite/alt_registry.rs

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ fn registry_incompatible_with_git() {
252252
.with_status(101)
253253
.with_stderr_data(str![[r#"
254254
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
255-
256-
Caused by:
257-
dependency (bar) specification is ambiguous. Only one of `git` or `registry` is allowed.
255+
|
256+
= caused by: dependency (bar) specification is ambiguous. Only one of `git` or `registry` is allowed.
258257
259258
"#]])
260259
.run();
@@ -289,11 +288,10 @@ fn cannot_publish_to_crates_io_with_registry_dependency() {
289288
.with_stderr_data(str![[r#"
290289
[UPDATING] crates.io index
291290
[ERROR] failed to verify manifest at `[ROOT]/foo/Cargo.toml`
292-
293-
Caused by:
294-
crates cannot be published to crates.io with dependencies sourced from other
295-
registries. `bar` needs to be published to crates.io before publishing this crate.
296-
(crate `bar` is pulled from registry `alternative`)
291+
|
292+
= caused by: crates cannot be published to crates.io with dependencies sourced from other
293+
registries. `bar` needs to be published to crates.io before publishing this crate.
294+
(crate `bar` is pulled from registry `alternative`)
297295
298296
"#]])
299297
.run();
@@ -308,11 +306,10 @@ Caused by:
308306
.with_stderr_data(str![[r#"
309307
[UPDATING] crates.io index
310308
[ERROR] failed to verify manifest at `[ROOT]/foo/Cargo.toml`
311-
312-
Caused by:
313-
crates cannot be published to crates.io with dependencies sourced from other
314-
registries. `bar` needs to be published to crates.io before publishing this crate.
315-
(crate `bar` is pulled from registry `alternative`)
309+
|
310+
= caused by: crates cannot be published to crates.io with dependencies sourced from other
311+
registries. `bar` needs to be published to crates.io before publishing this crate.
312+
(crate `bar` is pulled from registry `alternative`)
316313
317314
"#]])
318315
.run();
@@ -469,7 +466,7 @@ fn block_publish_due_to_no_token() {
469466
.with_stderr_data(str![[r#"
470467
[UPDATING] `alternative` index
471468
[ERROR] no token found for `alternative`, please run `cargo login --registry alternative`
472-
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
469+
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
473470
474471
"#]])
475472
.run();
@@ -496,7 +493,7 @@ fn cargo_registries_crates_io_protocol() {
496493
.with_stderr_data(str![[r#"
497494
[UPDATING] `alternative` index
498495
[ERROR] no token found for `alternative`, please run `cargo login --registry alternative`
499-
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
496+
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
500497
501498
"#]])
502499
.run();
@@ -674,9 +671,8 @@ fn passwords_in_registries_index_url_forbidden() {
674671
.with_status(101)
675672
.with_stderr_data(str![[r#"
676673
[ERROR] invalid index URL for registry `alternative` defined in [ROOT]/home/.cargo/config.toml
677-
678-
Caused by:
679-
registry URLs may not contain passwords
674+
|
675+
= caused by: registry URLs may not contain passwords
680676
681677
"#]])
682678
.run();
@@ -1711,12 +1707,9 @@ fn registries_index_relative_path_not_allowed() {
17111707
p.cargo("check")
17121708
.with_stderr_data(str![[r#"
17131709
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
1714-
1715-
Caused by:
1716-
invalid index URL for registry `relative` defined in [ROOT]/.cargo/config.toml
1717-
1718-
Caused by:
1719-
invalid url `alternative-registry`: relative URL without a base
1710+
|
1711+
= caused by: invalid index URL for registry `relative` defined in [ROOT]/.cargo/config.toml
1712+
= caused by: invalid url `alternative-registry`: relative URL without a base
17201713
17211714
"#]])
17221715
.with_status(101)
@@ -1885,7 +1878,7 @@ fn warn_for_unused_fields() {
18851878
[UPDATING] `alternative` index
18861879
[WARNING] unused config key `registries.alternative.unexpected-field` in `[ROOT]/foo/.cargo/config.toml`
18871880
[ERROR] no token found for `alternative`, please run `cargo login --registry alternative`
1888-
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
1881+
or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
18891882
18901883
"#]])
18911884
.run();
@@ -1900,7 +1893,7 @@ or use environment variable CARGO_REGISTRIES_ALTERNATIVE_TOKEN
19001893
[UPDATING] crates.io index
19011894
[WARNING] unused config key `registry.unexpected-field` in `[ROOT]/foo/.cargo/config.toml`
19021895
[ERROR] no token found, please run `cargo login`
1903-
or use environment variable CARGO_REGISTRY_TOKEN
1896+
or use environment variable CARGO_REGISTRY_TOKEN
19041897
19051898
"#]])
19061899
.run();

tests/testsuite/artifact_dep.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ fn check_with_invalid_artifact_dependency() {
3939
.masquerade_as_nightly_cargo(&["bindeps"])
4040
.with_stderr_data(str![[r#"
4141
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
42-
43-
Caused by:
44-
'unknown' is not a valid artifact specifier
42+
|
43+
= caused by: 'unknown' is not a valid artifact specifier
4544
4645
"#]])
4746
.with_status(101)
@@ -82,9 +81,8 @@ Caused by:
8281
cargo
8382
.with_stderr_data(str![[r#"
8483
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
85-
86-
Caused by:
87-
'lib' specifier cannot be used without an 'artifact = …' value (bar)
84+
|
85+
= caused by: 'lib' specifier cannot be used without an 'artifact = …' value (bar)
8886
8987
"#]])
9088
.with_status(101)
@@ -114,9 +112,8 @@ Caused by:
114112
cargo
115113
.with_stderr_data(str![[r#"
116114
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
117-
118-
Caused by:
119-
'target' specifier cannot be used without an 'artifact = …' value (bar)
115+
|
116+
= caused by: 'target' specifier cannot be used without an 'artifact = …' value (bar)
120117
121118
"#]])
122119
.with_status(101)
@@ -150,12 +147,14 @@ fn check_with_invalid_target_triple() {
150147
.masquerade_as_nightly_cargo(&["bindeps"])
151148
.with_stderr_data(str![[r#"
152149
[ERROR] failed to run `rustc` to learn about target-specific information
153-
154-
Caused by:
155-
process didn't exit successfully: `rustc - --crate-name ___ --print=file-names --target unknown-target-triple [..]` ([EXIT_STATUS]: 1)
156-
--- stderr
157-
...
158-
150+
|
151+
= 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)
152+
--- stderr
153+
[ERROR] error loading target specification: could not find specification for target "unknown-target-triple"
154+
|
155+
= [HELP] run `rustc --print target-list` for a list of built-in targets
156+
157+
159158
160159
"#]])
161160
.with_status(101)
@@ -187,9 +186,8 @@ fn build_without_nightly_aborts_with_error() {
187186
.with_status(101)
188187
.with_stderr_data(str![[r#"
189188
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
190-
191-
Caused by:
192-
`artifact = …` requires `-Z bindeps` (bar)
189+
|
190+
= caused by: `artifact = …` requires `-Z bindeps` (bar)
193191
194192
"#]])
195193
.run();
@@ -2089,9 +2087,8 @@ fn check_target_equals_target_in_non_build_dependency_errors() {
20892087
.with_status(101)
20902088
.with_stderr_data(str![[r#"
20912089
[ERROR] failed to parse manifest at `[ROOT]/foo/Cargo.toml`
2092-
2093-
Caused by:
2094-
`target = "target"` in normal- or dev-dependencies has no effect (bar)
2090+
|
2091+
= caused by: `target = "target"` in normal- or dev-dependencies has no effect (bar)
20952092
20962093
"#]])
20972094
.run();
@@ -3305,16 +3302,16 @@ fn check_transitive_artifact_dependency_with_different_target() {
33053302
.with_stderr_data(str![[r#"
33063303
[LOCKING] 2 packages to latest compatible versions
33073304
[ERROR] failed to determine target information for target `custom-target`.
3308-
Artifact dependency `baz` in package `bar v0.0.0 ([ROOT]/foo/bar)` requires building for `custom-target`
3309-
3310-
Caused by:
3311-
failed to run `rustc` to learn about target-specific information
3312-
3313-
Caused by:
3314-
process didn't exit successfully: `rustc [..] ([EXIT_STATUS]: 1)
3315-
--- stderr
3316-
...
3317-
3305+
Artifact dependency `baz` in package `bar v0.0.0 ([ROOT]/foo/bar)` requires building for `custom-target`
3306+
|
3307+
= caused by: failed to run `rustc` to learn about target-specific information
3308+
= 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)
3309+
--- stderr
3310+
[ERROR] error loading target specification: could not find specification for target "custom-target"
3311+
|
3312+
= [HELP] run `rustc --print target-list` for a list of built-in targets
3313+
3314+
33183315
33193316
"#]])
33203317
.with_status(101)

tests/testsuite/artifact_dir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ fn artifact_dir_is_a_file() {
194194
.with_stderr_data(str![[r#"
195195
[COMPILING] foo v0.0.1 ([ROOT]/foo)
196196
[ERROR] failed to create directory `[ROOT]/foo/out`
197-
198-
Caused by:
197+
|
198+
= caused by: File exists (os error 17)
199199
...
200200
201201
"#]])

0 commit comments

Comments
 (0)