Skip to content

Commit 3e82084

Browse files
vmxemilio
authored andcommitted
Run cargo expand outside the normal target directory
When crates should get expanded, then cargo is run again from within cbindgen. When cbindgen is started from a build.rs file, this means that cargo is starting another cargo run. Cargo locks the directory it is running on. If two cargos spawn by each other run with the same target directory, then they both want to accquire a lock and hence deadlock. This commit fixes the problem with running the spawned cargo at a different directory. Please note that this will always be the same directory, hence any subsequent runs will be faster (as you would exepct it to be). This is part of mozilla#347.
1 parent b4b82a5 commit 3e82084

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/bindgen/cargo/cargo_expand.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::env;
66
use std::error;
77
use std::fmt;
88
use std::io;
9-
use std::path::Path;
9+
use std::path::{Path, PathBuf};
1010
use std::process::Command;
1111
use std::str::{from_utf8, Utf8Error};
1212

@@ -75,6 +75,12 @@ pub fn expand(
7575
cmd.env("CARGO_TARGET_DIR", _temp_dir.unwrap().path());
7676
} else if let Ok(ref path) = env::var("CARGO_EXPAND_TARGET_DIR") {
7777
cmd.env("CARGO_TARGET_DIR", path);
78+
} else if let Ok(ref path) = env::var("OUT_DIR") {
79+
// When cbindgen was started programatically from a build.rs file, Cargo is running and
80+
// locking the default target directory. In this case we need to use another directory,
81+
// else we would end up in a deadlock. If Cargo is running `OUT_DIR` will be set, so we
82+
// can use a directory relative to that.
83+
cmd.env("CARGO_TARGET_DIR", PathBuf::from(path).join("expanded"));
7884
}
7985

8086
cmd.arg("rustc");

0 commit comments

Comments
 (0)