Skip to content

Commit 1439b3f

Browse files
committed
Don't print rustdoc command lines on failure by default
This commit lifts a helper function from invoking rustc to additionally being used for invoking rustdoc. This enables hiding the command line invocation of `rustdoc` by default when it fails, although it's still available to see with `--verbose`. The intention here is to match the behavior of `cargo build` for rustc failures here and was inspired by recently running `cargo doc` and not being able to see the actual failure as the command line ended up taking the whole screen (afterwards I made the screen bigger and that helped too).
1 parent 4b22dcf commit 1439b3f

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,19 +376,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
376376
}
377377
}
378378

379-
fn verbose_if_simple_exit_code(err: Error) -> Error {
380-
// If a signal on unix (`code == None`) or an abnormal termination
381-
// on Windows (codes like `0xC0000409`), don't hide the error details.
382-
match err
383-
.downcast_ref::<ProcessError>()
384-
.as_ref()
385-
.and_then(|perr| perr.code)
386-
{
387-
Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(),
388-
_ => err,
389-
}
390-
}
391-
392379
state.running(&rustc);
393380
let timestamp = paths::set_invocation_time(&fingerprint_dir)?;
394381
if build_plan {
@@ -510,6 +497,19 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc<dyn Executor>) -> Car
510497
}
511498
}
512499

500+
fn verbose_if_simple_exit_code(err: Error) -> Error {
501+
// If a signal on unix (`code == None`) or an abnormal termination
502+
// on Windows (codes like `0xC0000409`), don't hide the error details.
503+
match err
504+
.downcast_ref::<ProcessError>()
505+
.as_ref()
506+
.and_then(|perr| perr.code)
507+
{
508+
Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(),
509+
_ => err,
510+
}
511+
}
512+
513513
/// Link the compiled target (often of form `foo-{metadata_hash}`) to the
514514
/// final target. This must happen during both "Fresh" and "Compile".
515515
fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResult<Work> {
@@ -862,6 +862,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Work> {
862862
},
863863
false,
864864
)
865+
.map_err(verbose_if_simple_exit_code)
865866
.with_context(|| format!("could not document `{}`", name));
866867

867868
if let Err(e) = result {

tests/testsuite/doc.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,3 +2640,24 @@ fn link_to_private_item() {
26402640
)
26412641
.run();
26422642
}
2643+
2644+
#[cargo_test]
2645+
fn rustdoc_failure_hides_command_line_by_default() {
2646+
let p = project().file("src/lib.rs", "invalid rust code").build();
2647+
2648+
let string_to_test = "\
2649+
Caused by:
2650+
process didn't exit successfully[..]rustdoc[..]";
2651+
2652+
// `cargo doc` doesn't print the full command line on failures by default
2653+
p.cargo("doc")
2654+
.with_stderr_does_not_contain(string_to_test)
2655+
.with_status(101)
2656+
.run();
2657+
2658+
// ... but it still does so if requested with `--verbose`.
2659+
p.cargo("doc --verbose")
2660+
.with_stderr_contains(string_to_test)
2661+
.with_status(101)
2662+
.run();
2663+
}

0 commit comments

Comments
 (0)