Skip to content

Commit

Permalink
Prefix build script output with crate name when running in extra verb…
Browse files Browse the repository at this point in the history
…ose mode. Fixes #6158.

cargo's extra verbose mode is useful for getting detailed information out of
builds in CI where it can be difficult to examine the build environment
after-the-fact. However, when multiple build scripts are running as part of a
build it's not always clear what output is from which build script. This patch
makes cargo prefix each line of build script output with the crate name and
version this case.
  • Loading branch information
luser committed Oct 18, 2018
1 parent cbde1c6 commit 20920c0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
} else {
state.running(&cmd);
let output = if extra_verbose {
state.capture_output(&cmd, true)
let prefix = format!("[{} {}] ", id.name(), id.version());
state.capture_output(&cmd, Some(prefix), true)
} else {
cmd.exec_with_output()
};
Expand Down
6 changes: 4 additions & 2 deletions src/cargo/core/compiler/job_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,17 @@ impl<'a> JobState<'a> {
pub fn capture_output(
&self,
cmd: &ProcessBuilder,
prefix: Option<String>,
print_output: bool,
) -> CargoResult<Output> {
let prefix = prefix.unwrap_or_else(|| String::new());
cmd.exec_with_streaming(
&mut |out| {
let _ = self.tx.send(Message::Stdout(out.to_string()));
let _ = self.tx.send(Message::Stdout(format!("{}{}", prefix, out)));
Ok(())
},
&mut |err| {
let _ = self.tx.send(Message::Stderr(err.to_string()));
let _ = self.tx.send(Message::Stderr(format!("{}{}", prefix, err)));
Ok(())
},
print_output,
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Executor for DefaultExecutor {
_mode: CompileMode,
state: &job_queue::JobState<'_>,
) -> CargoResult<()> {
state.capture_output(&cmd, false).map(drop)
state.capture_output(&cmd, None, false).map(drop)
}
}

Expand Down Expand Up @@ -645,7 +645,7 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
false,
).map(drop)
} else {
state.capture_output(&rustdoc, false).map(drop)
state.capture_output(&rustdoc, None, false).map(drop)
};
exec_result.chain_err(|| format!("Could not document `{}`.", name))?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2803,13 +2803,13 @@ fn output_shows_on_vv() {
).build();

p.cargo("build -vv")
.with_stdout("stdout")
.with_stdout("[foo 0.5.0] stdout")
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([..])
[RUNNING] `rustc [..]`
[RUNNING] `[..]`
stderr
[foo 0.5.0] stderr
[RUNNING] `rustc [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
Expand Down
30 changes: 15 additions & 15 deletions tests/testsuite/metabuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ fn metabuild_basic() {
let p = basic_project();
p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb")
.with_stdout_contains("Hello mb-other")
.with_stdout_contains("[foo 0.0.1] Hello mb")
.with_stdout_contains("[foo 0.0.1] Hello mb-other")
.run();
}

Expand Down Expand Up @@ -164,12 +164,12 @@ fn metabuild_optional_dep() {

p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_does_not_contain("Hello mb")
.with_stdout_does_not_contain("[foo 0.0.1] Hello mb")
.run();

p.cargo("build -vv --features mb")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb")
.with_stdout_contains("[foo 0.0.1] Hello mb")
.run();
}

Expand Down Expand Up @@ -206,7 +206,7 @@ fn metabuild_lib_name() {

p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb")
.with_stdout_contains("[foo 0.0.1] Hello mb")
.run();
}

Expand Down Expand Up @@ -235,12 +235,12 @@ fn metabuild_fresh() {

p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb")
.with_stdout_contains("[foo 0.0.1] Hello mb")
.run();

p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_does_not_contain("Hello mb")
.with_stdout_does_not_contain("[foo 0.0.1] Hello mb")
.with_stderr(
"\
[FRESH] mb [..]
Expand Down Expand Up @@ -279,7 +279,7 @@ fn metabuild_links() {

p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb")
.with_stdout_contains("[foo 0.0.1] Hello mb")
.run();
}

Expand Down Expand Up @@ -376,10 +376,10 @@ fn metabuild_workspace() {

p.cargo("build -vv --all")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb1 [..]member1")
.with_stdout_contains("Hello mb2 [..]member1")
.with_stdout_contains("Hello mb1 [..]member2")
.with_stdout_does_not_contain("Hello mb2 [..]member2")
.with_stdout_contains("[member1 0.0.1] Hello mb1 [..]member1")
.with_stdout_contains("[member1 0.0.1] Hello mb2 [..]member1")
.with_stdout_contains("[member2 0.0.1] Hello mb1 [..]member2")
.with_stdout_does_not_contain("[member2 0.0.1] Hello mb2 [..]member2")
.run();
}

Expand Down Expand Up @@ -572,8 +572,8 @@ fn metabuild_two_versions() {

p.cargo("build -vv --all")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb1 [..]member1")
.with_stdout_contains("Hello mb2 [..]member2")
.with_stdout_contains("[member1 0.0.1] Hello mb1 [..]member1")
.with_stdout_contains("[member2 0.0.1] Hello mb2 [..]member2")
.run();

assert_eq!(
Expand Down Expand Up @@ -628,7 +628,7 @@ fn metabuild_external_dependency() {

p.cargo("build -vv")
.masquerade_as_nightly_cargo()
.with_stdout_contains("Hello mb")
.with_stdout_contains("[dep 1.0.0] Hello mb")
.run();

assert_eq!(
Expand Down
25 changes: 12 additions & 13 deletions tests/testsuite/profile_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn profile_selection_build() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]
[FINISHED] dev [unoptimized + debuginfo] [..]
Expand Down Expand Up @@ -113,7 +113,7 @@ fn profile_selection_build_release() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `[..]/target/release/build/foo-[..]/build-script-build`
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C panic=abort -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/main.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C panic=abort -C codegen-units=2 [..]
[FINISHED] release [optimized] [..]
Expand Down Expand Up @@ -174,8 +174,8 @@ fn profile_selection_build_all_targets() {
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
foo custom build PROFILE=debug DEBUG=false OPT_LEVEL=3
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=false OPT_LEVEL=3
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]`
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]`
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]`
Expand Down Expand Up @@ -241,7 +241,7 @@ fn profile_selection_build_all_targets_release() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `[..]/target/release/build/foo-[..]/build-script-build`
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C panic=abort -C codegen-units=2 [..]`
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]`
Expand Down Expand Up @@ -296,7 +296,7 @@ fn profile_selection_test() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]
Expand Down Expand Up @@ -360,7 +360,7 @@ fn profile_selection_test_release() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `[..]/target/release/build/foo-[..]/build-script-build`
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C panic=abort -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]
Expand Down Expand Up @@ -424,7 +424,7 @@ fn profile_selection_bench() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `[..]target/release/build/foo-[..]/build-script-build`
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C panic=abort -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]
Expand Down Expand Up @@ -493,7 +493,7 @@ fn profile_selection_check_all_targets() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C panic=abort -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
Expand Down Expand Up @@ -543,7 +543,7 @@ fn profile_selection_check_all_targets_release() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `[..]target/release/build/foo-[..]/build-script-build`
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C opt-level=3 -C panic=abort -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C opt-level=3 -C codegen-units=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,metadata -C opt-level=3 -C codegen-units=2 --test [..]
Expand Down Expand Up @@ -607,14 +607,13 @@ fn profile_selection_check_all_targets_test() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
[RUNNING] `rustc --crate-name test1 tests/test1.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
[RUNNING] `rustc --crate-name bench1 benches/bench1.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
[RUNNING] `rustc --crate-name ex1 examples/ex1.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
[FINISHED] dev [unoptimized + debuginfo] [..]
").run();

p.cargo("check --all-targets --profile=test -vv")
Expand Down Expand Up @@ -653,7 +652,7 @@ fn profile_selection_doc() {
[COMPILING] foo [..]
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
[DOCUMENTING] foo [..]
[RUNNING] `rustdoc --crate-name foo src/lib.rs [..]
[FINISHED] dev [unoptimized + debuginfo] [..]
Expand Down

0 comments on commit 20920c0

Please sign in to comment.