diff --git a/tooling/nargo_cli/src/cli/mod.rs b/tooling/nargo_cli/src/cli/mod.rs index 8c72fde893c..a5d406573ae 100644 --- a/tooling/nargo_cli/src/cli/mod.rs +++ b/tooling/nargo_cli/src/cli/mod.rs @@ -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 { @@ -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(()) } @@ -215,7 +215,7 @@ fn command_dir(cmd: &NargoCommand, program_dir: &Path) -> Result 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(..)