Skip to content

Commit

Permalink
Lock shared for commands that don't write artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jan 6, 2025
1 parent 5d263d4 commit c4a397b
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions tooling/nargo_cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,19 @@ pub(crate) fn start_cli() -> eyre::Result<()> {
config.program_dir = program_dir;
}

let lock_file = if needs_lock(&command) {
let toml_path = get_package_manifest(&config.program_dir)?;
let file = File::open(toml_path).expect("Expected Nargo.toml to exist");
if file.try_lock_exclusive().is_err() {
let toml_path = get_package_manifest(&config.program_dir)?;
let lock_file = File::open(toml_path).expect("Expected Nargo.toml to exist");

if needs_exclusive_lock(&command) {
if lock_file.try_lock_exclusive().is_err() {
eprintln!("Waiting for lock on Nargo.toml...");
}

file.lock_exclusive().expect("Failed to lock Nargo.toml");
Some(file)
lock_file.lock_exclusive().expect("Failed to lock Nargo.toml");
} else {
None
if lock_file.try_lock_shared().is_err() {
eprintln!("Waiting for lock on Nargo.toml...");
}
lock_file.lock_shared().expect("Failed to lock Nargo.toml");
};

match command {
Expand All @@ -164,9 +166,7 @@ pub(crate) fn start_cli() -> eyre::Result<()> {
NargoCommand::GenerateCompletionScript(args) => generate_completion_script_cmd::run(args),
}?;

if let Some(lock_file) = lock_file {
lock_file.unlock().expect("Failed to unlock Nargo.toml");
}
lock_file.unlock().expect("Failed to unlock Nargo.toml");

Ok(())
}
Expand Down Expand Up @@ -215,7 +215,7 @@ fn command_dir(cmd: &NargoCommand, program_dir: &Path) -> Result<Option<PathBuf>
Ok(Some(nargo_toml::find_root(program_dir, workspace)?))
}

fn needs_lock(cmd: &NargoCommand) -> bool {
fn needs_exclusive_lock(cmd: &NargoCommand) -> bool {
match cmd {
NargoCommand::Check(..)
| NargoCommand::Compile(..)
Expand Down

0 comments on commit c4a397b

Please sign in to comment.