From 240a6b39888bf13a122f4b90abe9ae39ec106498 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:23:09 +1000 Subject: [PATCH] output the env var being set as part of the command output --- Cargo.lock | 7 ++++ cmd/soroban-cli/Cargo.toml | 2 +- .../src/commands/contract/build.rs | 37 +++++++++++++------ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 55ebf62a2..e5c37b671 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4281,6 +4281,12 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shell-escape" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45bb67a18fa91266cc7807181f62f9178a6873bfad7dc788c42e6430db40184f" + [[package]] name = "shlex" version = "1.3.0" @@ -4479,6 +4485,7 @@ dependencies = [ "serde-aux", "serde_json", "sha2 0.10.8", + "shell-escape", "shlex", "soroban-ledger-snapshot 22.0.0-rc.1.1", "soroban-sdk 22.0.0-rc.1.1", diff --git a/cmd/soroban-cli/Cargo.toml b/cmd/soroban-cli/Cargo.toml index 0cc12e74f..00b8df302 100644 --- a/cmd/soroban-cli/Cargo.toml +++ b/cmd/soroban-cli/Cargo.toml @@ -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"] } diff --git a/cmd/soroban-cli/src/commands/contract/build.rs b/cmd/soroban-cli/src/commands/contract/build.rs index 43ff6fa6c..26da7cf66 100644 --- a/cmd/soroban-cli/src/commands/contract/build.rs +++ b/cmd/soroban-cli/src/commands/contract/build.rs @@ -1,6 +1,7 @@ use clap::Parser; use itertools::Itertools; use std::{ + borrow::Cow, collections::HashSet, env, ffi::OsStr, @@ -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::::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}"); @@ -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, Error> { let cargo_home = home::cargo_home().map_err(Error::CargoHome)?; let cargo_home = format!("{}", cargo_home.display()); @@ -289,7 +305,7 @@ 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() { @@ -297,7 +313,7 @@ fn set_env_to_remap_absolute_paths(cmd: &mut Command) -> Result<(), Error> { "⚠ 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() { @@ -305,7 +321,7 @@ fn set_env_to_remap_absolute_paths(cmd: &mut Command) -> Result<(), Error> { "⚠ 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() { @@ -313,7 +329,7 @@ fn set_env_to_remap_absolute_paths(cmd: &mut Command) -> Result<(), Error> { "⚠ 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/"); @@ -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.