Skip to content

Commit

Permalink
Use severity aware diagnostic callback
Browse files Browse the repository at this point in the history
commit-id:ed123aeb
  • Loading branch information
maciektr committed Dec 1, 2023
1 parent 537e58a commit 51df1a2
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions scarb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ anyhow.workspace = true
async-trait.workspace = true
cairo-lang-compiler.workspace = true
cairo-lang-defs.workspace = true
cairo-lang-diagnostics.workspace = true
cairo-lang-filesystem.workspace = true
cairo-lang-formatter.workspace = true
cairo-lang-semantic.workspace = true
Expand Down
15 changes: 9 additions & 6 deletions scarb/src/compiler/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ use anyhow::{Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsReporter;
use cairo_lang_compiler::CompilerConfig;
use cairo_lang_diagnostics::Severity;
use cairo_lang_filesystem::db::FilesGroup;
use cairo_lang_filesystem::ids::{CrateId, CrateLongId};
use serde::Serialize;
use std::io::{BufWriter, Write};

use scarb_ui::components::TypedMessage;

use crate::compiler::CompilationUnit;
use crate::core::Workspace;
use crate::flock::Filesystem;

pub fn build_compiler_config<'c>(unit: &CompilationUnit, ws: &Workspace<'c>) -> CompilerConfig<'c> {
let diagnostics_reporter = DiagnosticsReporter::callback({
let config = ws.config();
|diagnostic: String| {
config
.ui()
.print(TypedMessage::naked_text("diagnostic", &diagnostic));

|severity: Severity, diagnostic: String| {
let msg = diagnostic.clone();
let msg = msg.strip_suffix('\n').unwrap_or(diagnostic.as_str());
match severity {
Severity::Error => config.ui().error(msg),
Severity::Warning => config.ui().warn(msg),
};
}
});
CompilerConfig {
Expand Down
10 changes: 3 additions & 7 deletions scarb/tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ fn compile_with_syntax_error() {
not_a_keyword
^***********^
error: could not compile `hello` due to previous error
"#});
}
Expand All @@ -102,7 +101,7 @@ fn compile_with_syntax_error_json() {
.code(1)
.stdout_matches(indoc! {r#"
{"status":"compiling","message":"hello v0.1.0 ([..]Scarb.toml)"}
{"type":"diagnostic","message":"error: Skipped tokens. Expected: Const/Module/Use/FreeFunction/ExternFunction/ExternType/Trait/Impl/Struct/Enum/TypeAlias/InlineMacro or an attribute./n --> [..]/lib.cairo:1:1/nnot_a_keyword/n^***********^/n/n"}
{"type":"error","message":"Skipped tokens. Expected: Const/Module/Use/FreeFunction/ExternFunction/ExternType/Trait/Impl/Struct/Enum/TypeAlias/InlineMacro or an attribute./n --> [..]/lib.cairo:1:1/nnot_a_keyword/n^***********^/n"}
{"type":"error","message":"could not compile `hello` due to previous error"}
"#});
}
Expand Down Expand Up @@ -720,7 +719,6 @@ fn dev_dep_used_outside_tests() {
use q::dev_dep_function;
^
error: could not compile `x` due to previous error
"#});
}
Expand Down Expand Up @@ -779,12 +777,11 @@ fn warnings_allowed_by_default() {
.success()
.stdout_matches(indoc! {r#"
[..] Compiling [..] v1.0.0 ([..]Scarb.toml)
warning: Unused variable. Consider ignoring by prefixing with `_`.
warn: Unused variable. Consider ignoring by prefixing with `_`.
--> [..]lib.cairo:2:9
let a = 41;
^
Finished release target(s) in [..] seconds
"#});
}
Expand Down Expand Up @@ -813,12 +810,11 @@ fn warnings_can_be_disallowed() {
.failure()
.stdout_matches(indoc! {r#"
[..] Compiling [..] v1.0.0 ([..]Scarb.toml)
warning: Unused variable. Consider ignoring by prefixing with `_`.
warn: Unused variable. Consider ignoring by prefixing with `_`.
--> [..]lib.cairo:2:9
let a = 41;
^
error: could not compile [..] due to previous error
"#});
}
2 changes: 0 additions & 2 deletions scarb/tests/build_starknet_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ fn compile_starknet_contract_without_starknet_dep() {
#[starknet::contract]
^*******************^
error: Plugin diagnostic: Unsupported attribute.
--> [..]/lib.cairo:13:5
#[storage]
Expand Down Expand Up @@ -414,7 +413,6 @@ fn compile_starknet_contract_without_starknet_dep() {
self.value.write( self.value.read() + a );
^***^
error: could not compile `hello` due to previous error
"#});
}
Expand Down
1 change: 0 additions & 1 deletion scarb/tests/build_targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ fn compile_dep_not_a_lib() {
fn hellp() -> felt252 { dep::forty_two() }
^*^
error: could not compile `hello` due to previous error
"#});
}
Expand Down
1 change: 0 additions & 1 deletion scarb/tests/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,6 @@ fn error_on_verification() {
.
^
error: failed to verify package tarball
Caused by:
Expand Down

0 comments on commit 51df1a2

Please sign in to comment.