From 7b92e31f5b1d68b2338ff85967f49f5d6f95593f Mon Sep 17 00:00:00 2001 From: maciektr Date: Wed, 12 Jun 2024 21:36:18 +0200 Subject: [PATCH] Allow expanding erroneous code commit-id:8156f337 --- scarb/src/ops/expand.rs | 20 +++----------------- scarb/tests/expand.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/scarb/src/ops/expand.rs b/scarb/src/ops/expand.rs index 38e895816..62281c60b 100644 --- a/scarb/src/ops/expand.rs +++ b/scarb/src/ops/expand.rs @@ -4,8 +4,7 @@ use crate::compiler::{CairoCompilationUnit, CompilationUnit, CompilationUnitAttr use crate::core::{Package, TargetKind, Workspace}; use crate::ops; use crate::ops::{get_test_package_ids, FeaturesOpts}; -use anyhow::{anyhow, bail, Context, Result}; -use cairo_lang_compiler::diagnostics::DiagnosticsError; +use anyhow::{bail, Context, Result}; use cairo_lang_defs::db::DefsGroup; use cairo_lang_defs::ids::{LanguageElementId, ModuleId, ModuleItemId}; use cairo_lang_defs::patcher::PatchBuilder; @@ -150,17 +149,8 @@ fn do_expand( ) -> Result<()> { let ScarbDatabase { db, .. } = build_scarb_root_database(compilation_unit, ws)?; let mut compiler_config = build_compiler_config(compilation_unit, ws); - compiler_config - .diagnostics_reporter - .ensure(&db) - .map_err(|err| err.into()) - .map_err(|err| { - if !suppress_error(&err) { - ws.config().ui().anyhow(&err); - } - - anyhow!("could not check due to previous error") - })?; + // Report diagnostics, but do not fail. + let _ = compiler_config.diagnostics_reporter.check(&db); let main_crate_id = db.intern_crate(CrateLongId::Real( compilation_unit.main_component().cairo_package_name(), )); @@ -241,7 +231,3 @@ fn format_cairo(content: String) -> Option { FormatOutcome::DiffFound(diff) => diff.formatted, }) } - -fn suppress_error(err: &anyhow::Error) -> bool { - matches!(err.downcast_ref(), Some(&DiagnosticsError)) -} diff --git a/scarb/tests/expand.rs b/scarb/tests/expand.rs index 769c47149..636be2137 100644 --- a/scarb/tests/expand.rs +++ b/scarb/tests/expand.rs @@ -385,3 +385,42 @@ fn can_skip_formatting() { expanded, ); } + +#[test] +fn can_expand_erroneous_code() { + let t = TempDir::new().unwrap(); + ProjectBuilder::start() + .name("hello") + // Missing opening bracket. + .lib_cairo(indoc! {r#" + fn hello() -> felt252 { + 0 + + "#}) + .build(&t); + Scarb::quick_snapbox() + .arg("expand") + .current_dir(&t) + .assert() + .success() + .stdout_matches(indoc! {r#" + error: Missing token TerminalRBrace. + --> [..]lib.cairo:2:6 + 0 + ^ + + "#}); + assert_eq!(t.child("target").files(), vec!["CACHEDIR.TAG", "dev"]); + assert_eq!(t.child("target/dev").files(), vec!["hello.expanded.cairo"]); + let expanded = t.child("target/dev/hello.expanded.cairo").read_to_string(); + snapbox::assert_eq( + indoc! {r#" + + mod hello { + fn hello() -> felt252 { + 0 + } + "#}, + expanded, + ); +}