From 1439b3fbe9d2b50ae9f393c06c054de577c97656 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 2 Feb 2024 07:38:28 -0800 Subject: [PATCH] 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). --- src/cargo/core/compiler/mod.rs | 27 ++++++++++++++------------- tests/testsuite/doc.rs | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs index 2a720b7665f..19a77173e92 100644 --- a/src/cargo/core/compiler/mod.rs +++ b/src/cargo/core/compiler/mod.rs @@ -376,19 +376,6 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc) -> Car } } - fn verbose_if_simple_exit_code(err: Error) -> Error { - // If a signal on unix (`code == None`) or an abnormal termination - // on Windows (codes like `0xC0000409`), don't hide the error details. - match err - .downcast_ref::() - .as_ref() - .and_then(|perr| perr.code) - { - Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(), - _ => err, - } - } - state.running(&rustc); let timestamp = paths::set_invocation_time(&fingerprint_dir)?; if build_plan { @@ -510,6 +497,19 @@ fn rustc(cx: &mut Context<'_, '_>, unit: &Unit, exec: &Arc) -> Car } } +fn verbose_if_simple_exit_code(err: Error) -> Error { + // If a signal on unix (`code == None`) or an abnormal termination + // on Windows (codes like `0xC0000409`), don't hide the error details. + match err + .downcast_ref::() + .as_ref() + .and_then(|perr| perr.code) + { + Some(n) if cargo_util::is_simple_exit_code(n) => VerboseError::new(err).into(), + _ => err, + } +} + /// Link the compiled target (often of form `foo-{metadata_hash}`) to the /// final target. This must happen during both "Fresh" and "Compile". fn link_targets(cx: &mut Context<'_, '_>, unit: &Unit, fresh: bool) -> CargoResult { @@ -862,6 +862,7 @@ fn rustdoc(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { }, false, ) + .map_err(verbose_if_simple_exit_code) .with_context(|| format!("could not document `{}`", name)); if let Err(e) = result { diff --git a/tests/testsuite/doc.rs b/tests/testsuite/doc.rs index 0358c379088..919114291cb 100644 --- a/tests/testsuite/doc.rs +++ b/tests/testsuite/doc.rs @@ -2640,3 +2640,24 @@ fn link_to_private_item() { ) .run(); } + +#[cargo_test] +fn rustdoc_failure_hides_command_line_by_default() { + let p = project().file("src/lib.rs", "invalid rust code").build(); + + let string_to_test = "\ +Caused by: + process didn't exit successfully[..]rustdoc[..]"; + + // `cargo doc` doesn't print the full command line on failures by default + p.cargo("doc") + .with_stderr_does_not_contain(string_to_test) + .with_status(101) + .run(); + + // ... but it still does so if requested with `--verbose`. + p.cargo("doc --verbose") + .with_stderr_contains(string_to_test) + .with_status(101) + .run(); +}