Skip to content

Commit

Permalink
Auto merge of #6492 - mikerite:print-env-vars-4, r=alexcrichton
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jan 3, 2019
2 parents d4a04da + e557f66 commit 34320d2
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 114 deletions.
3 changes: 3 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ impl<'cfg> Compilation<'cfg> {
} else {
bcx.rustc.process()
};
if bcx.config.extra_verbose() {
rustc.display_env_vars();
}
for (k, v) in bcx.build_config.extra_rustc_env.iter() {
rustc.env(k, v);
}
Expand Down
26 changes: 25 additions & 1 deletion src/cargo/util/process_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,28 @@ pub struct ProcessBuilder {
///
/// [jobserver_docs]: https://docs.rs/jobserver/0.1.6/jobserver/
jobserver: Option<Client>,
/// Whether to include environment variable in display
display_env_vars: bool
}

impl fmt::Display for ProcessBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "`{}", self.program.to_string_lossy())?;
write!(f, "`")?;

if self.display_env_vars {
for (key, val) in self.env.iter() {
if let Some(val) = val {
let val = escape(val.to_string_lossy());
if cfg!(windows) {
write!(f, "set {}={}&& ", key, val)?;
} else {
write!(f, "{}={} ", key, val)?;
}
}
}
}

write!(f, "{}", self.program.to_string_lossy())?;

for arg in &self.args {
write!(f, " {}", escape(arg.to_string_lossy()))?;
Expand Down Expand Up @@ -129,6 +146,12 @@ impl ProcessBuilder {
self
}

/// Enable environment variable display
pub fn display_env_vars(&mut self) -> &mut Self {
self.display_env_vars = true;
self
}

/// Run the process, waiting for completion, and mapping non-success exit codes to an error.
pub fn exec(&self) -> CargoResult<()> {
let mut command = self.build_command();
Expand Down Expand Up @@ -316,6 +339,7 @@ pub fn process<T: AsRef<OsStr>>(cmd: T) -> ProcessBuilder {
cwd: None,
env: HashMap::new(),
jobserver: None,
display_env_vars: false,
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,40 @@ fn crate_authors_env_vars() {
p.cargo("test -v").run();
}

#[test]
fn vv_prints_rustc_env_vars() {
let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = ["escape='\"@example.com"]
"#,
)
.file("src/main.rs", "fn main() {}")
.build();

let mut b = p.cargo("build -vv");

if cfg!(windows) {
b.with_stderr_contains(
"[RUNNING] `[..]set CARGO_PKG_NAME=foo&& [..]rustc [..]`"
).with_stderr_contains(
r#"[RUNNING] `[..]set CARGO_PKG_AUTHORS="escape='\"@example.com"&& [..]rustc [..]`"#
)
} else {
b.with_stderr_contains(
"[RUNNING] `[..]CARGO_PKG_NAME=foo [..]rustc [..]`"
).with_stderr_contains(
r#"[RUNNING] `[..]CARGO_PKG_AUTHORS='escape='\''"@example.com' [..]rustc [..]`"#
)
};

b.run();
}

// The tester may already have LD_LIBRARY_PATH=::/foo/bar which leads to a false positive error
fn setenv_for_removing_empty_component(mut execs: Execs) -> Execs {
let v = dylib_path_envvar();
Expand Down
10 changes: 5 additions & 5 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2965,13 +2965,13 @@ fn warnings_printed_on_vv() {
[DOWNLOADING] crates ...
[DOWNLOADED] bar v0.1.0 ([..])
[COMPILING] bar v0.1.0
[RUNNING] `rustc [..]`
[RUNNING] `[..] rustc [..]`
[RUNNING] `[..]`
warning: foo
warning: bar
[RUNNING] `rustc [..]`
[RUNNING] `[..] rustc [..]`
[COMPILING] foo v0.5.0 ([..])
[RUNNING] `rustc [..]`
[RUNNING] `[..] rustc [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
Expand Down Expand Up @@ -3010,10 +3010,10 @@ fn output_shows_on_vv() {
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([..])
[RUNNING] `rustc [..]`
[RUNNING] `[..] rustc [..]`
[RUNNING] `[..]`
[foo 0.5.0] stderr
[RUNNING] `rustc [..]`
[RUNNING] `[..] rustc [..]`
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
",
)
Expand Down
Loading

0 comments on commit 34320d2

Please sign in to comment.