Skip to content

Commit 20920c0

Browse files
committed
Prefix build script output with crate name when running in extra verbose 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.
1 parent cbde1c6 commit 20920c0

File tree

6 files changed

+37
-35
lines changed

6 files changed

+37
-35
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
326326
} else {
327327
state.running(&cmd);
328328
let output = if extra_verbose {
329-
state.capture_output(&cmd, true)
329+
let prefix = format!("[{} {}] ", id.name(), id.version());
330+
state.capture_output(&cmd, Some(prefix), true)
330331
} else {
331332
cmd.exec_with_output()
332333
};

src/cargo/core/compiler/job_queue.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@ impl<'a> JobState<'a> {
112112
pub fn capture_output(
113113
&self,
114114
cmd: &ProcessBuilder,
115+
prefix: Option<String>,
115116
print_output: bool,
116117
) -> CargoResult<Output> {
118+
let prefix = prefix.unwrap_or_else(|| String::new());
117119
cmd.exec_with_streaming(
118120
&mut |out| {
119-
let _ = self.tx.send(Message::Stdout(out.to_string()));
121+
let _ = self.tx.send(Message::Stdout(format!("{}{}", prefix, out)));
120122
Ok(())
121123
},
122124
&mut |err| {
123-
let _ = self.tx.send(Message::Stderr(err.to_string()));
125+
let _ = self.tx.send(Message::Stderr(format!("{}{}", prefix, err)));
124126
Ok(())
125127
},
126128
print_output,

src/cargo/core/compiler/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Executor for DefaultExecutor {
118118
_mode: CompileMode,
119119
state: &job_queue::JobState<'_>,
120120
) -> CargoResult<()> {
121-
state.capture_output(&cmd, false).map(drop)
121+
state.capture_output(&cmd, None, false).map(drop)
122122
}
123123
}
124124

@@ -645,7 +645,7 @@ fn rustdoc<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult
645645
false,
646646
).map(drop)
647647
} else {
648-
state.capture_output(&rustdoc, false).map(drop)
648+
state.capture_output(&rustdoc, None, false).map(drop)
649649
};
650650
exec_result.chain_err(|| format!("Could not document `{}`.", name))?;
651651
Ok(())

tests/testsuite/build_script.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,13 +2803,13 @@ fn output_shows_on_vv() {
28032803
).build();
28042804

28052805
p.cargo("build -vv")
2806-
.with_stdout("stdout")
2806+
.with_stdout("[foo 0.5.0] stdout")
28072807
.with_stderr(
28082808
"\
28092809
[COMPILING] foo v0.5.0 ([..])
28102810
[RUNNING] `rustc [..]`
28112811
[RUNNING] `[..]`
2812-
stderr
2812+
[foo 0.5.0] stderr
28132813
[RUNNING] `rustc [..]`
28142814
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
28152815
",

tests/testsuite/metabuild.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ fn metabuild_basic() {
7272
let p = basic_project();
7373
p.cargo("build -vv")
7474
.masquerade_as_nightly_cargo()
75-
.with_stdout_contains("Hello mb")
76-
.with_stdout_contains("Hello mb-other")
75+
.with_stdout_contains("[foo 0.0.1] Hello mb")
76+
.with_stdout_contains("[foo 0.0.1] Hello mb-other")
7777
.run();
7878
}
7979

@@ -164,12 +164,12 @@ fn metabuild_optional_dep() {
164164

165165
p.cargo("build -vv")
166166
.masquerade_as_nightly_cargo()
167-
.with_stdout_does_not_contain("Hello mb")
167+
.with_stdout_does_not_contain("[foo 0.0.1] Hello mb")
168168
.run();
169169

170170
p.cargo("build -vv --features mb")
171171
.masquerade_as_nightly_cargo()
172-
.with_stdout_contains("Hello mb")
172+
.with_stdout_contains("[foo 0.0.1] Hello mb")
173173
.run();
174174
}
175175

@@ -206,7 +206,7 @@ fn metabuild_lib_name() {
206206

207207
p.cargo("build -vv")
208208
.masquerade_as_nightly_cargo()
209-
.with_stdout_contains("Hello mb")
209+
.with_stdout_contains("[foo 0.0.1] Hello mb")
210210
.run();
211211
}
212212

@@ -235,12 +235,12 @@ fn metabuild_fresh() {
235235

236236
p.cargo("build -vv")
237237
.masquerade_as_nightly_cargo()
238-
.with_stdout_contains("Hello mb")
238+
.with_stdout_contains("[foo 0.0.1] Hello mb")
239239
.run();
240240

241241
p.cargo("build -vv")
242242
.masquerade_as_nightly_cargo()
243-
.with_stdout_does_not_contain("Hello mb")
243+
.with_stdout_does_not_contain("[foo 0.0.1] Hello mb")
244244
.with_stderr(
245245
"\
246246
[FRESH] mb [..]
@@ -279,7 +279,7 @@ fn metabuild_links() {
279279

280280
p.cargo("build -vv")
281281
.masquerade_as_nightly_cargo()
282-
.with_stdout_contains("Hello mb")
282+
.with_stdout_contains("[foo 0.0.1] Hello mb")
283283
.run();
284284
}
285285

@@ -376,10 +376,10 @@ fn metabuild_workspace() {
376376

377377
p.cargo("build -vv --all")
378378
.masquerade_as_nightly_cargo()
379-
.with_stdout_contains("Hello mb1 [..]member1")
380-
.with_stdout_contains("Hello mb2 [..]member1")
381-
.with_stdout_contains("Hello mb1 [..]member2")
382-
.with_stdout_does_not_contain("Hello mb2 [..]member2")
379+
.with_stdout_contains("[member1 0.0.1] Hello mb1 [..]member1")
380+
.with_stdout_contains("[member1 0.0.1] Hello mb2 [..]member1")
381+
.with_stdout_contains("[member2 0.0.1] Hello mb1 [..]member2")
382+
.with_stdout_does_not_contain("[member2 0.0.1] Hello mb2 [..]member2")
383383
.run();
384384
}
385385

@@ -572,8 +572,8 @@ fn metabuild_two_versions() {
572572

573573
p.cargo("build -vv --all")
574574
.masquerade_as_nightly_cargo()
575-
.with_stdout_contains("Hello mb1 [..]member1")
576-
.with_stdout_contains("Hello mb2 [..]member2")
575+
.with_stdout_contains("[member1 0.0.1] Hello mb1 [..]member1")
576+
.with_stdout_contains("[member2 0.0.1] Hello mb2 [..]member2")
577577
.run();
578578

579579
assert_eq!(
@@ -628,7 +628,7 @@ fn metabuild_external_dependency() {
628628

629629
p.cargo("build -vv")
630630
.masquerade_as_nightly_cargo()
631-
.with_stdout_contains("Hello mb")
631+
.with_stdout_contains("[dep 1.0.0] Hello mb")
632632
.run();
633633

634634
assert_eq!(

tests/testsuite/profile_targets.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn profile_selection_build() {
8383
[COMPILING] foo [..]
8484
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
8585
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
86-
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
86+
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
8787
[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 [..]
8888
[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 [..]
8989
[FINISHED] dev [unoptimized + debuginfo] [..]
@@ -113,7 +113,7 @@ fn profile_selection_build_release() {
113113
[COMPILING] foo [..]
114114
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
115115
[RUNNING] `[..]/target/release/build/foo-[..]/build-script-build`
116-
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
116+
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
117117
[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 [..]
118118
[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 [..]
119119
[FINISHED] release [optimized] [..]
@@ -174,8 +174,8 @@ fn profile_selection_build_all_targets() {
174174
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
175175
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
176176
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
177-
foo custom build PROFILE=debug DEBUG=false OPT_LEVEL=3
178-
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
177+
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=false OPT_LEVEL=3
178+
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
179179
[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 [..]`
180180
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]`
181181
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]`
@@ -241,7 +241,7 @@ fn profile_selection_build_all_targets_release() {
241241
[COMPILING] foo [..]
242242
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
243243
[RUNNING] `[..]/target/release/build/foo-[..]/build-script-build`
244-
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
244+
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
245245
[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 [..]`
246246
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]`
247247
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]`
@@ -296,7 +296,7 @@ fn profile_selection_test() {
296296
[COMPILING] foo [..]
297297
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
298298
[RUNNING] `[..]/target/debug/build/foo-[..]/build-script-build`
299-
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
299+
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
300300
[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 [..]
301301
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
302302
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C codegen-units=3 -C debuginfo=2 --test [..]
@@ -360,7 +360,7 @@ fn profile_selection_test_release() {
360360
[COMPILING] foo [..]
361361
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
362362
[RUNNING] `[..]/target/release/build/foo-[..]/build-script-build`
363-
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
363+
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
364364
[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 [..]
365365
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
366366
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]
@@ -424,7 +424,7 @@ fn profile_selection_bench() {
424424
[COMPILING] foo [..]
425425
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
426426
[RUNNING] `[..]target/release/build/foo-[..]/build-script-build`
427-
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
427+
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
428428
[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 [..]
429429
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
430430
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,link -C opt-level=3 -C codegen-units=4 --test [..]
@@ -493,7 +493,7 @@ fn profile_selection_check_all_targets() {
493493
[COMPILING] foo [..]
494494
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
495495
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
496-
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
496+
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
497497
[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 [..]
498498
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 [..]
499499
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
@@ -543,7 +543,7 @@ fn profile_selection_check_all_targets_release() {
543543
[COMPILING] foo [..]
544544
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C opt-level=3 -C codegen-units=2 [..]
545545
[RUNNING] `[..]target/release/build/foo-[..]/build-script-build`
546-
foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
546+
[foo 0.0.1] foo custom build PROFILE=release DEBUG=false OPT_LEVEL=3
547547
[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 [..]
548548
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C opt-level=3 -C codegen-units=2 [..]
549549
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,metadata -C opt-level=3 -C codegen-units=2 --test [..]
@@ -607,14 +607,13 @@ fn profile_selection_check_all_targets_test() {
607607
[COMPILING] foo [..]
608608
[RUNNING] `rustc --crate-name build_script_build build.rs [..]--crate-type bin --emit=dep-info,link -C codegen-units=1 -C debuginfo=2 [..]
609609
[RUNNING] `[..]target/debug/build/foo-[..]/build-script-build`
610-
foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
610+
[foo 0.0.1] foo custom build PROFILE=debug DEBUG=true OPT_LEVEL=0
611611
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--crate-type lib --emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 [..]
612612
[RUNNING] `rustc --crate-name foo src/lib.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
613613
[RUNNING] `rustc --crate-name test1 tests/test1.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
614614
[RUNNING] `rustc --crate-name foo src/main.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
615615
[RUNNING] `rustc --crate-name bench1 benches/bench1.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
616616
[RUNNING] `rustc --crate-name ex1 examples/ex1.rs [..]--emit=dep-info,metadata -C codegen-units=1 -C debuginfo=2 --test [..]
617-
[FINISHED] dev [unoptimized + debuginfo] [..]
618617
").run();
619618

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

0 commit comments

Comments
 (0)