Skip to content

Commit

Permalink
feat: add --check option to nargo fmt for dry-run formatting verifica…
Browse files Browse the repository at this point in the history
…tion (#3530)

Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
kek kek kek and kevaundray authored Nov 23, 2023
1 parent 03ab6a2 commit 4469707
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
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 @@ use super::NargoConfig;

/// Format the Noir files in a workspace
#[derive(Debug, Clone, Args)]
pub(crate) struct FormatCommand {}
pub(crate) struct FormatCommand {
/// Run noirfmt in check mode
#[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 @@ pub(crate) fn run(_args: FormatCommand, config: NargoConfig) -> Result<(), CliEr
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 @@ pub(crate) fn run(_args: FormatCommand, config: NargoConfig) -> Result<(), CliEr
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

0 comments on commit 4469707

Please sign in to comment.