Skip to content
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

fix: remove from env CARGO_ENCODED_RUSTFLAGS for easier nested builds, simplify RUSTFLAGS computation rule #289

Merged
merged 6 commits into from
Jan 22, 2025
42 changes: 17 additions & 25 deletions cargo-near-build/src/cargo_native/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,32 @@ use super::ArtifactType;
pub fn run<T>(
manifest_path: &ManifestPath,
args: &[&str],
mut env: Vec<(&str, &str)>,
env: Vec<(&str, &str)>,
hide_warnings: bool,
color: ColorPreference,
) -> eyre::Result<CompilationArtifact<T>>
where
T: ArtifactType,
{
let mut final_env = BTreeMap::new();

if hide_warnings {
env.push(("RUSTFLAGS", "-Awarnings"));
}

for (key, value) in env {
match key {
"RUSTFLAGS" => {
let rustflags: &mut String = final_env
.entry(key)
.or_insert_with(|| std::env::var(key).unwrap_or_default());
// helps avoids situation on complete match `RUSTFLAGS="-C link-arg=-s -C link-arg=-s"`
if !rustflags.contains(value) {
if !rustflags.is_empty() {
rustflags.push(' ');
}
rustflags.push_str(value);
}
}
_ => {
final_env.insert(key, value.to_string());
}
let final_env = {
let mut env: BTreeMap<_, _> = env.into_iter().collect();
if hide_warnings {
env.insert("RUSTFLAGS", "-Awarnings");
}
}
env
};
// removing env, which may be implicitly passed when bulding from within a build-script
// see https://github.com/near/cargo-near/issues/287
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
// CARGO_ENCODED_RUSTFLAGS="-Awarnings" and RUSTFLAGS="-Awarnings" both result
// in mysterious failures of `cargo build --target wasm32-unknown-unknown` (**cargo** bug)
dj8yfo marked this conversation as resolved.
Show resolved Hide resolved
let removed_env = ["CARGO_ENCODED_RUSTFLAGS"];

let artifacts = invoke_cargo(
"build",
[&["--message-format=json-render-diagnostics"], args].concat(),
manifest_path.directory().ok(),
final_env.iter(),
&removed_env,
color,
)?;

Expand Down Expand Up @@ -105,6 +93,7 @@ fn invoke_cargo<A, P, E, S, EK, EV>(
args: A,
working_dir: Option<P>,
env: E,
removed_env: &[&str],
color: ColorPreference,
) -> eyre::Result<Vec<Artifact>>
where
Expand All @@ -118,6 +107,9 @@ where
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut cmd = Command::new(cargo);

for key in removed_env {
cmd.env_remove(key);
}
cmd.envs(env);

if let Some(path) = working_dir {
Expand Down
Loading