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

feat: lock on Nargo.toml on several nargo commands #6941

Merged
merged 10 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 11 additions & 11 deletions Cargo.lock

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

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ noir_lsp = { path = "tooling/lsp" }
noir_debugger = { path = "tooling/debugger" }
noirc_abi = { path = "tooling/noirc_abi" }
noirc_artifacts = { path = "tooling/noirc_artifacts" }
bb_abstraction_leaks = { path = "tooling/bb_abstraction_leaks" }
acvm_cli = { path = "tooling/acvm_cli" }

# Arkworks
ark-bn254 = { version = "^0.5.0", default-features = false, features = [
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ termion = "3.0.0"
tracing-subscriber.workspace = true
tracing-appender = "0.2.3"
clap_complete = "4.5.36"
fs2 = "0.4.3"

[target.'cfg(not(unix))'.dependencies]
tokio-util = { version = "0.7.8", features = ["compat"] }
Expand All @@ -85,7 +86,6 @@ dirs.workspace = true
assert_cmd = "2.0.8"
assert_fs = "1.0.10"
predicates = "2.1.5"
file-lock = "2.1.11"
fm.workspace = true
criterion.workspace = true
pprof.workspace = true
Expand All @@ -101,7 +101,7 @@ light-poseidon = "0.2.0"
ark-bn254-v04 = { package = "ark-bn254", version = "^0.4.0", default-features = false, features = [
"curve",
] }
ark-ff-v04 = { package = "ark-ff", version = "^0.4.0", default-features = false }
ark-ff-v04 = { package = "ark-ff", version = "^0.4.0", default-features = false }

[[bench]]
name = "criterion"
Expand Down
23 changes: 0 additions & 23 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,33 +177,13 @@ fn generate_test_cases(
}
let test_cases = test_cases.join("\n");

// We need to isolate test cases in the same group, otherwise they overwrite each other's artifacts.
// On CI we use `cargo nextest`, which runs tests in different processes; for this we use a file lock.
// Locally we might be using `cargo test`, which run tests in the same process; in this case the file lock
// wouldn't work, becuase the process itself has the lock, and it looks like it can have N instances without
// any problems; for this reason we also use a `Mutex`.
let mutex_name = format! {"TEST_MUTEX_{}", test_name.to_uppercase()};
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
write!(
test_file,
r#"
lazy_static::lazy_static! {{
/// Prevent concurrent tests in the matrix from overwriting the compilation artifacts in {test_dir}
static ref {mutex_name}: std::sync::Mutex<()> = std::sync::Mutex::new(());
}}

{test_cases}
fn test_{test_name}(force_brillig: ForceBrillig, inliner_aggressiveness: Inliner) {{
let test_program_dir = PathBuf::from("{test_dir}");

// Ignore poisoning errors if some of the matrix cases failed.
let mutex_guard = {mutex_name}.lock().unwrap_or_else(|e| e.into_inner());

let file_guard = file_lock::FileLock::lock(
test_program_dir.join("Nargo.toml"),
true,
file_lock::FileOptions::new().read(true).write(true).append(true)
).expect("failed to lock Nargo.toml");

let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
nargo.arg("{test_command}").arg("--force");
Expand All @@ -221,9 +201,6 @@ fn test_{test_name}(force_brillig: ForceBrillig, inliner_aggressiveness: Inliner
}}

{test_content}

drop(file_guard);
drop(mutex_guard);
}}
"#
)
Expand Down
6 changes: 6 additions & 0 deletions tooling/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errors::CliError;
use crate::lock::Lock;

use clap::Args;
use fm::FileManager;
Expand Down Expand Up @@ -34,6 +35,8 @@ pub(crate) struct CheckCommand {

pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliError> {
let toml_path = get_package_manifest(&config.program_dir)?;
let lock = Lock::lock(toml_path.clone());
aakoshh marked this conversation as resolved.
Show resolved Hide resolved

let selection = args.package_options.package_selection();
let workspace = resolve_workspace_from_toml(
&toml_path,
Expand All @@ -57,6 +60,9 @@ pub(crate) fn run(args: CheckCommand, config: NargoConfig) -> Result<(), CliErro
println!("[{}] Constraint system successfully built!", package.name);
}
}

lock.unlock();

Ok(())
}

Expand Down
6 changes: 6 additions & 0 deletions tooling/nargo_cli/src/cli/compile_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use notify_debouncer_full::new_debouncer;

use crate::errors::CliError;
use crate::lock::Lock;

use super::fs::program::{read_program_from_file, save_contract_to_file, save_program_to_file};
use super::{NargoConfig, PackageOptions};
Expand All @@ -42,6 +43,9 @@

pub(crate) fn run(args: CompileCommand, config: NargoConfig) -> Result<(), CliError> {
let selection = args.package_options.package_selection();
let toml_path = get_package_manifest(&config.program_dir)?;
let lock = Lock::lock(toml_path);

let workspace = read_workspace(&config.program_dir, selection)?;

if args.watch {
Expand All @@ -51,6 +55,8 @@
compile_workspace_full(&workspace, &args.compile_options)?;
}

lock.unlock();

Ok(())
}

Expand All @@ -74,7 +80,7 @@
fn watch_workspace(workspace: &Workspace, compile_options: &CompileOptions) -> notify::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();

// No specific tickrate, max debounce time 1 seconds

Check warning on line 83 in tooling/nargo_cli/src/cli/compile_cmd.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (tickrate)
let mut debouncer = new_debouncer(Duration::from_secs(1), None, tx)?;

// Add a path to be watched. All files and directories at that path and
Expand All @@ -82,7 +88,7 @@
debouncer.watcher().watch(&workspace.root_dir, RecursiveMode::Recursive)?;

let mut screen = std::io::stdout();
write!(screen, "{}", termion::cursor::Save).unwrap();

Check warning on line 91 in tooling/nargo_cli/src/cli/compile_cmd.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (termion)
screen.flush().unwrap();
let _ = compile_workspace_full(workspace, compile_options);
for res in rx {
Expand All @@ -103,7 +109,7 @@
});

if noir_files_modified {
write!(screen, "{}{}", termion::cursor::Restore, termion::clear::AfterCursor).unwrap();

Check warning on line 112 in tooling/nargo_cli/src/cli/compile_cmd.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (termion)

Check warning on line 112 in tooling/nargo_cli/src/cli/compile_cmd.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (termion)
screen.flush().unwrap();
let _ = compile_workspace_full(workspace, compile_options);
}
Expand Down
6 changes: 6 additions & 0 deletions tooling/nargo_cli/src/cli/execute_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use super::fs::{inputs::read_inputs_from_file, witness::save_witness_to_dir};
use super::{NargoConfig, PackageOptions};
use crate::cli::fs::program::read_program_from_file;
use crate::errors::CliError;
use crate::lock::Lock;

/// Executes a circuit to calculate its return value
#[derive(Debug, Clone, Args)]
Expand Down Expand Up @@ -48,6 +49,8 @@ pub(crate) struct ExecuteCommand {

pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliError> {
let toml_path = get_package_manifest(&config.program_dir)?;
let lock = Lock::lock(toml_path.clone());

let selection = args.package_options.package_selection();
let workspace = resolve_workspace_from_toml(
&toml_path,
Expand Down Expand Up @@ -98,6 +101,9 @@ pub(crate) fn run(args: ExecuteCommand, config: NargoConfig) -> Result<(), CliEr
}
}
}

lock.unlock();

Ok(())
}

Expand Down
11 changes: 9 additions & 2 deletions tooling/nargo_cli/src/cli/export_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use noirc_driver::{
use clap::Args;

use crate::errors::CliError;
use crate::lock::Lock;

use super::check_cmd::check_crate_and_report_errors;

Expand All @@ -36,6 +37,8 @@ pub(crate) struct ExportCommand {

pub(crate) fn run(args: ExportCommand, config: NargoConfig) -> Result<(), CliError> {
let toml_path = get_package_manifest(&config.program_dir)?;
let lock = Lock::lock(toml_path.clone());

let selection = args.package_options.package_selection();
let workspace = resolve_workspace_from_toml(
&toml_path,
Expand All @@ -50,7 +53,7 @@ pub(crate) fn run(args: ExportCommand, config: NargoConfig) -> Result<(), CliErr
let library_packages: Vec<_> =
workspace.into_iter().filter(|package| package.is_library()).collect();

library_packages
let result = library_packages
.par_iter()
.map(|package| {
compile_exported_functions(
Expand All @@ -61,7 +64,11 @@ pub(crate) fn run(args: ExportCommand, config: NargoConfig) -> Result<(), CliErr
&args.compile_options,
)
})
.collect()
.collect();

lock.unlock();

result
}

fn compile_exported_functions(
Expand Down
6 changes: 5 additions & 1 deletion tooling/nargo_cli/src/cli/info_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use prettytable::{row, table, Row};
use rayon::prelude::*;
use serde::Serialize;

use crate::{cli::fs::inputs::read_inputs_from_file, errors::CliError};
use crate::{cli::fs::inputs::read_inputs_from_file, errors::CliError, lock::Lock};

use super::{
compile_cmd::{compile_workspace_full, get_target_width},
Expand Down Expand Up @@ -49,6 +49,8 @@ pub(crate) struct InfoCommand {

pub(crate) fn run(mut args: InfoCommand, config: NargoConfig) -> Result<(), CliError> {
let toml_path = get_package_manifest(&config.program_dir)?;
let lock = Lock::lock(toml_path.clone());

let selection = args.package_options.package_selection();
let workspace = resolve_workspace_from_toml(
&toml_path,
Expand Down Expand Up @@ -118,6 +120,8 @@ pub(crate) fn run(mut args: InfoCommand, config: NargoConfig) -> Result<(), CliE
}
}

lock.unlock();

Ok(())
}

Expand Down
26 changes: 26 additions & 0 deletions tooling/nargo_cli/src/lock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::{fs::File, path::PathBuf};

use fs2::FileExt as _;

/// A lock used to lock the Nargo.toml file so two concurrent runs of nargo
/// commands (for example two `nargo execute`) don't overwrite output artifacts.
pub(crate) struct Lock {
file: File,
}

impl Lock {
#[allow(clippy::self_named_constructors)]
pub(crate) fn lock(toml_path: PathBuf) -> Self {
let file = File::open(toml_path).expect("Expected Nargo.toml to exist");
if file.try_lock_exclusive().is_err() {
eprintln!("Waiting for lock on Nargo.toml...");
}

file.lock_exclusive().expect("Failed to lock Nargo.toml");
Self { file }
}

pub(crate) fn unlock(&self) {
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
self.file.unlock().expect("Failed to unlock Nargo.toml");
}
}
1 change: 1 addition & 0 deletions tooling/nargo_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

mod cli;
mod errors;
mod lock;

use std::env;

Expand Down
Loading