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

fix: lock git dependencies folder when resolving workspace #7327

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions tooling/nargo_toml/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ toml.workspace = true
url.workspace = true
noirc_driver.workspace = true
semver = "1.0.20"
fs2 = "0.4.3"
tracing.workspace = true

[dev-dependencies]
tempfile.workspace = true
Expand Down
33 changes: 33 additions & 0 deletions tooling/nargo_toml/src/flock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use fs2::FileExt;
use std::{
fs::{File, OpenOptions},
path::Path,
};

// TODO: move this to some utils crate.

pub(crate) struct FileLock {
file: File,
}

impl FileLock {
pub(crate) fn new(file_path: &Path, lock_name: &str) -> std::io::Result<Self> {
std::fs::create_dir_all(file_path.parent().expect("can't create lock on filesystem root"))?;
let file = OpenOptions::new().create(true).truncate(false).write(true).open(file_path)?;
if file.try_lock_exclusive().is_err() {
eprintln!("Waiting for lock on {lock_name}...");
}

file.lock_exclusive()?;

Ok(Self { file })
}
}

impl Drop for FileLock {
fn drop(&mut self) {
if let Err(e) = self.file.unlock() {
tracing::warn!("failed to release lock: {e:?}");
}
}
}
6 changes: 6 additions & 0 deletions tooling/nargo_toml/src/git.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::path::PathBuf;

use crate::flock::FileLock;

/// Creates a unique folder name for a GitHub repo
/// by using its URL and tag
fn resolve_folder_name(base: &url::Url, tag: &str) -> String {
Expand All @@ -23,6 +25,10 @@ fn git_dep_location(base: &url::Url, tag: &str) -> PathBuf {
nargo_crates().join(folder_name)
}

pub(crate) fn lock_git_deps() -> std::io::Result<FileLock> {
FileLock::new(&nargo_crates().join(".package-cache"), "git dependencies cache")
}

/// XXX: I'd prefer to use a GitHub library however, there
/// does not seem to be an easy way to download a repo at a specific
/// tag
Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo_toml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ use noirc_frontend::graph::CrateName;
use serde::Deserialize;

mod errors;
mod flock;
mod git;
mod semver;

pub use errors::ManifestError;
use git::clone_git_repo;
use git::{clone_git_repo, lock_git_deps};

/// Searches for a `Nargo.toml` file in the current directory and all parent directories.
/// For example, if the current directory is `/workspace/package/src`, then this function
Expand Down Expand Up @@ -382,6 +383,7 @@ fn toml_to_workspace(
package_selection: PackageSelection,
) -> Result<Workspace, ManifestError> {
let mut resolved = Vec::new();
let _lock = lock_git_deps().expect("Failed to lock git dependencies cache");
let workspace = match nargo_toml.config {
Config::Package { package_config } => {
let member = package_config.resolve_to_package(&nargo_toml.root_dir, &mut resolved)?;
Expand Down Expand Up @@ -519,6 +521,7 @@ pub fn resolve_workspace_from_toml(
) -> Result<Workspace, ManifestError> {
let nargo_toml = read_toml(toml_path)?;
let workspace = toml_to_workspace(nargo_toml, package_selection)?;

if let Some(current_compiler_version) = current_compiler_version {
semver::semver_check_workspace(&workspace, current_compiler_version)?;
}
Expand Down
Loading