Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return compilation errors from noir_wasm #3091

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 compiler/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fm.workspace = true
nargo.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
noirc_errors.workspace = true
wasm-bindgen.workspace = true
serde.workspace = true
js-sys.workspace = true
Expand Down
8 changes: 6 additions & 2 deletions compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@

if contracts.unwrap_or_default() {
let compiled_contract = compile_contract(&mut context, crate_id, &compile_options)
.map_err(|_| JsCompileError::new("Failed to compile contract".to_string()))?
.map_err(|errs| {
JsCompileError::new("Failed to compile contract", errs, &context.file_manager)
})?
.0;

let optimized_contract =
Expand All @@ -68,7 +70,9 @@
Ok(<JsValue as JsValueSerdeExt>::from_serde(&preprocessed_contract).unwrap())
} else {
let compiled_program = compile_main(&mut context, crate_id, &compile_options, None, true)
.map_err(|_| JsCompileError::new("Failed to compile program".to_string()))?
.map_err(|errs| {
JsCompileError::new("Failed to compile program", errs, &context.file_manager)
})?
.0;

let optimized_program =
Expand Down Expand Up @@ -140,7 +144,7 @@
}

cfg_if::cfg_if! {
if #[cfg(target_os = "wasi")] {

Check warning on line 147 in compiler/wasm/src/compile.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (wasi)
fn get_non_stdlib_asset(path_to_file: &Path) -> std::io::Result<String> {
std::fs::read_to_string(path_to_file)
}
Expand Down
94 changes: 75 additions & 19 deletions compiler/wasm/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,85 @@
use js_sys::JsString;

use gloo_utils::format::JsValueSerdeExt;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;

use fm::FileManager;
use noirc_errors::FileDiagnostic;

#[wasm_bindgen(typescript_custom_section)]
const COMPILE_ERROR: &'static str = r#"
export type CompileError = Error;
const DIAGNOSTICS: &'static str = r#"
export type Diagnostic = {
message: string;
file_path: string;
secondaries: ReadonlyArray<{
message: string;
start: number;
end: number;
}>;
}

interface CompileError {
diagnostics: ReadonlyArray<Diagnostic>;
}
"#;

/// `CompileError` is a raw js error.
/// It'd be ideal that `CompileError` was a subclass of `Error`, but for that we'd need to use JS snippets or a js module.
/// Currently JS snippets don't work with a nodejs target. And a module would be too much for just a custom error type.
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = js_sys::Error, js_name = "CompileError", typescript_type = "CompileError")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type JsCompileError;

#[wasm_bindgen(constructor, js_class = "Error")]
fn constructor(message: JsString) -> JsCompileError;
#[derive(Serialize, Deserialize)]
struct JsDiagnosticLabel {
message: String,
start: u32,
end: u32,
}

#[derive(Serialize, Deserialize)]
struct JsDiagnostic {
message: String,
file_path: String,
secondaries: Vec<JsDiagnosticLabel>,
}

impl JsDiagnostic {
fn new(file_diagnostic: &FileDiagnostic, file_path: String) -> JsDiagnostic {
let diagnostic = &file_diagnostic.diagnostic;
let message = diagnostic.message.clone();

let secondaries = diagnostic
.secondaries
.iter()
.map(|label| JsDiagnosticLabel {
message: label.message.clone(),
start: label.span.start(),
end: label.span.end(),
})
.collect();

JsDiagnostic { message, file_path, secondaries }
}
}

#[wasm_bindgen(getter_with_clone, js_name = "CompileError")]
pub struct JsCompileError {
pub message: js_sys::JsString,
pub diagnostics: JsValue,
}

impl JsCompileError {
/// Creates a new execution error with the given call stack.
/// Call stacks won't be optional in the future, after removing ErrorLocation in ACVM.
pub fn new(message: String) -> Self {
JsCompileError::constructor(JsString::from(message))
pub fn new(
message: &str,
file_diagnostics: Vec<FileDiagnostic>,
file_manager: &FileManager,
) -> JsCompileError {
let diagnostics: Vec<_> = file_diagnostics
.iter()
.map(|err| {
JsDiagnostic::new(
&err,
file_manager.path(err.file_id).to_str().unwrap().to_string(),
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
)
})
.collect();

JsCompileError {
message: js_sys::JsString::from(message.to_string()),
diagnostics: <JsValue as JsValueSerdeExt>::from_serde(&diagnostics).unwrap(),
}
}
}
Loading