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
25 changes: 8 additions & 17 deletions cargo-near-build/src/cargo_native/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,14 @@ where
{
let mut final_env = BTreeMap::new();

// this will overwrite any other RUSTFLAGS specified
if hide_warnings {
env.push(("RUSTFLAGS", "-Awarnings"));
}

// last instance of a key gets inserted
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());
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a simplification to avoid concatenating with value from env.
It will be simply -Awarnings for abi builds and -C link-arg=-s for wasm builds

final_env.insert(key, value.to_string());
akorchyn marked this conversation as resolved.
Show resolved Hide resolved
}

let artifacts = invoke_cargo(
Expand Down Expand Up @@ -118,6 +104,11 @@ where
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut cmd = Command::new(cargo);

// removing env, which may be implicitly passed when bulding from within a build-script
// see https://github.com/near/cargo-near/issues/287
// CARGO_ENCODED_RUSTFLAGS="-Awarnings" and RUSTFLAGS="-Awarnings" both result
// in mysterious failures of `cargo build --target wasm32-unknown-unknown` (rustc bug)
cmd.env_remove("CARGO_ENCODED_RUSTFLAGS");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says that both CARGO_ENCODED_RUSTFLAGS and RUSTFLAGS cause problems, but we only remove CARGO_ENCODED_RUSTFLAGS, is this intended?

Copy link
Collaborator Author

@dj8yfo dj8yfo Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUSTFLAGS gets reset to unset in build scripts, so the 2nd or 3rd of 4th build won't inherit this value from
previous 1st or 2nd or 3rd one,
and the 1st build isn't having its value concatenated with value from env as per another commented change,
and will have it defined as -Awarnings for abi builds and -C link-arg=-s for wasm builds

cmd.envs(env);

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