From 93c4ab6f950704ba4e3b19d6b2bcc0b8d03f12b1 Mon Sep 17 00:00:00 2001 From: Danil Date: Fri, 16 Aug 2024 17:55:55 +0200 Subject: [PATCH] Integrate formatters Signed-off-by: Danil --- .prettierignore | 26 ++++ .../heap_read_write.sol | 8 +- etc/contracts-test-data/counter/counter.sol | 6 +- zk_toolbox/Cargo.lock | 1 + zk_toolbox/Cargo.toml | 2 +- zk_toolbox/crates/zk_supervisor/Cargo.toml | 1 + .../crates/zk_supervisor/src/commands/fmt.rs | 144 +++++------------- .../crates/zk_supervisor/src/commands/lint.rs | 2 +- zk_toolbox/crates/zk_supervisor/src/main.rs | 2 +- 9 files changed, 82 insertions(+), 110 deletions(-) diff --git a/.prettierignore b/.prettierignore index 5138b38cc6c..297c599d56f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,6 +2,32 @@ bellman-cuda sdk/zksync-rs/CHANGELOG.md CHANGELOG.md +core/lib/dal/.sqlx +prover/lib/dal/.sqlx +node_modules # Ignore contract submodules contracts + +**/target/** +**/node_modules +volumes +**/build/** +dist +.git +generated +grafonnet-lib +prettier-config +lint-config +**/cache +**/artifacts +**/typechain +binaryen +system-contracts +artifacts-zk +cache-zk +// Ignore directories with OZ and forge submodules. +contracts/l1-contracts/lib + +**/.git +**/node_modules diff --git a/core/tests/vm-benchmark/deployment_benchmarks_sources/heap_read_write.sol b/core/tests/vm-benchmark/deployment_benchmarks_sources/heap_read_write.sol index d5a503eb708..6aa7ca59a0c 100644 --- a/core/tests/vm-benchmark/deployment_benchmarks_sources/heap_read_write.sol +++ b/core/tests/vm-benchmark/deployment_benchmarks_sources/heap_read_write.sol @@ -11,11 +11,15 @@ contract HeapBenchmark { mstore(add(array, sub(n, 1)), 4242) let j := 0 - for {} lt(j, n) {} { + for { + + } lt(j, n) { + + } { v1 := mload(add(array, mod(mul(j, j), n))) v2 := mload(add(array, j)) mstore(add(array, j), add(add(v1, v2), 42)) - j := add(j, 1) + j := add(j, 1) if gt(j, sub(n, 1)) { j := 0 } diff --git a/etc/contracts-test-data/counter/counter.sol b/etc/contracts-test-data/counter/counter.sol index 3c4e1922276..ec9219d7a19 100644 --- a/etc/contracts-test-data/counter/counter.sol +++ b/etc/contracts-test-data/counter/counter.sol @@ -9,13 +9,13 @@ contract Counter { value += x; } - function incrementWithRevertPayable(uint256 x, bool shouldRevert) payable public returns (uint256) { + function incrementWithRevertPayable(uint256 x, bool shouldRevert) public payable returns (uint256) { return incrementWithRevert(x, shouldRevert); } function incrementWithRevert(uint256 x, bool shouldRevert) public returns (uint256) { value += x; - if(shouldRevert) { + if (shouldRevert) { revert("This method always reverts"); } return value; @@ -24,4 +24,4 @@ contract Counter { function get() public view returns (uint256) { return value; } -} \ No newline at end of file +} diff --git a/zk_toolbox/Cargo.lock b/zk_toolbox/Cargo.lock index 04a29f5b0f4..140ef509194 100644 --- a/zk_toolbox/Cargo.lock +++ b/zk_toolbox/Cargo.lock @@ -6294,6 +6294,7 @@ dependencies = [ "clap-markdown", "common", "config", + "futures", "human-panic", "serde", "strum", diff --git a/zk_toolbox/Cargo.toml b/zk_toolbox/Cargo.toml index ab850d82770..6743a2f7484 100644 --- a/zk_toolbox/Cargo.toml +++ b/zk_toolbox/Cargo.toml @@ -58,4 +58,4 @@ tokio = { version = "1.37", features = ["full"] } toml = "0.8.12" url = { version = "2.5.0", features = ["serde"] } xshell = "0.2.6" -clap-markdown = "0.1.4" +clap-markdown = "0.1.4" \ No newline at end of file diff --git a/zk_toolbox/crates/zk_supervisor/Cargo.toml b/zk_toolbox/crates/zk_supervisor/Cargo.toml index 911fba2248a..94c649afcc2 100644 --- a/zk_toolbox/crates/zk_supervisor/Cargo.toml +++ b/zk_toolbox/crates/zk_supervisor/Cargo.toml @@ -22,3 +22,4 @@ url.workspace = true xshell.workspace = true serde.workspace = true clap-markdown.workspace = true +futures.workspace = true \ No newline at end of file diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/fmt.rs b/zk_toolbox/crates/zk_supervisor/src/commands/fmt.rs index 70587f27dac..bc08153e48d 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/fmt.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/fmt.rs @@ -1,51 +1,35 @@ -use std::env::args; -use std::ffi::OsStr; -use std::path::PathBuf; -use std::process::Command; +use std::{env::args, ffi::OsStr, future, path::PathBuf, process::Command}; use clap::Parser; -use common::{cmd::Cmd, logger}; +use common::{cmd::Cmd, logger, spinner::Spinner}; use xshell::{cmd, Shell}; -use crate::commands::lint::{Extension, IGNORED_DIRS, IGNORED_FILES}; +use crate::commands::lint::Extension; const CONFIG_PATH: &str = "etc/prettier-config"; -fn prettier(shell: &Shell, extension: Extension, check: bool) -> anyhow::Result<()> { - let command = if check { "check" } else { "write" }; - let files = get_unignored_files(shell, extension)?; - logger::info(format!("Got {} files for {extension}", files.len())); - - if files.is_empty() { - logger::info(format!("No files of extension {extension} to format")); - return Ok(()); - } - - let mut prettier_command = cmd!(shell, "yarn --silent prettier --config ") - .arg(format!("{}/{}.js", CONFIG_PATH, extension)) - .arg(format!("--{}", command)) - .arg(format!("{}", files.join(" "))); - - // if !check { - // prettier_command = prettier_command.arg("> /dev/null"); - // } - - Ok(prettier_command.run()?) +async fn prettier(shell: Shell, extension: Extension, check: bool) -> anyhow::Result<()> { + let spinner = Spinner::new(&format!("Running prettier for: {extension}")); + let mode = if check { "--check" } else { "--write" }; + let glob = format!("**/*.{extension}"); + let config = format!("etc/prettier-config/{extension}.js"); + Ok(Cmd::new(cmd!( + shell, + "yarn --silent prettier {glob} {mode} --config {config}" + )) + .run()?) } -fn prettier_contracts(shell: &Shell, check: bool) -> anyhow::Result<()> { - let mut prettier_command = cmd!(shell, "yarn --silent --cwd contracts") +async fn prettier_contracts(shell: Shell, check: bool) -> anyhow::Result<()> { + let prettier_command = cmd!(shell, "yarn --silent --cwd contracts") .arg(format!("prettier:{}", if check { "check" } else { "fix" })); - // if !check { - // prettier_command = prettier_command.arg("> /dev/null"); - // } - Ok(Cmd::new(prettier_command).run()?) } -fn rustfmt(shell: &Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result<()> { +async fn rustfmt(shell: Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result<()> { for dir in vec![".", "prover", "zk_toolbox"] { + logger::info(format!("Running rustfmt for: {dir}")); let _dir = shell.push_dir(link_to_code.join(dir)); let mut cmd = cmd!(shell, "cargo fmt -- --config imports_granularity=Crate --config group_imports=StdExternalCrate"); if check { @@ -56,13 +40,13 @@ fn rustfmt(shell: &Shell, check: bool, link_to_code: PathBuf) -> anyhow::Result< Ok(()) } -fn run_all_rust_formatters( - shell: &Shell, +async fn run_all_rust_formatters( + shell: Shell, check: bool, link_to_code: PathBuf, ) -> anyhow::Result<()> { - rustfmt(shell, check, link_to_code)?; - format_sqlx_queries(check)?; + rustfmt(shell.clone(), check, link_to_code).await?; + format_sqlx_queries(shell, check).await?; Ok(()) } @@ -84,89 +68,45 @@ pub struct FmtArgs { pub formatter: Option, } -pub fn run(shell: &Shell, args: FmtArgs) -> anyhow::Result<()> { +pub async fn run(shell: Shell, args: FmtArgs) -> anyhow::Result<()> { match args.formatter { None => { + let mut tasks = vec![]; let extensions: Vec<_> = vec![Extension::Js, Extension::Ts, Extension::Md, Extension::Sol]; for ext in extensions { - prettier(shell, ext, args.check)? + tasks.push(tokio::spawn(prettier(shell.clone(), ext, args.check))); } - run_all_rust_formatters(shell, args.check, ".".into())?; - prettier_contracts(shell, args.check)? + tasks.push(tokio::spawn(rustfmt(shell.clone(), args.check, ".".into()))); + tasks.push(tokio::spawn(format_sqlx_queries(shell.clone(), args.check))); + tasks.push(tokio::spawn(prettier_contracts(shell.clone(), args.check))); + + futures::future::join_all(tasks) + .await + .iter() + .for_each(|res| { + if let Err(err) = res { + logger::error(err) + } + }); } Some(Formatter::Prettier { mut extensions }) => { if extensions.is_empty() { extensions = vec![Extension::Js, Extension::Ts, Extension::Md, Extension::Sol]; } for ext in extensions { - prettier(shell, ext, args.check)? + prettier(shell.clone(), ext, args.check).await? } } - Some(Formatter::Rustfmt) => run_all_rust_formatters(shell, args.check, ".".into())?, - Some(Formatter::Contract) => prettier_contracts(shell, args.check)?, + Some(Formatter::Rustfmt) => { + run_all_rust_formatters(shell.clone(), args.check, ".".into()).await? + } + Some(Formatter::Contract) => prettier_contracts(shell.clone(), args.check).await?, } Ok(()) } -fn get_unignored_files(shell: &Shell, extension: Extension) -> anyhow::Result> { - let root = if let Extension::Sol = extension { - "contracts" - } else { - "." - }; - - let ignored_dirs: Vec<_> = IGNORED_DIRS - .iter() - .map(|dir| { - vec![ - "-o".to_string(), - "-path".to_string(), - format!("'*{dir}'"), - "-prune".to_string(), - ] - }) - .flatten() - .collect(); - - let ignored_files: Vec<_> = IGNORED_FILES - .iter() - .map(|file| { - vec![ - "-a".to_string(), - "!".to_string(), - "-name".to_ascii_lowercase(), - format!("'{file}'"), - ] - }) - .flatten() - .collect(); - - dbg!(shell.current_dir()); - // let output = Cmd::new( - // cmd!(shell, "find {root} -type f -name").arg(format!("'*.{extension}'")), // .args(ignored_files) - // // .arg("-print"), - // ) - // .run_with_output()?; - // dbg!(&output); - let output = Cmd::new( - cmd!(shell, "find {root} -type f -name '*.js'") - .args(ignored_files) - .arg("-print") - .args(ignored_dirs), - ) - .run_with_output()?; - dbg!(&output); - - let files = String::from_utf8(output.stdout)? - .lines() - .map(|line| line.to_string()) - .collect(); - - Ok(files) -} - -fn format_sqlx_queries(check: bool) -> anyhow::Result<()> { +async fn format_sqlx_queries(shell: Shell, check: bool) -> anyhow::Result<()> { // Implement your SQLx query formatting logic here. Ok(()) } diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs index fec5c93d063..f8fb15bf34e 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -151,7 +151,7 @@ fn lint( &["--config".to_string(), config_path], files.as_slice(), ] - .concat(); + .concat(); Cmd::new(cmd.args(&args)).run()?; spinner.finish(); diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index d079c437bf3..21d76b30dbb 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -101,7 +101,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> { clap_markdown::print_help_markdown::(); } SupervisorSubcommands::Lint(args) => commands::lint::run(shell, args)?, - SupervisorSubcommands::Fmt(args) => commands::fmt::run(shell, args)?, + SupervisorSubcommands::Fmt(args) => commands::fmt::run(shell.clone(), args).await?, } Ok(()) }