Skip to content

Commit

Permalink
output the env var being set as part of the command output
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Oct 18, 2024
1 parent 6719807 commit 240a6b3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/soroban-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ ulid = { workspace = true, features = ["serde"] }
strum = "0.17.1"
strum_macros = "0.17.1"
async-compression = { version = "0.4.12", features = ["tokio", "gzip"] }

shell-escape = "0.1.5"
tempfile = "3.8.1"
toml_edit = "0.21.0"
rust-embed = { version = "8.2.0", features = ["debug-embed"] }
Expand Down
37 changes: 26 additions & 11 deletions cmd/soroban-cli/src/commands/contract/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use itertools::Itertools;
use std::{
borrow::Cow,
collections::HashSet,
env,
ffi::OsStr,
Expand Down Expand Up @@ -133,11 +134,26 @@ impl Cmd {
cmd.arg(format!("--features={activate}"));
}
}
set_env_to_remap_absolute_paths(&mut cmd)?;
let cmd_str = format!(
"cargo {}",
cmd.get_args().map(OsStr::to_string_lossy).join(" ")

if let Some(rustflags) = make_rustflags_to_remap_absolute_paths()? {
cmd.env("CARGO_BUILD_RUSTFLAGS", rustflags);
}

let mut cmd_str_parts = Vec::<String>::new();
cmd_str_parts.extend(cmd.get_envs().map(|(key, val)| {
format!(
"{}={}",
key.to_string_lossy(),
shell_escape::escape(val.unwrap_or_default().to_string_lossy())
)
}));
cmd_str_parts.push("cargo".to_string());
cmd_str_parts.extend(
cmd.get_args()
.map(OsStr::to_string_lossy)
.map(Cow::into_owned),
);
let cmd_str = cmd_str_parts.join(" ");

if self.print_commands_only {
println!("{cmd_str}");
Expand Down Expand Up @@ -280,7 +296,7 @@ impl Cmd {
/// the absolute path replacement. Non-Unicode `CARGO_BUILD_RUSTFLAGS` will result in the
/// existing rustflags being ignored, which is also the behavior of
/// Cargo itself.
fn set_env_to_remap_absolute_paths(cmd: &mut Command) -> Result<(), Error> {
fn make_rustflags_to_remap_absolute_paths() -> Result<Option<String>, Error> {
let cargo_home = home::cargo_home().map_err(Error::CargoHome)?;
let cargo_home = format!("{}", cargo_home.display());

Expand All @@ -289,31 +305,31 @@ fn set_env_to_remap_absolute_paths(cmd: &mut Command) -> Result<(), Error> {
"⚠ Warning: Cargo home directory contains whitespace. \
Dependency paths will not be remapped; builds may not be reproducible."
);
return Ok(());
return Ok(None);
}

if env::var("RUSTFLAGS").is_ok() {
eprintln!(
"⚠ Warning: `RUSTFLAGS` set. \
Dependency paths will not be remapped; builds may not be reproducible."
);
return Ok(());
return Ok(None);
}

if env::var("CARGO_ENCODED_RUSTFLAGS").is_ok() {
eprintln!(
"⚠ Warning: `CARGO_ENCODED_RUSTFLAGS` set. \
Dependency paths will not be remapped; builds may not be reproducible."
);
return Ok(());
return Ok(None);
}

if env::var("TARGET_wasm32-unknown-unknown_RUSTFLAGS").is_ok() {
eprintln!(
"⚠ Warning: `TARGET_wasm32-unknown-unknown_RUSTFLAGS` set. \
Dependency paths will not be remapped; builds may not be reproducible."
);
return Ok(());
return Ok(None);
}

let registry_prefix = format!("{cargo_home}/registry/src/");
Expand All @@ -323,9 +339,8 @@ fn set_env_to_remap_absolute_paths(cmd: &mut Command) -> Result<(), Error> {
rustflags.push(new_rustflag);

let rustflags = rustflags.join(" ");
cmd.env("CARGO_BUILD_RUSTFLAGS", rustflags);

Ok(())
Ok(Some(rustflags))
}

/// Get any existing `CARGO_BUILD_RUSTFLAGS`, split on whitespace.
Expand Down

0 comments on commit 240a6b3

Please sign in to comment.