Skip to content

Commit

Permalink
feat(zk_toolbox): Update lint CI with zk_toolbox (#2694)
Browse files Browse the repository at this point in the history
## What ❔
Update CI with `zk_toolbox`
  • Loading branch information
matias-gonz committed Aug 27, 2024
1 parent 589e122 commit 7e122e9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 83 deletions.
25 changes: 11 additions & 14 deletions .github/workflows/ci-core-lint-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@ jobs:
- name: Start services
run: |
mkdir -p ./volumes/postgres
run_retried docker compose pull zk postgres
docker compose up -d zk postgres
ci_localnet_up
ci_run sccache --start-server
- name: Setup db
- name: Build
run: |
ci_run zk
ci_run run_retried rustup show
ci_run zk db migrate
ci_run ./bin/zkt
ci_run yarn install
ci_run git config --global --add safe.directory /usr/src/zksync
- name: Lints
run: |
ci_run zk fmt --check
ci_run zk lint rust --check
ci_run zk lint toolbox --check
ci_run zk lint js --check
ci_run zk lint ts --check
ci_run zk lint md --check
ci_run zk db check-sqlx-data
ci_run zk_supervisor fmt --check
ci_run zk_supervisor lint -t md --check
ci_run zk_supervisor lint -t sol --check
ci_run zk_supervisor lint -t js --check
ci_run zk_supervisor lint -t ts --check
ci_run zk_supervisor lint -t rs --check
4 changes: 4 additions & 0 deletions .github/workflows/ci-zk-toolbox-reusable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ jobs:
--prover-db-url=postgres://postgres:notsecurepassword@postgres:5432 \
--prover-db-name=zksync_prover_localhost_rollup
- name: Check Database
run: |
ci_run zk_supervisor database check-sqlx-data
- name: Run server
run: |
ci_run zk_inception server --ignore-prerequisites &>server.log &
Expand Down
3 changes: 2 additions & 1 deletion zk_toolbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,12 @@ Lint code:
zks lint
```

By default, this command runs the linter on all files. To target specific file types, use the `--extension` option.
By default, this command runs the linter on all files. To target specific file types, use the `--target` option.
Supported extensions include:

- `rs`: Rust files.
- `md`: Markdown files.
- `sol`: Solidity files.
- `js`: JavaScript files.
- `ts`: TypeScript files.
- `contracts`: files in `contracts` directory.
27 changes: 13 additions & 14 deletions zk_toolbox/crates/zk_supervisor/src/commands/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ use config::EcosystemConfig;
use xshell::{cmd, Shell};

use crate::{
commands::lint_utils::{get_unignored_files, Extension},
commands::lint_utils::{get_unignored_files, Target},
messages::{
msg_running_fmt_for_extension_spinner, msg_running_fmt_for_extensions_spinner,
msg_running_rustfmt_for_dir_spinner, MSG_RUNNING_CONTRACTS_FMT_SPINNER,
},
};

async fn prettier(shell: Shell, extension: Extension, check: bool) -> anyhow::Result<()> {
let spinner = Spinner::new(&msg_running_fmt_for_extension_spinner(extension));
let files = get_unignored_files(&shell, &extension)?;
async fn prettier(shell: Shell, target: Target, check: bool) -> anyhow::Result<()> {
let spinner = Spinner::new(&msg_running_fmt_for_extension_spinner(target));
let files = get_unignored_files(&shell, &target)?;

if files.is_empty() {
return Ok(());
}

spinner.freeze();
let mode = if check { "--check" } else { "--write" };
let config = format!("etc/prettier-config/{extension}.js");
let config = format!("etc/prettier-config/{target}.js");
Ok(
Cmd::new(cmd!(shell, "yarn --silent prettier {mode} --config {config}").args(files))
.run()?,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub enum Formatter {
Contract,
Prettier {
#[arg(short, long)]
extensions: Vec<Extension>,
targets: Vec<Target>,
},
}

Expand All @@ -85,8 +85,7 @@ 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];
let extensions: Vec<_> = vec![Target::Js, Target::Ts, Target::Md, Target::Sol];
let spinner = Spinner::new(&msg_running_fmt_for_extensions_spinner(&extensions));
spinner.freeze();
for ext in extensions {
Expand All @@ -108,13 +107,13 @@ pub async fn run(shell: Shell, args: FmtArgs) -> anyhow::Result<()> {
}
});
}
Some(Formatter::Prettier { mut extensions }) => {
if extensions.is_empty() {
extensions = vec![Extension::Js, Extension::Ts, Extension::Md, Extension::Sol];
Some(Formatter::Prettier { mut targets }) => {
if targets.is_empty() {
targets = vec![Target::Js, Target::Ts, Target::Md, Target::Sol];
}
let spinner = Spinner::new(&msg_running_fmt_for_extensions_spinner(&extensions));
for ext in extensions {
prettier(shell.clone(), ext, args.check).await?
let spinner = Spinner::new(&msg_running_fmt_for_extensions_spinner(&targets));
for target in targets {
prettier(shell.clone(), target, args.check).await?
}
spinner.finish()
}
Expand Down
69 changes: 31 additions & 38 deletions zk_toolbox/crates/zk_supervisor/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use config::EcosystemConfig;
use xshell::{cmd, Shell};

use crate::{
commands::lint_utils::{get_unignored_files, Extension},
commands::lint_utils::{get_unignored_files, Target},
messages::{
msg_running_linter_for_extension_spinner, msg_running_linters_for_files,
MSG_LINT_CONFIG_PATH_ERR, MSG_RUNNING_CONTRACTS_LINTER_SPINNER,
Expand All @@ -17,31 +17,32 @@ const CONFIG_PATH: &str = "etc/lint-config";
pub struct LintArgs {
#[clap(long, short = 'c')]
pub check: bool,
#[clap(long, short = 'e')]
pub extensions: Vec<Extension>,
#[clap(long, short = 't')]
pub targets: Vec<Target>,
}

pub fn run(shell: &Shell, args: LintArgs) -> anyhow::Result<()> {
let extensions = if args.extensions.is_empty() {
let targets = if args.targets.is_empty() {
vec![
Extension::Rs,
Extension::Md,
Extension::Sol,
Extension::Js,
Extension::Ts,
Target::Rs,
Target::Md,
Target::Sol,
Target::Js,
Target::Ts,
Target::Contracts,
]
} else {
args.extensions.clone()
args.targets.clone()
};

logger::info(msg_running_linters_for_files(&extensions));
logger::info(msg_running_linters_for_files(&targets));

let ecosystem = EcosystemConfig::from_file(shell)?;

for extension in extensions {
match extension {
Extension::Rs => lint_rs(shell, &ecosystem, args.check)?,
Extension::Sol => lint_contracts(shell, &ecosystem, args.check)?,
for target in targets {
match target {
Target::Rs => lint_rs(shell, &ecosystem, args.check)?,
Target::Contracts => lint_contracts(shell, &ecosystem, args.check)?,
ext => lint(shell, &ecosystem, &ext, args.check)?,
}
}
Expand All @@ -50,7 +51,7 @@ pub fn run(shell: &Shell, args: LintArgs) -> anyhow::Result<()> {
}

fn lint_rs(shell: &Shell, ecosystem: &EcosystemConfig, check: bool) -> anyhow::Result<()> {
let spinner = Spinner::new(&msg_running_linter_for_extension_spinner(&Extension::Rs));
let spinner = Spinner::new(&msg_running_linter_for_extension_spinner(&Target::Rs));

let link_to_code = &ecosystem.link_to_code;
let lint_to_prover = &ecosystem.link_to_code.join("prover");
Expand All @@ -61,14 +62,7 @@ fn lint_rs(shell: &Shell, ecosystem: &EcosystemConfig, check: bool) -> anyhow::R
for path in paths {
let _dir_guard = shell.push_dir(path);
let mut cmd = cmd!(shell, "cargo clippy");
let common_args = &[
"--locked",
"--",
"-D",
"warnings",
"-D",
"unstable_features",
];
let common_args = &["--locked", "--", "-D", "warnings"];
if !check {
cmd = cmd.args(&["--fix", "--allow-dirty"]);
}
Expand All @@ -79,34 +73,35 @@ fn lint_rs(shell: &Shell, ecosystem: &EcosystemConfig, check: bool) -> anyhow::R
Ok(())
}

fn get_linter(extension: &Extension) -> Vec<String> {
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 get_linter(target: &Target) -> Vec<String> {
match target {
Target::Rs => vec!["cargo".to_string(), "clippy".to_string()],
Target::Md => vec!["markdownlint".to_string()],
Target::Sol => vec!["solhint".to_string()],
Target::Js => vec!["eslint".to_string()],
Target::Ts => vec!["eslint".to_string(), "--ext".to_string(), "ts".to_string()],
Target::Contracts => vec![],
}
}

fn lint(
shell: &Shell,
ecosystem: &EcosystemConfig,
extension: &Extension,
target: &Target,
check: bool,
) -> anyhow::Result<()> {
let spinner = Spinner::new(&msg_running_linter_for_extension_spinner(extension));
let spinner = Spinner::new(&msg_running_linter_for_extension_spinner(target));
let _dir_guard = shell.push_dir(&ecosystem.link_to_code);
let files = get_unignored_files(shell, extension)?;
let files = get_unignored_files(shell, target)?;
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.join(format!("{}.js", target));
let config_path = config_path
.to_str()
.expect(MSG_LINT_CONFIG_PATH_ERR)
.to_string();

let linter = get_linter(extension);
let linter = get_linter(target);

let fix_option = if check {
vec![]
Expand All @@ -128,8 +123,6 @@ fn lint(
}

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");
Expand Down
7 changes: 4 additions & 3 deletions zk_toolbox/crates/zk_supervisor/src/commands/lint_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,24 @@ const IGNORED_FILES: [&str; 4] = [

#[derive(Debug, ValueEnum, EnumIter, strum::Display, PartialEq, Eq, Clone, Copy)]
#[strum(serialize_all = "lowercase")]
pub enum Extension {
pub enum Target {
Md,
Sol,
Js,
Ts,
Rs,
Contracts,
}

pub fn get_unignored_files(shell: &Shell, extension: &Extension) -> anyhow::Result<Vec<String>> {
pub fn get_unignored_files(shell: &Shell, target: &Target) -> anyhow::Result<Vec<String>> {
let mut files = Vec::new();
let output = cmd!(shell, "git ls-files --recurse-submodules").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())
&& path.ends_with(&format!(".{}", extension))
&& path.ends_with(&format!(".{}", target))
{
files.push(path);
}
Expand Down
23 changes: 10 additions & 13 deletions zk_toolbox/crates/zk_supervisor/src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::commands::lint_utils::Extension;
use crate::commands::lint_utils::Target;

// Ecosystem related messages
pub(super) const MSG_CHAIN_NOT_FOUND_ERR: &str = "Chain not found";
Expand Down Expand Up @@ -152,28 +152,25 @@ 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: &[Extension]) -> String {
let extensions: Vec<String> = extensions.iter().map(|e| format!(".{}", e)).collect();
format!(
"Running linters for files with extensions: {:?}",
extensions
)
pub(super) fn msg_running_linters_for_files(targets: &[Target]) -> String {
let targets: Vec<String> = targets.iter().map(|e| format!(".{}", e)).collect();
format!("Running linters for targets: {:?}", targets)
}

pub(super) fn msg_running_linter_for_extension_spinner(extension: &Extension) -> String {
format!("Running linter for files with extension: .{}", extension)
pub(super) fn msg_running_linter_for_extension_spinner(target: &Target) -> String {
format!("Running linter for files with extension: .{}", target)
}

pub(super) fn msg_running_fmt_for_extension_spinner(extension: Extension) -> String {
format!("Running prettier for: {extension:?}")
pub(super) fn msg_running_fmt_for_extension_spinner(target: Target) -> String {
format!("Running prettier for: {target:?}")
}

pub(super) fn msg_running_rustfmt_for_dir_spinner(dir: &str) -> String {
format!("Running rustfmt for: {dir:?}")
}

pub(super) fn msg_running_fmt_for_extensions_spinner(extensions: &[Extension]) -> String {
format!("Running prettier for: {extensions:?} and rustfmt")
pub(super) fn msg_running_fmt_for_extensions_spinner(targets: &[Target]) -> String {
format!("Running prettier for: {targets:?} and rustfmt")
}

pub(super) const MSG_LINT_CONFIG_PATH_ERR: &str = "Lint config path error";
Expand Down

0 comments on commit 7e122e9

Please sign in to comment.