Skip to content

Display environment variables for rustc commands #6492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1553,6 +1553,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