Skip to content

Commit 34320d2

Browse files
committed
Auto merge of #6492 - mikerite:print-env-vars-4, r=alexcrichton
Display environment variables for rustc commands This picks up on the work done in PR #5683. The extra output is only displayed with `-vv`. The Windows output has the form `set FOO=foo && BAR=bar rustc ...` instead of the form that suggested in #5683 to make escaping easier and since it's simpler.
2 parents d4a04da + e557f66 commit 34320d2

File tree

5 files changed

+175
-114
lines changed

5 files changed

+175
-114
lines changed

src/cargo/core/compiler/compilation.rs

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ impl<'cfg> Compilation<'cfg> {
8989
} else {
9090
bcx.rustc.process()
9191
};
92+
if bcx.config.extra_verbose() {
93+
rustc.display_env_vars();
94+
}
9295
for (k, v) in bcx.build_config.extra_rustc_env.iter() {
9396
rustc.env(k, v);
9497
}

src/cargo/util/process_builder.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,28 @@ pub struct ProcessBuilder {
2727
///
2828
/// [jobserver_docs]: https://docs.rs/jobserver/0.1.6/jobserver/
2929
jobserver: Option<Client>,
30+
/// Whether to include environment variable in display
31+
display_env_vars: bool
3032
}
3133

3234
impl fmt::Display for ProcessBuilder {
3335
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34-
write!(f, "`{}", self.program.to_string_lossy())?;
36+
write!(f, "`")?;
37+
38+
if self.display_env_vars {
39+
for (key, val) in self.env.iter() {
40+
if let Some(val) = val {
41+
let val = escape(val.to_string_lossy());
42+
if cfg!(windows) {
43+
write!(f, "set {}={}&& ", key, val)?;
44+
} else {
45+
write!(f, "{}={} ", key, val)?;
46+
}
47+
}
48+
}
49+
}
50+
51+
write!(f, "{}", self.program.to_string_lossy())?;
3552

3653
for arg in &self.args {
3754
write!(f, " {}", escape(arg.to_string_lossy()))?;
@@ -129,6 +146,12 @@ impl ProcessBuilder {
129146
self
130147
}
131148

149+
/// Enable environment variable display
150+
pub fn display_env_vars(&mut self) -> &mut Self {
151+
self.display_env_vars = true;
152+
self
153+
}
154+
132155
/// Run the process, waiting for completion, and mapping non-success exit codes to an error.
133156
pub fn exec(&self) -> CargoResult<()> {
134157
let mut command = self.build_command();
@@ -316,6 +339,7 @@ pub fn process<T: AsRef<OsStr>>(cmd: T) -> ProcessBuilder {
316339
cwd: None,
317340
env: HashMap::new(),
318341
jobserver: None,
342+
display_env_vars: false,
319343
}
320344
}
321345

tests/testsuite/build.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,40 @@ fn crate_authors_env_vars() {
15601560
p.cargo("test -v").run();
15611561
}
15621562

1563+
#[test]
1564+
fn vv_prints_rustc_env_vars() {
1565+
let p = project()
1566+
.file(
1567+
"Cargo.toml",
1568+
r#"
1569+
[project]
1570+
name = "foo"
1571+
version = "0.0.1"
1572+
authors = ["escape='\"@example.com"]
1573+
"#,
1574+
)
1575+
.file("src/main.rs", "fn main() {}")
1576+
.build();
1577+
1578+
let mut b = p.cargo("build -vv");
1579+
1580+
if cfg!(windows) {
1581+
b.with_stderr_contains(
1582+
"[RUNNING] `[..]set CARGO_PKG_NAME=foo&& [..]rustc [..]`"
1583+
).with_stderr_contains(
1584+
r#"[RUNNING] `[..]set CARGO_PKG_AUTHORS="escape='\"@example.com"&& [..]rustc [..]`"#
1585+
)
1586+
} else {
1587+
b.with_stderr_contains(
1588+
"[RUNNING] `[..]CARGO_PKG_NAME=foo [..]rustc [..]`"
1589+
).with_stderr_contains(
1590+
r#"[RUNNING] `[..]CARGO_PKG_AUTHORS='escape='\''"@example.com' [..]rustc [..]`"#
1591+
)
1592+
};
1593+
1594+
b.run();
1595+
}
1596+
15631597
// The tester may already have LD_LIBRARY_PATH=::/foo/bar which leads to a false positive error
15641598
fn setenv_for_removing_empty_component(mut execs: Execs) -> Execs {
15651599
let v = dylib_path_envvar();

tests/testsuite/build_script.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2965,13 +2965,13 @@ fn warnings_printed_on_vv() {
29652965
[DOWNLOADING] crates ...
29662966
[DOWNLOADED] bar v0.1.0 ([..])
29672967
[COMPILING] bar v0.1.0
2968-
[RUNNING] `rustc [..]`
2968+
[RUNNING] `[..] rustc [..]`
29692969
[RUNNING] `[..]`
29702970
warning: foo
29712971
warning: bar
2972-
[RUNNING] `rustc [..]`
2972+
[RUNNING] `[..] rustc [..]`
29732973
[COMPILING] foo v0.5.0 ([..])
2974-
[RUNNING] `rustc [..]`
2974+
[RUNNING] `[..] rustc [..]`
29752975
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
29762976
",
29772977
)
@@ -3010,10 +3010,10 @@ fn output_shows_on_vv() {
30103010
.with_stderr(
30113011
"\
30123012
[COMPILING] foo v0.5.0 ([..])
3013-
[RUNNING] `rustc [..]`
3013+
[RUNNING] `[..] rustc [..]`
30143014
[RUNNING] `[..]`
30153015
[foo 0.5.0] stderr
3016-
[RUNNING] `rustc [..]`
3016+
[RUNNING] `[..] rustc [..]`
30173017
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
30183018
",
30193019
)

0 commit comments

Comments
 (0)