Skip to content

Commit 59c7c06

Browse files
committed
fix: support relative TARGET_DIR in near-test-contracts
near-test-contracts builds some wasm contracts for use in testing. It does so by recursively invoking `cargo` from `build.rs`. Before this commit, we tried to re-use parent's `CARGO_TARGET_DIR` to figure out where we should put the data. That was rather hacky, as cargo doesn't expose that information to the build scripts in a reliable way: rust-lang/cargo#9661 (comment) Naturally, our hacks broken when when the `CARGO_TARGET_DIR` was set to a relative path, because build.rs doesn't know where workspace root lives. The fix is to use `OUT_DIR` rather than `CARGO_TARGET_DIR`, which I think is the supported way to this in the first place. Eg, the `cc` crate uses `OUT_DIR` to store intermediate `.o` files, which I think matches our use case pretty closely.
1 parent eff8a83 commit 59c7c06

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

runtime/near-test-contracts/build.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ fn try_main() -> Result<(), Error> {
2929
}
3030

3131
fn build_contract(dir: &str, args: &[&str], output: &str) -> Result<(), Error> {
32-
let mut cmd = cargo_build_cmd();
32+
let target_dir = out_dir();
33+
34+
let mut cmd = cargo_build_cmd(&target_dir);
3335
cmd.args(args);
3436
cmd.current_dir(dir);
3537
check_status(cmd)?;
3638

37-
let target_dir = shared_target_dir().unwrap_or_else(|| format!("./{}/target", dir).into());
3839
let src =
3940
target_dir.join(format!("wasm32-unknown-unknown/release/{}.wasm", dir.replace('-', "_")));
4041
fs::copy(&src, format!("./res/{}.wasm", output))
@@ -44,14 +45,11 @@ fn build_contract(dir: &str, args: &[&str], output: &str) -> Result<(), Error> {
4445
Ok(())
4546
}
4647

47-
fn cargo_build_cmd() -> Command {
48+
fn cargo_build_cmd(target_dir: &Path) -> Command {
4849
let mut res = Command::new("cargo");
4950
res.env("RUSTFLAGS", "-C link-arg=-s");
5051
res.env_remove("CARGO_ENCODED_RUSTFLAGS");
51-
if let Some(target_dir) = shared_target_dir() {
52-
res.env("CARGO_TARGET_DIR", target_dir);
53-
}
54-
52+
res.env("CARGO_TARGET_DIR", target_dir);
5553
res.args(&["build", "--target=wasm32-unknown-unknown", "--release"]);
5654
res
5755
}
@@ -66,13 +64,6 @@ fn check_status(mut cmd: Command) -> Result<(), Error> {
6664
Ok(())
6765
}
6866

69-
fn shared_target_dir() -> Option<PathBuf> {
70-
let target_dir = env::var("CARGO_TARGET_DIR").ok()?;
71-
// Avoid sharing the same target directory with the patent Cargo
72-
// invocation, to avoid deadlock on the target dir.
73-
//
74-
// That is, this logic is needed for the case like the following:
75-
//
76-
// CARGO_TARGET_DIR=/tmp cargo build -p near-test-contracts --release
77-
Some(Path::new(&target_dir).join("near-test-contracts"))
67+
fn out_dir() -> PathBuf {
68+
env::var("OUT_DIR").unwrap().into()
7869
}

0 commit comments

Comments
 (0)