diff --git a/Cargo.lock b/Cargo.lock index 8b0b8e21077..1899b721b16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1606,6 +1606,17 @@ dependencies = [ "instant", ] +[[package]] +name = "fd-lock" +version = "3.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb21c69b9fea5e15dbc1049e4b77145dd0ba1c84019c488102de0dc4ea4b0a27" +dependencies = [ + "cfg-if 1.0.0", + "rustix 0.36.4", + "windows-sys 0.42.0", +] + [[package]] name = "ff" version = "0.12.1" @@ -1778,6 +1789,7 @@ name = "forc-pkg" version = "0.31.3" dependencies = [ "anyhow", + "fd-lock", "forc-tracing", "forc-util", "fuels-types", @@ -2662,6 +2674,16 @@ version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "59ce5ef949d49ee85593fc4d3f3f95ad61657076395cbbce23e2121fc5542074" +[[package]] +name = "io-lifetimes" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c" +dependencies = [ + "libc", + "windows-sys 0.42.0", +] + [[package]] name = "itertools" version = "0.10.5" @@ -2854,6 +2876,12 @@ version = "0.0.46" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d4d2456c373231a208ad294c33dc5bff30051eafd954cd4caae83a712b12854d" +[[package]] +name = "linux-raw-sys" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f9f08d8963a6c613f4b1a78f4f4a4dbfadf8e6545b2d72861731e4858b8b47f" + [[package]] name = "lock_api" version = "0.4.9" @@ -3884,9 +3912,23 @@ checksum = "727a1a6d65f786ec22df8a81ca3121107f235970dc1705ed681d3e6e8b9cd5f9" dependencies = [ "bitflags", "errno", - "io-lifetimes", + "io-lifetimes 0.7.5", + "libc", + "linux-raw-sys 0.0.46", + "windows-sys 0.42.0", +] + +[[package]] +name = "rustix" +version = "0.36.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb93e85278e08bb5788653183213d3a60fc242b10cb9be96586f5a73dcb67c23" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes 1.0.3", "libc", - "linux-raw-sys", + "linux-raw-sys 0.1.3", "windows-sys 0.42.0", ] @@ -4817,7 +4859,7 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40ca90c434fd12083d1a6bdcbe9f92a14f96c8a1ba600ba451734ac334521f7a" dependencies = [ - "rustix", + "rustix 0.35.13", "windows-sys 0.42.0", ] diff --git a/forc-pkg/Cargo.toml b/forc-pkg/Cargo.toml index 3ccc770c4eb..cc3f7f47caf 100644 --- a/forc-pkg/Cargo.toml +++ b/forc-pkg/Cargo.toml @@ -10,6 +10,7 @@ description = "Building, locking, fetching and updating Sway projects as Forc pa [dependencies] anyhow = "1" +fd-lock = "3.0.8" forc-tracing = { version = "0.31.3", path = "../forc-tracing" } forc-util = { version = "0.31.3", path = "../forc-util" } fuels-types = "0.31" diff --git a/forc-pkg/src/pkg.rs b/forc-pkg/src/pkg.rs index 21a70221549..9377619fadd 100644 --- a/forc-pkg/src/pkg.rs +++ b/forc-pkg/src/pkg.rs @@ -7,6 +7,7 @@ use crate::{ CORE, PRELUDE, STD, }; use anyhow::{anyhow, bail, Context, Error, Result}; +use fd_lock::RwLock; use forc_util::{ default_output_directory, find_file_name, git_checkouts_directory, kebab_to_snake_case, print_on_failure, print_on_success, @@ -1609,11 +1610,20 @@ fn with_tmp_git_repo(fetch_id: u64, name: &str, source: &SourceGit, f: F) where F: FnOnce(git2::Repository) -> Result, { - // Clear existing temporary directory if it exists. let repo_dir = tmp_git_repo_dir(fetch_id, name, &source.repo); - if repo_dir.exists() { - let _ = std::fs::remove_dir_all(&repo_dir); - } + + // Always clear existing temporary directory. + let _ = std::fs::remove_dir_all(&repo_dir); + + let _ = std::fs::create_dir_all(&repo_dir); + + let mut lock = RwLock::new( + fs::OpenOptions::new() + .write(true) + .create(true) + .open(&repo_dir.join(".forc-lock"))?, + ); + let _ = lock.write()?; // Initialise the repository. let repo = git2::Repository::init(&repo_dir) @@ -1811,10 +1821,17 @@ pub fn fetch_git(fetch_id: u64, name: &str, pinned: &SourceGitPinned) -> Result< // Change HEAD to point to the pinned commit. let id = git2::Oid::from_str(&pinned.commit_hash)?; repo.set_head_detached(id)?; - if path.exists() { - let _ = std::fs::remove_dir_all(&path); - } - std::fs::create_dir_all(&path)?; + + let _ = std::fs::remove_dir_all(&path); + + let _ = std::fs::create_dir_all(&path); + let mut lock = RwLock::new( + fs::OpenOptions::new() + .write(true) + .create(true) + .open(&path.join(".forc-lock"))?, + ); + let _ = lock.write()?; // Checkout HEAD to the target directory. let mut checkout = git2::build::CheckoutBuilder::new();