Skip to content

Commit

Permalink
fix: panic for runtime error in TS compiler (denoland#6758)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jul 15, 2020
1 parent cde4dbb commit 73a9036
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions cli/tests/compiler_js_error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deno.compile("main.js", { "main.js": "console.log(foo);" });
7 changes: 7 additions & 0 deletions cli/tests/compiler_js_error.ts.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Check [WILDCARD]compiler_js_error.ts
error: Uncaught Error: Error in TS compiler:
Uncaught AssertionError: Unexpected skip of the emit.
[WILDCARD]
at unwrapResponse ($deno$/ops/dispatch_json.ts:[WILDCARD])
at Object.sendAsync ($deno$/ops/dispatch_json.ts:[WILDCARD])
at async Object.compile ($deno$/compiler_api.ts:[WILDCARD])
6 changes: 6 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,12 @@ itest!(deno_lint_glob {
exit_code: 1,
});

itest!(compiler_js_error {
args: "run --unstable compiler_js_error.ts",
output: "compiler_js_error.ts.out",
exit_code: 1,
});

#[test]
fn cafile_env_fetch() {
use url::Url;
Expand Down
25 changes: 22 additions & 3 deletions cli/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::doc::Location;
use crate::file_fetcher::SourceFile;
use crate::file_fetcher::SourceFileFetcher;
use crate::flags::Flags;
use crate::fmt_errors::JSError;
use crate::global_state::GlobalState;
use crate::module_graph::ModuleGraph;
use crate::module_graph::ModuleGraphLoader;
Expand Down Expand Up @@ -1160,6 +1161,18 @@ async fn create_runtime_module_graph(
Ok((root_names, module_graph_loader.get_graph()))
}

/// Because TS compiler can raise runtime error, we need to
/// manually convert formatted JSError into and OpError.
fn js_error_to_op_error(error: ErrBox) -> OpError {
match error.downcast::<JSError>() {
Ok(js_error) => {
let msg = format!("Error in TS compiler:\n{}", js_error);
OpError::other(msg)
}
Err(error) => error.into(),
}
}

/// This function is used by `Deno.compile()` API.
pub async fn runtime_compile(
global_state: GlobalState,
Expand Down Expand Up @@ -1193,7 +1206,9 @@ pub async fn runtime_compile(

let compiler = global_state.ts_compiler.clone();

let msg = execute_in_same_thread(global_state, permissions, req_msg).await?;
let msg = execute_in_same_thread(global_state, permissions, req_msg)
.await
.map_err(js_error_to_op_error)?;
let json_str = std::str::from_utf8(&msg).unwrap();

let response: RuntimeCompileResponse = serde_json::from_str(json_str)?;
Expand Down Expand Up @@ -1239,7 +1254,9 @@ pub async fn runtime_bundle(
.into_boxed_str()
.into_boxed_bytes();

let msg = execute_in_same_thread(global_state, permissions, req_msg).await?;
let msg = execute_in_same_thread(global_state, permissions, req_msg)
.await
.map_err(js_error_to_op_error)?;
let json_str = std::str::from_utf8(&msg).unwrap();
let _response: RuntimeBundleResponse = serde_json::from_str(json_str)?;
// We're returning `Ok()` instead of `Err()` because it's not runtime
Expand All @@ -1264,7 +1281,9 @@ pub async fn runtime_transpile(
.into_boxed_str()
.into_boxed_bytes();

let msg = execute_in_same_thread(global_state, permissions, req_msg).await?;
let msg = execute_in_same_thread(global_state, permissions, req_msg)
.await
.map_err(js_error_to_op_error)?;
let json_str = std::str::from_utf8(&msg).unwrap();
let v = serde_json::from_str::<serde_json::Value>(json_str)
.expect("Error decoding JSON string.");
Expand Down

0 comments on commit 73a9036

Please sign in to comment.