From 20b6423b709ac07b0c19cbf8f73b28e676ce1139 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Thu, 8 Aug 2024 22:23:03 +0300 Subject: [PATCH 1/7] Add lint command --- zk_toolbox/crates/zk_supervisor/src/main.rs | 5 ++++- zk_toolbox/crates/zk_supervisor/src/messages.rs | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index 2976fb554184..70da0eadbe35 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -9,7 +9,7 @@ use common::{ use config::EcosystemConfig; use messages::{ msg_global_chain_does_not_exist, MSG_SUBCOMMAND_CLEAN, MSG_SUBCOMMAND_DATABASE_ABOUT, - MSG_SUBCOMMAND_TESTS_ABOUT, + MSG_SUBCOMMAND_LINT_ABOUT, MSG_SUBCOMMAND_TESTS_ABOUT, }; use xshell::Shell; @@ -38,6 +38,8 @@ enum SupervisorSubcommands { Clean(CleanCommands), #[command(subcommand, about = "Snapshots creator")] Snapshot(SnapshotCommands), + #[command(about = MSG_SUBCOMMAND_LINT_ABOUT, alias = "l")] + Lint, #[command(hide = true)] Markdown, } @@ -94,6 +96,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> { SupervisorSubcommands::Markdown => { clap_markdown::print_help_markdown::(); } + SupervisorSubcommands::Lint => todo!(), } Ok(()) } diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs index 116775072594..74181eacbeaa 100644 --- a/zk_toolbox/crates/zk_supervisor/src/messages.rs +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -9,6 +9,7 @@ pub(super) fn msg_global_chain_does_not_exist(chain: &str, available_chains: &st pub(super) const MSG_SUBCOMMAND_DATABASE_ABOUT: &str = "Database related commands"; pub(super) const MSG_SUBCOMMAND_TESTS_ABOUT: &str = "Run tests"; pub(super) const MSG_SUBCOMMAND_CLEAN: &str = "Clean artifacts"; +pub(super) const MSG_SUBCOMMAND_LINT_ABOUT: &str = "Lint code"; // Database related messages pub(super) const MSG_NO_DATABASES_SELECTED: &str = "No databases selected"; From 9110cce49018c127a47e97a12d72a3cbc811f7fb Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Fri, 9 Aug 2024 09:18:52 +0300 Subject: [PATCH 2/7] Add LintArgs --- .../crates/zk_supervisor/src/commands/lint.rs | 33 +++++++++++++++++++ .../crates/zk_supervisor/src/commands/mod.rs | 1 + zk_toolbox/crates/zk_supervisor/src/main.rs | 8 +++-- .../crates/zk_supervisor/src/messages.rs | 14 ++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 zk_toolbox/crates/zk_supervisor/src/commands/lint.rs diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs new file mode 100644 index 000000000000..07c15da34460 --- /dev/null +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -0,0 +1,33 @@ +use clap::{Parser, ValueEnum}; +use common::logger; +use strum::EnumIter; +use xshell::Shell; + +use crate::messages::msg_running_linters_for_files; + +#[derive(Debug, Parser)] +pub struct LintArgs { + #[clap(long, short = 'c')] + pub check: bool, + #[clap(long, short = 'e')] + pub extensions: Vec, +} + +#[derive(Debug, ValueEnum, EnumIter, strum::Display, PartialEq, Eq, Clone)] +pub enum Extension { + #[strum(serialize = "rs")] + Rs, + #[strum(serialize = "md")] + Md, + #[strum(serialize = "sol")] + Sol, + #[strum(serialize = "js")] + Js, + #[strum(serialize = "ts")] + Ts, +} + +pub fn run(shell: &Shell, args: LintArgs) -> anyhow::Result<()> { + logger::info(&msg_running_linters_for_files(&args.extensions)); + Ok(()) +} diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs index cc2b0a12b339..b7a6a54f1211 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/mod.rs @@ -1,4 +1,5 @@ pub mod clean; pub mod database; +pub mod lint; pub mod snapshot; pub mod test; diff --git a/zk_toolbox/crates/zk_supervisor/src/main.rs b/zk_toolbox/crates/zk_supervisor/src/main.rs index 70da0eadbe35..51b8f00ef373 100644 --- a/zk_toolbox/crates/zk_supervisor/src/main.rs +++ b/zk_toolbox/crates/zk_supervisor/src/main.rs @@ -1,5 +1,7 @@ use clap::{Parser, Subcommand}; -use commands::{database::DatabaseCommands, snapshot::SnapshotCommands, test::TestCommands}; +use commands::{ + database::DatabaseCommands, lint::LintArgs, snapshot::SnapshotCommands, test::TestCommands, +}; use common::{ check_general_prerequisites, config::{global_config, init_global_config, GlobalConfig}, @@ -39,7 +41,7 @@ enum SupervisorSubcommands { #[command(subcommand, about = "Snapshots creator")] Snapshot(SnapshotCommands), #[command(about = MSG_SUBCOMMAND_LINT_ABOUT, alias = "l")] - Lint, + Lint(LintArgs), #[command(hide = true)] Markdown, } @@ -96,7 +98,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> { SupervisorSubcommands::Markdown => { clap_markdown::print_help_markdown::(); } - SupervisorSubcommands::Lint => todo!(), + SupervisorSubcommands::Lint(args) => commands::lint::run(shell, args)?, } Ok(()) } diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs index 74181eacbeaa..652299a68033 100644 --- a/zk_toolbox/crates/zk_supervisor/src/messages.rs +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -1,3 +1,5 @@ +use crate::commands::lint::Extension; + // Ecosystem related messages pub(super) const MSG_CHAIN_NOT_FOUND_ERR: &str = "Chain not found"; @@ -132,3 +134,15 @@ pub(super) const MSG_CONTRACTS_CLEANING_FINISHED: &str = /// Snapshot creator related messages pub(super) const MSG_RUNNING_SNAPSHOT_CREATOR: &str = "Running snapshot creator"; + +// Lint related messages +pub(super) fn msg_running_linters_for_files(extensions: &Vec) -> String { + let extensions: Vec = extensions + .iter() + .map(|e| format!(".{}", e.to_string())) + .collect(); + format!( + "Running linters for files with extensions: {:?}", + extensions + ) +} From d105dbcb48423929601dba331765343092840880 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Fri, 9 Aug 2024 09:47:04 +0300 Subject: [PATCH 3/7] Add linter_rs --- .../crates/zk_supervisor/src/commands/lint.rs | 67 ++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs index 07c15da34460..1d833ee67188 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -1,7 +1,8 @@ use clap::{Parser, ValueEnum}; -use common::logger; +use common::{cmd::Cmd, logger}; +use config::EcosystemConfig; use strum::EnumIter; -use xshell::Shell; +use xshell::{cmd, Shell}; use crate::messages::msg_running_linters_for_files; @@ -28,6 +29,66 @@ pub enum Extension { } pub fn run(shell: &Shell, args: LintArgs) -> anyhow::Result<()> { - logger::info(&msg_running_linters_for_files(&args.extensions)); + let extensions = if args.extensions.is_empty() { + vec![ + Extension::Rs, + Extension::Md, + Extension::Sol, + Extension::Js, + Extension::Ts, + ] + } else { + args.extensions.clone() + }; + + logger::info(&msg_running_linters_for_files(&extensions)); + + let ecosystem = EcosystemConfig::from_file(shell)?; + + for extension in extensions { + match extension { + Extension::Rs => linter_rs(shell, &ecosystem)?, + Extension::Md => linter_md(shell, &ecosystem)?, + Extension::Sol => linter_sol(shell, &ecosystem, args.check)?, + Extension::Js => linter_js(shell, &ecosystem)?, + Extension::Ts => linter_ts(shell, &ecosystem)?, + } + } + + Ok(()) +} + +fn linter_rs(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { + let link_to_code = &ecosystem.link_to_code; + let lint_to_prover = &ecosystem.link_to_code.join("prover"); + let link_to_toolbox = &ecosystem.link_to_code.join("zk_toolbox"); + let paths = vec![link_to_code, lint_to_prover, link_to_toolbox]; + + for path in paths { + let _dir_guard = shell.push_dir(path); + Cmd::new(cmd!( + shell, + "cargo clippy --locked -- -D warnings -D unstable_features" + )) + .with_force_run() + .run()?; + } + Ok(()) } + +fn linter_md(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { + todo!() +} + +fn linter_sol(shell: &Shell, ecosystem: &EcosystemConfig, check: bool) -> anyhow::Result<()> { + todo!() +} + +fn linter_js(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { + todo!() +} + +fn linter_ts(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { + todo!() +} From a8bfce582f437555859b8c9cea69197a976e0013 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Fri, 9 Aug 2024 09:48:08 +0300 Subject: [PATCH 4/7] lint --- zk_toolbox/crates/zk_supervisor/src/commands/lint.rs | 2 +- zk_toolbox/crates/zk_supervisor/src/messages.rs | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs index 1d833ee67188..80322a052bb5 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -41,7 +41,7 @@ pub fn run(shell: &Shell, args: LintArgs) -> anyhow::Result<()> { args.extensions.clone() }; - logger::info(&msg_running_linters_for_files(&extensions)); + logger::info(msg_running_linters_for_files(&extensions)); let ecosystem = EcosystemConfig::from_file(shell)?; diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs index 652299a68033..ae18fe41ad45 100644 --- a/zk_toolbox/crates/zk_supervisor/src/messages.rs +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -136,11 +136,8 @@ pub(super) const MSG_CONTRACTS_CLEANING_FINISHED: &str = pub(super) const MSG_RUNNING_SNAPSHOT_CREATOR: &str = "Running snapshot creator"; // Lint related messages -pub(super) fn msg_running_linters_for_files(extensions: &Vec) -> String { - let extensions: Vec = extensions - .iter() - .map(|e| format!(".{}", e.to_string())) - .collect(); +pub(super) fn msg_running_linters_for_files(extensions: &[Extension]) -> String { + let extensions: Vec = extensions.iter().map(|e| format!(".{}", e)).collect(); format!( "Running linters for files with extensions: {:?}", extensions From 73ce4d0e517ca1be7eb2b1660bf823451ebf1241 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Mon, 12 Aug 2024 11:45:23 +0300 Subject: [PATCH 5/7] Add missing linters --- .../crates/zk_supervisor/src/commands/lint.rs | 134 +++++++++++++++--- .../crates/zk_supervisor/src/messages.rs | 7 + 2 files changed, 124 insertions(+), 17 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs index 80322a052bb5..20e88ec6e9b5 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -1,10 +1,44 @@ use clap::{Parser, ValueEnum}; -use common::{cmd::Cmd, logger}; +use common::{cmd::Cmd, logger, spinner::Spinner}; use config::EcosystemConfig; use strum::EnumIter; use xshell::{cmd, Shell}; -use crate::messages::msg_running_linters_for_files; +use crate::messages::{ + msg_running_linter_for_extension_spinner, msg_running_linters_for_files, + MSG_LINT_CONFIG_PATH_ERR, MSG_RUNNING_CONTRACTS_LINTER_SPINNER, +}; + +const IGNORED_DIRS: [&str; 18] = [ + "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", +]; + +const IGNORED_FILES: [&str; 4] = [ + "KeysWithPlonkVerifier.sol", + "TokenInit.sol", + ".tslintrc.js", + ".prettierrc.js", +]; + +const CONFIG_PATH: &str = "etc/lint-config"; #[derive(Debug, Parser)] pub struct LintArgs { @@ -47,18 +81,18 @@ pub fn run(shell: &Shell, args: LintArgs) -> anyhow::Result<()> { for extension in extensions { match extension { - Extension::Rs => linter_rs(shell, &ecosystem)?, - Extension::Md => linter_md(shell, &ecosystem)?, - Extension::Sol => linter_sol(shell, &ecosystem, args.check)?, - Extension::Js => linter_js(shell, &ecosystem)?, - Extension::Ts => linter_ts(shell, &ecosystem)?, + Extension::Rs => lint_rs(shell, &ecosystem)?, + Extension::Sol => lint_contracts(shell, &ecosystem, args.check)?, + ext => lint(shell, &ecosystem, &ext, args.check)?, } } Ok(()) } -fn linter_rs(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { +fn lint_rs(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { + let spinner = Spinner::new(&msg_running_linter_for_extension_spinner(&Extension::Rs)); + let link_to_code = &ecosystem.link_to_code; let lint_to_prover = &ecosystem.link_to_code.join("prover"); let link_to_toolbox = &ecosystem.link_to_code.join("zk_toolbox"); @@ -70,25 +104,91 @@ fn linter_rs(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { shell, "cargo clippy --locked -- -D warnings -D unstable_features" )) - .with_force_run() .run()?; } + spinner.finish(); + Ok(()) } -fn linter_md(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { - todo!() +fn get_linter(extension: &Extension) -> Vec { + match extension { + Extension::Rs => vec!["cargo".to_string(), "clippy".to_string()], + Extension::Md => vec!["markdownlint".to_string()], + Extension::Sol => vec!["solhint".to_string()], + Extension::Js => vec!["eslint".to_string()], + Extension::Ts => vec!["eslint".to_string(), "--ext".to_string(), "ts".to_string()], + } } -fn linter_sol(shell: &Shell, ecosystem: &EcosystemConfig, check: bool) -> anyhow::Result<()> { - todo!() +fn lint( + shell: &Shell, + ecosystem: &EcosystemConfig, + extension: &Extension, + check: bool, +) -> anyhow::Result<()> { + let spinner = Spinner::new(&msg_running_linter_for_extension_spinner(extension)); + let _dir_guard = shell.push_dir(&ecosystem.link_to_code); + let files = get_unignored_files(shell, extension)?; + + let cmd = cmd!(shell, "yarn"); + let config_path = ecosystem.link_to_code.join(CONFIG_PATH); + let config_path = config_path.join(format!("{}.js", extension)); + let config_path = config_path + .to_str() + .expect(MSG_LINT_CONFIG_PATH_ERR) + .to_string(); + + let linter = get_linter(extension); + + let fix_option = if check { + vec![] + } else { + vec!["--fix".to_string()] + }; + + let args = [ + linter.as_slice(), + &fix_option, + &["--config".to_string(), config_path], + files.as_slice(), + ] + .concat(); + + Cmd::new(cmd.args(&args)).run()?; + spinner.finish(); + Ok(()) } -fn linter_js(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { - todo!() +fn lint_contracts(shell: &Shell, ecosystem: &EcosystemConfig, check: bool) -> anyhow::Result<()> { + lint(shell, ecosystem, &Extension::Sol, check)?; + + let spinner = Spinner::new(MSG_RUNNING_CONTRACTS_LINTER_SPINNER); + let _dir_guard = shell.push_dir(&ecosystem.link_to_code); + let cmd = cmd!(shell, "yarn"); + let linter = if check { "lint:check" } else { "lint:fix" }; + let args = ["--cwd", "contracts", linter]; + Cmd::new(cmd.args(&args)).run()?; + spinner.finish(); + + Ok(()) } -fn linter_ts(shell: &Shell, ecosystem: &EcosystemConfig) -> anyhow::Result<()> { - todo!() +fn get_unignored_files(shell: &Shell, extension: &Extension) -> anyhow::Result> { + let mut files = Vec::new(); + let output = cmd!(shell, "git ls-files").read()?; + + for line in output.lines() { + let path = line.to_string(); + if !IGNORED_DIRS.iter().any(|dir| path.contains(dir)) + && !IGNORED_FILES.contains(&path.as_str()) + { + if path.ends_with(&format!(".{}", extension)) { + files.push(path); + } + } + } + + Ok(files) } diff --git a/zk_toolbox/crates/zk_supervisor/src/messages.rs b/zk_toolbox/crates/zk_supervisor/src/messages.rs index ae18fe41ad45..1466838b53d7 100644 --- a/zk_toolbox/crates/zk_supervisor/src/messages.rs +++ b/zk_toolbox/crates/zk_supervisor/src/messages.rs @@ -143,3 +143,10 @@ pub(super) fn msg_running_linters_for_files(extensions: &[Extension]) -> String extensions ) } + +pub(super) fn msg_running_linter_for_extension_spinner(extension: &Extension) -> String { + format!("Running linter for files with extension: .{}", extension) +} + +pub(super) const MSG_LINT_CONFIG_PATH_ERR: &str = "Lint config path error"; +pub(super) const MSG_RUNNING_CONTRACTS_LINTER_SPINNER: &str = "Running contracts linter.."; From f9820c75a012ca48ac6fe2f5cfbd2f9a1bcff8d3 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Tue, 13 Aug 2024 11:03:56 +0300 Subject: [PATCH 6/7] Add strum serialize all lowercase --- zk_toolbox/crates/zk_supervisor/src/commands/lint.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs index 20e88ec6e9b5..9a87421e1961 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -49,16 +49,12 @@ pub struct LintArgs { } #[derive(Debug, ValueEnum, EnumIter, strum::Display, PartialEq, Eq, Clone)] +#[strum(serialize_all = "lowercase")] pub enum Extension { - #[strum(serialize = "rs")] Rs, - #[strum(serialize = "md")] Md, - #[strum(serialize = "sol")] Sol, - #[strum(serialize = "js")] Js, - #[strum(serialize = "ts")] Ts, } From b853f50e916e712b13b2e11c9d60d64932f5c9f1 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Tue, 13 Aug 2024 11:08:36 +0300 Subject: [PATCH 7/7] lint --- zk_toolbox/crates/zk_supervisor/src/commands/lint.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs index 9a87421e1961..bbad72f65377 100644 --- a/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs +++ b/zk_toolbox/crates/zk_supervisor/src/commands/lint.rs @@ -179,10 +179,9 @@ fn get_unignored_files(shell: &Shell, extension: &Extension) -> anyhow::Result