Skip to content

Commit 223ad06

Browse files
committed
Moved cli frontend of the cairo linter to scarb command
1 parent 308ded6 commit 223ad06

File tree

12 files changed

+453
-4
lines changed

12 files changed

+453
-4
lines changed

Cargo.lock

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ repository = "https://github.com/software-mansion/scarb"
4747
# To keep our Cargo.toml following this contract, always use `cargo xtask upgrade`
4848
# for manipulating these dependencies.
4949
[workspace.dependencies]
50+
annotate-snippets = "0.11.5"
5051
anyhow = "1"
5152
assert_fs = "1"
5253
async-trait = "0.1"
@@ -71,6 +72,7 @@ cairo-lang-test-plugin = "*"
7172
cairo-lang-test-runner = "*"
7273
cairo-lang-utils = { version = "*", features = ["env_logger"] }
7374
cairo-language-server = "*"
75+
cairo-lint-core = {git = "https://github.com/software-mansion/cairo-lint", rev = "3cd07f36a6d509c8844284dd88a17535b6b1e265"}
7476
camino = { version = "1", features = ["serde1"] }
7577
cargo_metadata = ">=0.18"
7678
clap = { version = "4", features = ["derive", "env", "string"] }

scarb/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ readme.workspace = true
1414
repository.workspace = true
1515

1616
[dependencies]
17+
annotate-snippets.workspace = true
1718
anyhow.workspace = true
1819
async-trait.workspace = true
1920
cairo-lang-compiler.workspace = true
@@ -34,6 +35,7 @@ cairo-lang-starknet.workspace = true
3435
cairo-lang-syntax.workspace = true
3536
cairo-lang-test-plugin.workspace = true
3637
cairo-lang-utils.workspace = true
38+
cairo-lint-core.workspace = true
3739
camino.workspace = true
3840
clap.workspace = true
3941
convert_case.workspace = true

scarb/src/bin/scarb/args.rs

+23
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ pub enum Command {
187187
to a registry.
188188
")]
189189
Publish(PublishArgs),
190+
/// Run lint checker.
191+
Lint(LintArgs),
190192
/// Run arbitrary package scripts.
191193
Run(ScriptsRunnerArgs),
192194
/// Execute all unit and integration tests of a local package.
@@ -531,6 +533,27 @@ pub struct PublishArgs {
531533
pub ignore_cairo_version: bool,
532534
}
533535

536+
#[derive(Parser, Clone, Debug)]
537+
pub struct LintArgs {
538+
/// Name of the package.
539+
#[command(flatten)]
540+
pub packages_filter: PackagesFilter,
541+
/// Path to the file or project to analyze
542+
pub path: Option<String>,
543+
/// Logging verbosity.
544+
#[command(flatten)]
545+
pub verbose: VerbositySpec,
546+
/// Comma separated list of target names to compile.
547+
#[arg(long, value_delimiter = ',', env = "SCARB_TARGET_NAMES")]
548+
pub target_names: Vec<String>,
549+
/// Should lint the tests.
550+
#[arg(short, long, default_value_t = false)]
551+
pub test: bool,
552+
/// Should fix the lint when it can.
553+
#[arg(short, long, default_value_t = false)]
554+
pub fix: bool,
555+
}
556+
534557
/// Git reference specification arguments.
535558
#[derive(Parser, Clone, Debug)]
536559
#[group(requires = "git", multiple = false)]

scarb/src/bin/scarb/commands/lint.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use scarb::ops::{self, LintOptions};
2+
3+
use crate::args::LintArgs;
4+
use anyhow::Result;
5+
use scarb::core::Config;
6+
7+
#[tracing::instrument(skip_all, level = "info")]
8+
pub fn run(args: LintArgs, config: &Config) -> Result<()> {
9+
let ws = ops::read_workspace(config.manifest_path(), config)?;
10+
let packages = args
11+
.packages_filter
12+
.match_many(&ws)?
13+
.into_iter()
14+
.collect::<Vec<_>>();
15+
16+
ops::lint(
17+
LintOptions {
18+
packages,
19+
test: args.test,
20+
fix: args.fix,
21+
},
22+
&ws,
23+
)
24+
}

scarb/src/bin/scarb/commands/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub mod external;
1818
pub mod fetch;
1919
pub mod fmt;
2020
pub mod init;
21+
mod lint;
2122
pub mod manifest_path;
2223
pub mod metadata;
2324
pub mod new;
@@ -52,6 +53,7 @@ pub fn run(command: Command, config: &mut Config) -> Result<()> {
5253
Package(args) => package::run(args, config),
5354
ProcMacroServer => proc_macro_server::run(config),
5455
Publish(args) => publish::run(args, config),
56+
Lint(args) => lint::run(args, config),
5557
Remove(args) => remove::run(args, config),
5658
Run(args) => run::run(args, config),
5759
Test(args) => test::run(args, config),

scarb/src/compiler/db.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use cairo_lang_filesystem::db::{
1212
AsFilesGroupMut, CrateIdentifier, CrateSettings, DependencySettings, FilesGroup, FilesGroupEx,
1313
};
1414
use cairo_lang_filesystem::ids::CrateLongId;
15+
use cairo_lang_semantic::plugin::PluginSuite;
1516
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1617
use smol_str::SmolStr;
1718
use std::path::PathBuf;
@@ -26,12 +27,13 @@ pub struct ScarbDatabase {
2627
pub(crate) fn build_scarb_root_database(
2728
unit: &CairoCompilationUnit,
2829
ws: &Workspace<'_>,
30+
additional_plugins: Vec<PluginSuite>,
2931
) -> Result<ScarbDatabase> {
3032
let mut b = RootDatabase::builder();
3133
b.with_project_config(build_project_config(unit)?);
3234
b.with_cfg(unit.cfg_set.clone());
3335
b.with_inlining_strategy(unit.compiler_config.inlining_strategy.clone().into());
34-
let proc_macro_host = load_plugins(unit, ws, &mut b)?;
36+
let proc_macro_host = load_plugins(unit, ws, &mut b, additional_plugins)?;
3537
if !unit.compiler_config.enable_gas {
3638
b.skip_auto_withdraw_gas();
3739
}
@@ -50,6 +52,7 @@ fn load_plugins(
5052
unit: &CairoCompilationUnit,
5153
ws: &Workspace<'_>,
5254
builder: &mut RootDatabaseBuilder,
55+
additional_plugins: Vec<PluginSuite>,
5356
) -> Result<Arc<ProcMacroHostPlugin>> {
5457
let mut proc_macros = ProcMacroHost::default();
5558
for plugin_info in &unit.cairo_plugins {
@@ -64,6 +67,9 @@ fn load_plugins(
6467
proc_macros.register_new(plugin_info.package.clone(), ws.config())?;
6568
}
6669
}
70+
for plugin in additional_plugins {
71+
builder.with_plugin_suite(plugin);
72+
}
6773
let macro_host = Arc::new(proc_macros.into_plugin()?);
6874
builder.with_plugin_suite(ProcMacroHostPlugin::build_plugin_suite(macro_host.clone()));
6975
Ok(macro_host)

scarb/src/ops/compile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn compile_unit_inner(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
219219
let ScarbDatabase {
220220
mut db,
221221
proc_macro_host,
222-
} = build_scarb_root_database(&unit, ws)?;
222+
} = build_scarb_root_database(&unit, ws, vec![])?;
223223
check_starknet_dependency(&unit, ws, &db, &package_name);
224224
let result = ws.config().compilers().compile(unit, &mut db, ws);
225225
proc_macro_host
@@ -284,7 +284,7 @@ fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
284284
let result = match unit {
285285
CompilationUnit::ProcMacro(unit) => proc_macro::check_unit(unit, ws),
286286
CompilationUnit::Cairo(unit) => {
287-
let ScarbDatabase { db, .. } = build_scarb_root_database(&unit, ws)?;
287+
let ScarbDatabase { db, .. } = build_scarb_root_database(&unit, ws, vec![])?;
288288
let main_crate_ids = collect_main_crate_ids(&unit, &db);
289289
check_starknet_dependency(&unit, ws, &db, &package_name);
290290
let mut compiler_config = build_compiler_config(&db, &unit, &main_crate_ids, ws);

scarb/src/ops/expand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn do_expand(
168168
opts: ExpandOpts,
169169
ws: &Workspace<'_>,
170170
) -> Result<()> {
171-
let ScarbDatabase { db, .. } = build_scarb_root_database(compilation_unit, ws)?;
171+
let ScarbDatabase { db, .. } = build_scarb_root_database(compilation_unit, ws, vec![])?;
172172
let name = compilation_unit.main_component().cairo_package_name();
173173
let main_crate_id = db.intern_crate(CrateLongId::Real {
174174
name,

0 commit comments

Comments
 (0)