diff --git a/Cargo.lock b/Cargo.lock index 7b02cc6da..1778b4fcd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1919,6 +1919,7 @@ dependencies = [ "dirs 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "fs2 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 334f1717b..ff65929f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ toml = "0.5.0" uuid = "0.7" which = "2.0.1" rand = "0.6.5" +fs2 = "0.4.3" [dev-dependencies] assert_cmd = "0.11.1" diff --git a/src/wranglerjs/mod.rs b/src/wranglerjs/mod.rs index 4b4989dfb..9ffa5881a 100644 --- a/src/wranglerjs/mod.rs +++ b/src/wranglerjs/mod.rs @@ -1,6 +1,7 @@ use crate::commands::publish::package::Package; use crate::install; use binary_install::Cache; +use fs2::FileExt; use log::info; use rand::distributions::Alphanumeric; use rand::{thread_rng, Rng}; @@ -191,6 +192,11 @@ pub fn run_build( // Run {npm install} in the specified directory. Skips the install if a // {node_modules} is found in the directory. pub fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> { + let flock_path = dir.join(&".install.lock"); + let flock = File::create(&flock_path)?; + // avoid running multiple {npm install} at the same time (eg. in tests) + flock.lock_exclusive()?; + if dir.join("node_modules").exists() { info!("skipping npm install because node_modules exists"); return Ok(()); @@ -202,6 +208,10 @@ pub fn run_npm_install(dir: PathBuf) -> Result<(), failure::Error> { info!("Running {:?} in directory {:?}", command, dir); let status = command.status()?; + + flock.unlock()?; + fs::remove_file(&flock_path)?; + if status.success() { Ok(()) } else {