Skip to content

Commit c4cde23

Browse files
authored
Implement check command for cairo plugins (#1148)
commit-id:1299f0be --- **Stack**: - #1161 - #1159 - #1157 - #1143 - #1151 - #1150 - #1155 - #1148⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
1 parent fcdae26 commit c4cde23

File tree

4 files changed

+65
-16
lines changed

4 files changed

+65
-16
lines changed

scarb/src/compiler/plugin/proc_macro/compilation.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,50 @@ impl SharedLibraryProvider for Package {
4545
}
4646

4747
pub fn compile_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
48+
run_cargo(CargoAction::Build, unit, ws)
49+
}
50+
51+
pub fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
52+
run_cargo(CargoAction::Check, unit, ws)
53+
}
54+
55+
fn run_cargo(action: CargoAction, unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
4856
let main_package = unit.components.first().unwrap().package.clone();
4957
let cmd = CargoCommand {
58+
action,
5059
current_dir: main_package.root().to_path_buf(),
5160
target_dir: main_package
5261
.target_path(ws.config())
5362
.path_unchecked()
5463
.to_path_buf(),
5564
};
5665
{
57-
let _ = trace_span!("compile_proc_macro").enter();
66+
let _ = trace_span!("proc_macro").enter();
5867
exec(&mut cmd.into(), ws.config())?;
5968
}
6069
Ok(())
6170
}
6271

72+
enum CargoAction {
73+
Build,
74+
Check,
75+
}
76+
6377
struct CargoCommand {
6478
current_dir: Utf8PathBuf,
6579
target_dir: Utf8PathBuf,
80+
action: CargoAction,
6681
}
6782

6883
impl From<CargoCommand> for Command {
6984
fn from(args: CargoCommand) -> Self {
7085
let mut cmd = Command::new("cargo");
7186
cmd.current_dir(args.current_dir);
72-
cmd.args(["build", "--release"]);
87+
match args.action {
88+
CargoAction::Build => cmd.arg("build"),
89+
CargoAction::Check => cmd.arg("check"),
90+
};
91+
cmd.arg("--release");
7392
cmd.arg("--target-dir");
7493
cmd.arg(args.target_dir);
7594
cmd

scarb/src/compiler/plugin/proc_macro/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ pub mod compilation;
22
mod ffi;
33
mod host;
44

5-
pub use compilation::compile_unit;
5+
pub use compilation::{check_unit, compile_unit};
66
pub use ffi::*;
77
pub use host::*;

scarb/src/ops/compile.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,27 @@ fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
125125
.ui()
126126
.print(Status::new("Checking", &unit.name()));
127127

128-
let db = build_scarb_root_database(&unit, ws)?;
128+
if unit.is_cairo_plugin() {
129+
proc_macro::check_unit(unit, ws)?;
130+
} else {
131+
let db = build_scarb_root_database(&unit, ws)?;
129132

130-
check_starknet_dependency(&unit, ws, &db, &package_name);
133+
check_starknet_dependency(&unit, ws, &db, &package_name);
131134

132-
let mut compiler_config = build_compiler_config(&unit, ws);
135+
let mut compiler_config = build_compiler_config(&unit, ws);
133136

134-
compiler_config
135-
.diagnostics_reporter
136-
.ensure(&db)
137-
.map_err(|err| {
138-
let valid_error = err.into();
139-
if !suppress_error(&valid_error) {
140-
ws.config().ui().anyhow(&valid_error);
141-
}
137+
compiler_config
138+
.diagnostics_reporter
139+
.ensure(&db)
140+
.map_err(|err| {
141+
let valid_error = err.into();
142+
if !suppress_error(&valid_error) {
143+
ws.config().ui().anyhow(&valid_error);
144+
}
142145

143-
anyhow!("could not check `{package_name}` due to previous error")
144-
})?;
146+
anyhow!("could not check `{package_name}` due to previous error")
147+
})?;
148+
}
145149

146150
Ok(())
147151
}

scarb/tests/build_cairo_plugin.rs

+26
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,32 @@ fn compile_cairo_plugin() {
119119
assert_matches(r#"[..]Finished release [optimized] target(s) in[..]"#, last);
120120
}
121121

122+
#[test]
123+
fn check_cairo_plugin() {
124+
let t = TempDir::new().unwrap();
125+
simple_project(&t);
126+
let output = Scarb::quick_snapbox()
127+
.arg("check")
128+
// Disable colors in Cargo output.
129+
.env("CARGO_TERM_COLOR", "never")
130+
.current_dir(&t)
131+
.output()
132+
.unwrap();
133+
assert!(
134+
output.status.success(),
135+
"{}",
136+
String::from_utf8_lossy(&output.stderr)
137+
);
138+
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
139+
assert!(stdout.contains("Checking hello v1.0.0"));
140+
let lines = stdout.lines().map(ToString::to_string).collect::<Vec<_>>();
141+
let (last, lines) = lines.split_last().unwrap();
142+
assert_matches(r#"[..] Finished checking release target(s) in [..]"#, last);
143+
let (last, _lines) = lines.split_last().unwrap();
144+
// Line from Cargo output
145+
assert_matches(r#"[..]Finished release [optimized] target(s) in[..]"#, last);
146+
}
147+
122148
#[test]
123149
fn compile_cairo_plugin_with_lib_target() {
124150
let t = TempDir::new().unwrap();

0 commit comments

Comments
 (0)