Skip to content

Commit

Permalink
Moved cli frontend of the cairo linter to scarb command (#1873)
Browse files Browse the repository at this point in the history
  • Loading branch information
wawel37 authored Jan 15, 2025
1 parent f1e8abb commit 152926a
Show file tree
Hide file tree
Showing 13 changed files with 595 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ jobs:
- name: Upgrade CairoLS to latest main commit
run: cargo xtask upgrade cairols --rev $(git ls-remote --refs "https://github.com/software-mansion/cairols" main | awk '{print $1}')

- name: Upgrade Cairo-lint to latest main commit
run: cargo xtask upgrade cairolint --rev $(git ls-remote --refs "https://github.com/software-mansion/cairo-lint" main | awk '{print $1}')

- name: Rebuild xtasks after Cargo.toml changes
run: cargo build -p xtask

Expand Down
37 changes: 37 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ cairo-lang-test-plugin = "*"
cairo-lang-test-runner = "*"
cairo-lang-utils = { version = "*", features = ["env_logger"] }
cairo-language-server = "*"
cairo-lint-core = "*"
camino = { version = "1", features = ["serde1"] }
cargo_metadata = ">=0.18"
clap = { version = "4", features = ["derive", "env", "string"] }
Expand Down Expand Up @@ -192,6 +193,7 @@ cairo-lang-test-runner = { git = "https://github.com/starkware-libs/cairo", rev
cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", rev = "03944ce36c4b37ef954d7f462d23edce8669e692" }
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", rev = "03944ce36c4b37ef954d7f462d23edce8669e692" }
cairo-language-server = { git = "https://github.com/software-mansion/cairols", rev = "6432886fea7564816078ed140434addf50bbaa23" }
cairo-lint-core = { git = "https://github.com/software-mansion/cairo-lint", rev = "b95a1949b932e89179c052efdbf4e21002ce6777" }

[profile.release]
lto = true
Expand Down
2 changes: 2 additions & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ cairo-lang-starknet.workspace = true
cairo-lang-syntax.workspace = true
cairo-lang-test-plugin.workspace = true
cairo-lang-utils.workspace = true
cairo-lint-core.workspace = true
camino.workspace = true
clap.workspace = true
convert_case.workspace = true
Expand Down Expand Up @@ -116,6 +117,7 @@ similar-asserts.workspace = true
snapbox.workspace = true
test-case.workspace = true
test-for-each-example = { path = "../utils/test-for-each-example" }
cairo-lint-core = { workspace = true, features = ["testing-colors"] }

[build-dependencies]
fs_extra.workspace = true
Expand Down
21 changes: 21 additions & 0 deletions scarb/src/bin/scarb/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ pub enum Command {
to a registry.
")]
Publish(PublishArgs),
/// Checks a package to catch common mistakes and improve your Cairo code.
Lint(LintArgs),
/// Run arbitrary package scripts.
Run(ScriptsRunnerArgs),
/// Execute all unit and integration tests of a local package.
Expand Down Expand Up @@ -531,6 +533,25 @@ pub struct PublishArgs {
pub ignore_cairo_version: bool,
}

#[derive(Parser, Clone, Debug)]
pub struct LintArgs {
/// Name of the package.
#[command(flatten)]
pub packages_filter: PackagesFilter,

/// Should lint the tests.
#[arg(short, long, default_value_t = false)]
pub test: bool,

/// Should fix the lint when it can.
#[arg(short, long, default_value_t = false)]
pub fix: bool,

/// Do not error on `cairo-version` mismatch.
#[arg(long)]
pub ignore_cairo_version: bool,
}

/// Git reference specification arguments.
#[derive(Parser, Clone, Debug)]
#[group(requires = "git", multiple = false)]
Expand Down
25 changes: 25 additions & 0 deletions scarb/src/bin/scarb/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use scarb::ops::{self, LintOptions};

use crate::args::LintArgs;
use anyhow::Result;
use scarb::core::Config;

#[tracing::instrument(skip_all, level = "info")]
pub fn run(args: LintArgs, config: &Config) -> Result<()> {
let ws = ops::read_workspace(config.manifest_path(), config)?;
let packages = args
.packages_filter
.match_many(&ws)?
.into_iter()
.collect::<Vec<_>>();

ops::lint(
LintOptions {
packages,
test: args.test,
fix: args.fix,
ignore_cairo_version: args.ignore_cairo_version,
},
&ws,
)
}
2 changes: 2 additions & 0 deletions scarb/src/bin/scarb/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod external;
pub mod fetch;
pub mod fmt;
pub mod init;
mod lint;
pub mod manifest_path;
pub mod metadata;
pub mod new;
Expand Down Expand Up @@ -52,6 +53,7 @@ pub fn run(command: Command, config: &mut Config) -> Result<()> {
Package(args) => package::run(args, config),
ProcMacroServer => proc_macro_server::run(config),
Publish(args) => publish::run(args, config),
Lint(args) => lint::run(args, config),
Remove(args) => remove::run(args, config),
Run(args) => run::run(args, config),
Test(args) => test::run(args, config),
Expand Down
8 changes: 7 additions & 1 deletion scarb/src/compiler/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cairo_lang_filesystem::db::{
AsFilesGroupMut, CrateIdentifier, CrateSettings, DependencySettings, FilesGroup, FilesGroupEx,
};
use cairo_lang_filesystem::ids::CrateLongId;
use cairo_lang_semantic::plugin::PluginSuite;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use smol_str::SmolStr;
use std::path::PathBuf;
Expand All @@ -26,12 +27,13 @@ pub struct ScarbDatabase {
pub(crate) fn build_scarb_root_database(
unit: &CairoCompilationUnit,
ws: &Workspace<'_>,
additional_plugins: Vec<PluginSuite>,
) -> Result<ScarbDatabase> {
let mut b = RootDatabase::builder();
b.with_project_config(build_project_config(unit)?);
b.with_cfg(unit.cfg_set.clone());
b.with_inlining_strategy(unit.compiler_config.inlining_strategy.clone().into());
let proc_macro_host = load_plugins(unit, ws, &mut b)?;
let proc_macro_host = load_plugins(unit, ws, &mut b, additional_plugins)?;
if !unit.compiler_config.enable_gas {
b.skip_auto_withdraw_gas();
}
Expand All @@ -50,6 +52,7 @@ fn load_plugins(
unit: &CairoCompilationUnit,
ws: &Workspace<'_>,
builder: &mut RootDatabaseBuilder,
additional_plugins: Vec<PluginSuite>,
) -> Result<Arc<ProcMacroHostPlugin>> {
let mut proc_macros = ProcMacroHost::default();
for plugin_info in &unit.cairo_plugins {
Expand All @@ -64,6 +67,9 @@ fn load_plugins(
proc_macros.register_new(plugin_info.package.clone(), ws.config())?;
}
}
for plugin in additional_plugins {
builder.with_plugin_suite(plugin);
}
let macro_host = Arc::new(proc_macros.into_plugin()?);
builder.with_plugin_suite(ProcMacroHostPlugin::build_plugin_suite(macro_host.clone()));
Ok(macro_host)
Expand Down
5 changes: 3 additions & 2 deletions scarb/src/ops/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn compile_unit_inner(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
let ScarbDatabase {
mut db,
proc_macro_host,
} = build_scarb_root_database(&unit, ws)?;
} = build_scarb_root_database(&unit, ws, Default::default())?;
check_starknet_dependency(&unit, ws, &db, &package_name);
let result = ws.config().compilers().compile(unit, &mut db, ws);
proc_macro_host
Expand Down Expand Up @@ -284,7 +284,8 @@ fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
let result = match unit {
CompilationUnit::ProcMacro(unit) => proc_macro::check_unit(unit, ws),
CompilationUnit::Cairo(unit) => {
let ScarbDatabase { db, .. } = build_scarb_root_database(&unit, ws)?;
let ScarbDatabase { db, .. } =
build_scarb_root_database(&unit, ws, Default::default())?;
let main_crate_ids = collect_main_crate_ids(&unit, &db);
check_starknet_dependency(&unit, ws, &db, &package_name);
let mut compiler_config = build_compiler_config(&db, &unit, &main_crate_ids, ws);
Expand Down
3 changes: 2 additions & 1 deletion scarb/src/ops/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ fn do_expand(
opts: ExpandOpts,
ws: &Workspace<'_>,
) -> Result<()> {
let ScarbDatabase { db, .. } = build_scarb_root_database(compilation_unit, ws)?;
let ScarbDatabase { db, .. } =
build_scarb_root_database(compilation_unit, ws, Default::default())?;
let name = compilation_unit.main_component().cairo_package_name();
let main_crate_id = db.intern_crate(CrateLongId::Real {
name,
Expand Down
Loading

0 comments on commit 152926a

Please sign in to comment.