diff --git a/scarb/src/compiler/plugin/proc_macro/compilation.rs b/scarb/src/compiler/plugin/proc_macro/compilation.rs index 407b42013..885b19ee0 100644 --- a/scarb/src/compiler/plugin/proc_macro/compilation.rs +++ b/scarb/src/compiler/plugin/proc_macro/compilation.rs @@ -45,8 +45,21 @@ impl SharedLibraryProvider for Package { } pub fn compile_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Result<()> { + run_cargo(CargoAction::Build, unit, ws) +} + +pub fn check_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Result<()> { + run_cargo(CargoAction::Check, unit, ws) +} + +fn run_cargo( + action: CargoAction, + unit: ProcMacroCompilationUnit, + ws: &Workspace<'_>, +) -> Result<()> { let main_package = unit.components.first().unwrap().package.clone(); let cmd = CargoCommand { + action, current_dir: main_package.root().to_path_buf(), target_dir: main_package .target_path(ws.config()) @@ -54,22 +67,32 @@ pub fn compile_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Resul .to_path_buf(), }; { - let _ = trace_span!("compile_proc_macro").enter(); + let _ = trace_span!("proc_macro").enter(); exec(&mut cmd.into(), ws.config())?; } Ok(()) } +enum CargoAction { + Build, + Check, +} + struct CargoCommand { current_dir: Utf8PathBuf, target_dir: Utf8PathBuf, + action: CargoAction, } impl From for Command { fn from(args: CargoCommand) -> Self { let mut cmd = Command::new("cargo"); cmd.current_dir(args.current_dir); - cmd.args(["build", "--release"]); + match args.action { + CargoAction::Build => cmd.arg("build"), + CargoAction::Check => cmd.arg("check"), + }; + cmd.arg("--release"); cmd.arg("--target-dir"); cmd.arg(args.target_dir); cmd diff --git a/scarb/src/compiler/plugin/proc_macro/mod.rs b/scarb/src/compiler/plugin/proc_macro/mod.rs index 4588446d1..0f77e16b0 100644 --- a/scarb/src/compiler/plugin/proc_macro/mod.rs +++ b/scarb/src/compiler/plugin/proc_macro/mod.rs @@ -2,6 +2,6 @@ pub mod compilation; mod ffi; mod host; -pub use compilation::compile_unit; +pub use compilation::{check_unit, compile_unit}; pub use ffi::*; pub use host::*; diff --git a/scarb/src/ops/compile.rs b/scarb/src/ops/compile.rs index 1c5d77707..d4fbf310d 100644 --- a/scarb/src/ops/compile.rs +++ b/scarb/src/ops/compile.rs @@ -127,7 +127,7 @@ fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> { .print(Status::new("Checking", &unit.name())); match unit { - CompilationUnit::ProcMacro(_unit) => (), + CompilationUnit::ProcMacro(unit) => proc_macro::check_unit(unit, ws)?, CompilationUnit::Cairo(unit) => { let db = build_scarb_root_database(&unit, ws)?; diff --git a/scarb/tests/build_cairo_plugin.rs b/scarb/tests/build_cairo_plugin.rs index ef8259551..fdaabad79 100644 --- a/scarb/tests/build_cairo_plugin.rs +++ b/scarb/tests/build_cairo_plugin.rs @@ -120,6 +120,33 @@ fn compile_cairo_plugin() { ); } +#[test] +fn check_cairo_plugin() { + let t = TempDir::new().unwrap(); + simple_project(&t); + let output = Scarb::quick_snapbox() + .arg("check") + .current_dir(&t) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + let stdout = String::from_utf8_lossy(&output.stdout).to_string(); + assert!(stdout.contains("Checking hello v1.0.0")); + let lines = stdout.lines().map(ToString::to_string).collect::>(); + let (last, lines) = lines.split_last().unwrap(); + assert_matches(r#"[..] Finished checking release target(s) in [..]"#, last); + let (last, _lines) = lines.split_last().unwrap(); + // Line from Cargo output + assert_matches( + r#"[..] Finished release [optimized] target(s) in [..]"#, + last, + ); +} + #[test] fn compile_cairo_plugin_with_lib_target() { let t = TempDir::new().unwrap();