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: add --check option to nargo fmt for dry-run formatting verification #3530

Merged
merged 8 commits into from Nov 23, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ hex = "0.4.2"
const_format = "0.2.30"
num-bigint = "0.4"
num-traits = "0.2"
similar-asserts = "1.5.0"

[profile.dev]
# This is required to be able to run `cargo test` in acvm_js due to the `locals exceeds maximum` error.
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tower.workspace = true
async-lsp = { workspace = true, features = ["client-monitor", "stdio", "tracing", "tokio"] }
const_format.workspace = true
hex.workspace = true
similar-asserts.workspace = true
termcolor = "1.1.2"
color-eyre = "0.6.2"
tokio = { version = "1.0", features = ["io-std"] }
Expand Down
48 changes: 40 additions & 8 deletions tooling/nargo_cli/src/cli/fmt_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@

/// Format the Noir files in a workspace
#[derive(Debug, Clone, Args)]
pub(crate) struct FormatCommand {}
pub(crate) struct FormatCommand {
/// Run noirfmt in check mode

Check warning on line 17 in tooling/nargo_cli/src/cli/fmt_cmd.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (noirfmt)
#[arg(long)]
check: bool,
}

pub(crate) fn run(args: FormatCommand, config: NargoConfig) -> Result<(), CliError> {
let check_mode = args.check;

pub(crate) fn run(_args: FormatCommand, config: NargoConfig) -> Result<(), CliError> {
let toml_path = get_package_manifest(&config.program_dir)?;
let workspace = resolve_workspace_from_toml(
&toml_path,
Expand All @@ -26,6 +32,8 @@
let config = nargo_fmt::Config::read(&config.program_dir)
.map_err(|err| CliError::Generic(err.to_string()))?;

let mut check_exit_code_one = false;

for package in &workspace {
let mut file_manager =
FileManager::new(&package.root_dir, Box::new(|path| std::fs::read_to_string(path)));
Expand Down Expand Up @@ -53,16 +61,40 @@
return Ok(());
}

let source = nargo_fmt::format(
file_manager.fetch_file(file_id).source(),
parsed_module,
&config,
);
let original = file_manager.fetch_file(file_id).source();
let formatted = nargo_fmt::format(original, parsed_module, &config);

if check_mode {
let diff = similar_asserts::SimpleDiff::from_str(
original,
&formatted,
"original",
"formatted",
)
.to_string();

if !diff.lines().next().is_some_and(|line| line.contains("Invisible differences")) {
if !check_exit_code_one {
check_exit_code_one = true;
}

std::fs::write(entry.path(), source)
println!("{diff}");
}

Ok(())
} else {
std::fs::write(entry.path(), formatted)
}
})
.map_err(|error| CliError::Generic(error.to_string()))?;
}

if check_exit_code_one {
std::process::exit(1);
} else if check_mode {
println!("No formatting changes were detected");
}

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tooling/nargo_fmt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ toml.workspace = true
thiserror.workspace = true

[dev-dependencies]
similar-asserts = "1.5.0"
similar-asserts.workspace = true
Loading