Skip to content

Commit 152926a

Browse files
authored
Moved cli frontend of the cairo linter to scarb command (#1873)
1 parent f1e8abb commit 152926a

File tree

13 files changed

+595
-4
lines changed

13 files changed

+595
-4
lines changed

.github/workflows/nightly.yml

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ jobs:
4242
- name: Upgrade CairoLS to latest main commit
4343
run: cargo xtask upgrade cairols --rev $(git ls-remote --refs "https://github.com/software-mansion/cairols" main | awk '{print $1}')
4444

45+
- name: Upgrade Cairo-lint to latest main commit
46+
run: cargo xtask upgrade cairolint --rev $(git ls-remote --refs "https://github.com/software-mansion/cairo-lint" main | awk '{print $1}')
47+
4548
- name: Rebuild xtasks after Cargo.toml changes
4649
run: cargo build -p xtask
4750

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
@@ -71,6 +71,7 @@ cairo-lang-test-plugin = "*"
7171
cairo-lang-test-runner = "*"
7272
cairo-lang-utils = { version = "*", features = ["env_logger"] }
7373
cairo-language-server = "*"
74+
cairo-lint-core = "*"
7475
camino = { version = "1", features = ["serde1"] }
7576
cargo_metadata = ">=0.18"
7677
clap = { version = "4", features = ["derive", "env", "string"] }
@@ -192,6 +193,7 @@ cairo-lang-test-runner = { git = "https://github.com/starkware-libs/cairo", rev
192193
cairo-lang-test-utils = { git = "https://github.com/starkware-libs/cairo", rev = "03944ce36c4b37ef954d7f462d23edce8669e692" }
193194
cairo-lang-utils = { git = "https://github.com/starkware-libs/cairo", rev = "03944ce36c4b37ef954d7f462d23edce8669e692" }
194195
cairo-language-server = { git = "https://github.com/software-mansion/cairols", rev = "6432886fea7564816078ed140434addf50bbaa23" }
196+
cairo-lint-core = { git = "https://github.com/software-mansion/cairo-lint", rev = "b95a1949b932e89179c052efdbf4e21002ce6777" }
195197

196198
[profile.release]
197199
lto = true

scarb/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ cairo-lang-starknet.workspace = true
3434
cairo-lang-syntax.workspace = true
3535
cairo-lang-test-plugin.workspace = true
3636
cairo-lang-utils.workspace = true
37+
cairo-lint-core.workspace = true
3738
camino.workspace = true
3839
clap.workspace = true
3940
convert_case.workspace = true
@@ -116,6 +117,7 @@ similar-asserts.workspace = true
116117
snapbox.workspace = true
117118
test-case.workspace = true
118119
test-for-each-example = { path = "../utils/test-for-each-example" }
120+
cairo-lint-core = { workspace = true, features = ["testing-colors"] }
119121

120122
[build-dependencies]
121123
fs_extra.workspace = true

scarb/src/bin/scarb/args.rs

+21
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ pub enum Command {
187187
to a registry.
188188
")]
189189
Publish(PublishArgs),
190+
/// Checks a package to catch common mistakes and improve your Cairo code.
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,25 @@ 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+
542+
/// Should lint the tests.
543+
#[arg(short, long, default_value_t = false)]
544+
pub test: bool,
545+
546+
/// Should fix the lint when it can.
547+
#[arg(short, long, default_value_t = false)]
548+
pub fix: bool,
549+
550+
/// Do not error on `cairo-version` mismatch.
551+
#[arg(long)]
552+
pub ignore_cairo_version: bool,
553+
}
554+
534555
/// Git reference specification arguments.
535556
#[derive(Parser, Clone, Debug)]
536557
#[group(requires = "git", multiple = false)]

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

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
ignore_cairo_version: args.ignore_cairo_version,
22+
},
23+
&ws,
24+
)
25+
}

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

+3-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, Default::default())?;
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,8 @@ 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, .. } =
288+
build_scarb_root_database(&unit, ws, Default::default())?;
288289
let main_crate_ids = collect_main_crate_ids(&unit, &db);
289290
check_starknet_dependency(&unit, ws, &db, &package_name);
290291
let mut compiler_config = build_compiler_config(&db, &unit, &main_crate_ids, ws);

scarb/src/ops/expand.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ 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, .. } =
172+
build_scarb_root_database(compilation_unit, ws, Default::default())?;
172173
let name = compilation_unit.main_component().cairo_package_name();
173174
let main_crate_id = db.intern_crate(CrateLongId::Real {
174175
name,

0 commit comments

Comments
 (0)