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: improve nargo check cli with --override flag and feedback for existing files #4575

Merged
merged 8 commits into from
Apr 4, 2024
Merged
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
43 changes: 35 additions & 8 deletions tooling/nargo_cli/src/cli/check_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ pub(crate) struct CheckCommand {
#[clap(long, conflicts_with = "package")]
workspace: bool,

/// Force overwrite of existing files
#[clap(long = "overwrite")]
allow_overwrite: bool,

#[clap(flatten)]
compile_options: CompileOptions,
}
Expand All @@ -58,18 +62,29 @@ pub(crate) fn run(
let parsed_files = parse_all(&workspace_file_manager);

for package in &workspace {
check_package(&workspace_file_manager, &parsed_files, package, &args.compile_options)?;
println!("[{}] Constraint system successfully built!", package.name);
let any_file_written = check_package(
&workspace_file_manager,
&parsed_files,
package,
&args.compile_options,
args.allow_overwrite,
)?;
if any_file_written {
println!("[{}] Constraint system successfully built!", package.name);
}
}
Ok(())
}

/// Evaluates the necessity to create or update Prover.toml and Verifier.toml based on the allow_overwrite flag and files' existence.
/// Returns `true` if any file was generated or updated, `false` otherwise.
fn check_package(
file_manager: &FileManager,
parsed_files: &ParsedFiles,
package: &Package,
compile_options: &CompileOptions,
) -> Result<(), CompileError> {
allow_overwrite: bool,
) -> Result<bool, CompileError> {
let (mut context, crate_id) = prepare_package(file_manager, parsed_files, package);
check_crate_and_report_errors(
&mut context,
Expand All @@ -81,27 +96,39 @@ fn check_package(

if package.is_library() || package.is_contract() {
// Libraries do not have ABIs while contracts have many, so we cannot generate a `Prover.toml` file.
Ok(())
Ok(false)
} else {
// XXX: We can have a --overwrite flag to determine if you want to overwrite the Prover/Verifier.toml files
if let Some((parameters, return_type)) = compute_function_abi(&context, &crate_id) {
let path_to_prover_input = package.prover_input_path();
let path_to_verifier_input = package.verifier_input_path();

// If they are not available, then create them and populate them based on the ABI
if !path_to_prover_input.exists() {
// Before writing the file, check if it exists and whether overwrite is set
let should_write_prover = !path_to_prover_input.exists() || allow_overwrite;
let should_write_verifier = !path_to_verifier_input.exists() || allow_overwrite;

if should_write_prover {
let prover_toml = create_input_toml_template(parameters.clone(), None);
write_to_file(prover_toml.as_bytes(), &path_to_prover_input);
} else {
eprintln!("Note: Prover.toml already exists. Use --overwrite to force overwrite.");
}
if !path_to_verifier_input.exists() {

if should_write_verifier {
let public_inputs =
parameters.into_iter().filter(|param| param.is_public()).collect();

let verifier_toml = create_input_toml_template(public_inputs, return_type);
write_to_file(verifier_toml.as_bytes(), &path_to_verifier_input);
} else {
eprintln!(
"Note: Verifier.toml already exists. Use --overwrite to force overwrite."
);
}

Ok(())
let any_file_written = should_write_prover || should_write_verifier;

Ok(any_file_written)
} else {
Err(CompileError::MissingMainFunction(package.name.clone()))
}
Expand Down
Loading