Skip to content

Commit

Permalink
review update
Browse files Browse the repository at this point in the history
  • Loading branch information
tomek0123456789 committed Jan 25, 2024
1 parent deacd22 commit ea63ee9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 134 deletions.
12 changes: 0 additions & 12 deletions scarb/src/compiler/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use itertools::Itertools;
use smol_str::SmolStr;

use crate::compiler::compilers::{LibCompiler, StarknetContractCompiler, TestCompiler};
use crate::compiler::helpers::build_compiler_config;
use crate::compiler::{CompilationUnit, Compiler};
use crate::core::Workspace;

Expand Down Expand Up @@ -54,17 +53,6 @@ impl CompilerRepository {
};
compiler.compile(unit, db, ws)
}

pub fn check(
&self,
unit: CompilationUnit,
db: &mut RootDatabase,
ws: &Workspace<'_>,
) -> Result<()> {
let mut compiler_config = build_compiler_config(&unit, ws);
compiler_config.diagnostics_reporter.ensure(db)?;
Ok(())
}
}

impl fmt::Debug for CompilerRepository {
Expand Down
95 changes: 0 additions & 95 deletions scarb/src/ops/check.rs

This file was deleted.

101 changes: 81 additions & 20 deletions scarb/src/ops/compile.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use anyhow::{anyhow, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsError;
use indoc::formatdoc;

use scarb_ui::components::Status;
use scarb_ui::HumanDuration;

use crate::compiler::db::{build_scarb_root_database, has_starknet_plugin};
use crate::compiler::helpers::build_compiler_config;
use crate::compiler::CompilationUnit;
use crate::core::{PackageId, TargetKind, Utf8PathWorkspaceExt, Workspace};
use crate::core::{PackageId, PackageName, TargetKind, Utf8PathWorkspaceExt, Workspace};
use crate::ops;

#[derive(Debug)]
Expand All @@ -18,6 +20,25 @@ pub struct CompileOpts {

#[tracing::instrument(skip_all, level = "debug")]
pub fn compile(packages: Vec<PackageId>, opts: CompileOpts, ws: &Workspace<'_>) -> Result<()> {
process(packages, opts, ws, compile_unit, None)
}

#[tracing::instrument(skip_all, level = "debug")]
pub fn check(packages: Vec<PackageId>, opts: CompileOpts, ws: &Workspace<'_>) -> Result<()> {
process(packages, opts, ws, check_unit, Some("checking"))
}

#[tracing::instrument(skip_all, level = "debug")]
fn process<F>(
packages: Vec<PackageId>,
opts: CompileOpts,
ws: &Workspace<'_>,
mut operation: F,
operation_type: Option<&str>,
) -> Result<()>
where
F: FnMut(CompilationUnit, &Workspace<'_>) -> Result<()>,
{
let resolve = ops::resolve_workspace(ws)?;

// Add test compilation units to build
Expand Down Expand Up @@ -46,14 +67,17 @@ pub fn compile(packages: Vec<PackageId>, opts: CompileOpts, ws: &Workspace<'_>)
.collect::<Vec<_>>();

for unit in compilation_units {
compile_unit(unit, ws)?;
operation(unit, ws)?;
}

let elapsed_time = HumanDuration(ws.config().elapsed_time());
ws.config().ui().print(Status::new(
"Finished",
&format!("release target(s) in {elapsed_time}"),
));
let formatted_message = match operation_type {
Some(op) => format!("{op} release target(s) in {elapsed_time}"),
None => format!("release target(s) in {elapsed_time}"),
};
ws.config()
.ui()
.print(Status::new("Finished", &formatted_message));

Ok(())
}
Expand All @@ -67,6 +91,56 @@ fn compile_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {

let mut db = build_scarb_root_database(&unit, ws)?;

check_starknet_dependency(&unit, ws, &db, &package_name);

ws.config()
.compilers()
.compile(unit, &mut db, ws)
.map_err(|err| {
if !suppress_error(&err) {
ws.config().ui().anyhow(&err);
}

anyhow!("could not compile `{package_name}` due to previous error")
})?;

Ok(())
}

fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
let package_name = unit.main_package_id.name.clone();

ws.config()
.ui()
.print(Status::new("Checking", &unit.name()));

let db = build_scarb_root_database(&unit, ws)?;

check_starknet_dependency(&unit, ws, &db, &package_name);

let mut compiler_config = build_compiler_config(&unit, ws);

compiler_config
.diagnostics_reporter
.ensure(&db)
.map_err(|err| {
let valid_error = err.into();
if !suppress_error(&valid_error) {
ws.config().ui().anyhow(&valid_error);
}

anyhow!("could not check `{package_name}` due to previous error")
})?;

Ok(())
}

fn check_starknet_dependency(
unit: &CompilationUnit,
ws: &Workspace<'_>,
db: &RootDatabase,
package_name: &PackageName,
) {
// NOTE: This is a special case that can be hit frequently by newcomers. Not specifying
// `starknet` dependency will error in 99% real-world Starknet contract projects.
// I think we can get away with emitting false positives for users who write raw contracts
Expand All @@ -85,21 +159,8 @@ fn compile_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
cairo_version = crate::version::get().cairo.version,
})
}

ws.config()
.compilers()
.compile(unit, &mut db, ws)
.map_err(|err| {
if !suppress_error(&err) {
ws.config().ui().anyhow(&err);
}

anyhow!("could not compile `{package_name}` due to previous error")
})?;

Ok(())
}

pub fn suppress_error(err: &anyhow::Error) -> bool {
fn suppress_error(err: &anyhow::Error) -> bool {
matches!(err.downcast_ref(), Some(&DiagnosticsError))
}
2 changes: 0 additions & 2 deletions scarb/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! For datastructures describing the state, see [`crate::core`] module.
pub use cache::*;
pub use check::*;
pub use clean::*;
pub use compile::*;
pub use fmt::*;
Expand All @@ -18,7 +17,6 @@ pub use subcommands::*;
pub use workspace::*;

mod cache;
mod check;
mod clean;
mod compile;
mod fmt;
Expand Down
23 changes: 18 additions & 5 deletions scarb/tests/build_starknet_contract_allowed_libfuncs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ fn default_behaviour() {
.lib_cairo(EXPERIMENTAL_LIBFUNC)
.build(&t);

// TODO this warning can only be achieved by using `build`, not `check`
// should we be worried about this?
Scarb::quick_snapbox()
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
.arg("build")
.current_dir(&t)
.assert()
Expand Down Expand Up @@ -105,13 +105,16 @@ fn check_false() {
.build(&t);

Scarb::quick_snapbox()
.arg("check")
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
// Also, `check` subcommand would not check if the libfuncs warning doesn't appear
.arg("build")
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Checking hello v0.1.0 ([..])
[..] Finished checking release target(s) in [..]
[..] Compiling hello v0.1.0 ([..])
[..] Finished release target(s) in [..]
"#});
}

Expand All @@ -136,6 +139,8 @@ fn deny_true() {
.build(&t);

Scarb::quick_snapbox()
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
.arg("build")
.current_dir(&t)
.assert()
Expand Down Expand Up @@ -165,6 +170,8 @@ fn pass_named_list() {
.build(&t);

Scarb::quick_snapbox()
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
.arg("build")
.current_dir(&t)
.assert()
Expand All @@ -190,6 +197,8 @@ fn unknown_list_name() {
.build(&t);

Scarb::quick_snapbox()
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
.arg("build")
.current_dir(&t)
.assert()
Expand Down Expand Up @@ -224,6 +233,8 @@ fn list_path() {
.build(&t);

Scarb::quick_snapbox()
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
.arg("build")
.current_dir(&t)
.assert()
Expand Down Expand Up @@ -252,6 +263,8 @@ fn list_path_does_not_exist() {
.build(&t);

Scarb::quick_snapbox()
// NOTE: we cannot use `check` here, because without full compilation
// we cannot predict what libfuncs would be generated
.arg("build")
.current_dir(&t)
.assert()
Expand Down

0 comments on commit ea63ee9

Please sign in to comment.