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

add file locks to prevent git checkout races #3484

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
519e924
initial commit
spiral-ladder Nov 6, 2022
264b322
Merge branch 'master' into bing/forc-fs
spiral-ladder Nov 8, 2022
4fb5f41
Merge remote-tracking branch 'origin' into bing/forc-fs
spiral-ladder Dec 1, 2022
1fc95e6
fix lock location
spiral-ladder Dec 1, 2022
d576b55
reuse fetch_id
spiral-ladder Dec 1, 2022
27d92c1
rename: repo_dir_root -> repo_dir_path
spiral-ladder Dec 1, 2022
ce1c40c
rename: git_commit_root -> git_commit_path
spiral-ladder Dec 1, 2022
7bf0292
cleanup unused functions and comments
spiral-ladder Dec 1, 2022
37f484b
use lock in fetch_git
spiral-ladder Dec 1, 2022
61fbafb
Update forc-fs Cargo.toml
spiral-ladder Dec 1, 2022
6a127ca
more renames: root -> path
spiral-ladder Dec 1, 2022
1c7bba3
use into_path_unlocked for 'forc template'
spiral-ladder Dec 1, 2022
3f771b8
remove forc-fs
spiral-ladder Dec 2, 2022
d8e8110
refactor: forc_fs::Filesystem -> PathBuf
spiral-ladder Dec 2, 2022
816ff9b
Merge branch 'master' of github.com:FuelLabs/sway into bing/forc-fs
spiral-ladder Dec 2, 2022
aee9d08
more reverts
spiral-ladder Dec 2, 2022
8cc8e7e
add fd-lock
spiral-ladder Dec 2, 2022
5d25ca7
use fd-lock
spiral-ladder Dec 2, 2022
f2cc379
add lock file to constants
spiral-ladder Dec 2, 2022
4b5c0d9
reorder lock
spiral-ladder Dec 2, 2022
5eb4707
lock.write
spiral-ladder Dec 2, 2022
b22aeb8
reorder?
spiral-ladder Dec 2, 2022
f32b49d
Merge branch 'master' of github.com:FuelLabs/sway into bing/forc-fs
spiral-ladder Dec 5, 2022
565a643
add exists check again
spiral-ladder Dec 5, 2022
26c1ba1
fix build errors/warnings
spiral-ladder Dec 5, 2022
d43767b
always remove dir
spiral-ladder Dec 5, 2022
1eac74d
lock before remove_dir_all
spiral-ladder Dec 5, 2022
ebf2071
use file to lock
spiral-ladder Dec 5, 2022
fa61cd0
rearrange
spiral-ladder Dec 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions forc-pkg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 25 additions & 8 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1609,11 +1610,20 @@ fn with_tmp_git_repo<F, O>(fetch_id: u64, name: &str, source: &SourceGit, f: F)
where
F: FnOnce(git2::Repository) -> Result<O>,
{
// 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)
Expand Down Expand Up @@ -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();
Expand Down