From 8a367d3cc3d01ec3fd9f3f8033c9ff6cee59cc62 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Tue, 23 Apr 2024 08:50:50 -0700 Subject: [PATCH] fix(cli): Don't panic on invalid emit options (#23463) Fixes #23456. --- cli/args/mod.rs | 9 +++++---- cli/factory.rs | 2 +- cli/tools/bundle.rs | 2 +- cli/tools/compile.rs | 2 +- cli/tools/repl/session.rs | 2 +- tests/specs/run/invalid_emit_options/__test__.jsonc | 5 +++++ tests/specs/run/invalid_emit_options/deno.json | 5 +++++ tests/specs/run/invalid_emit_options/main.out | 4 ++++ tests/specs/run/invalid_emit_options/main.ts | 0 9 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 tests/specs/run/invalid_emit_options/__test__.jsonc create mode 100644 tests/specs/run/invalid_emit_options/deno.json create mode 100644 tests/specs/run/invalid_emit_options/main.out create mode 100644 tests/specs/run/invalid_emit_options/main.ts diff --git a/cli/args/mod.rs b/cli/args/mod.rs index 6c84abac20..aa3622d096 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -150,9 +150,10 @@ pub fn jsr_api_url() -> &'static Url { pub fn ts_config_to_transpile_and_emit_options( config: deno_config::TsConfig, -) -> (deno_ast::TranspileOptions, deno_ast::EmitOptions) { +) -> Result<(deno_ast::TranspileOptions, deno_ast::EmitOptions), AnyError> { let options: deno_config::EmitConfigOptions = - serde_json::from_value(config.0).unwrap(); + serde_json::from_value(config.0) + .context("Failed to parse compilerOptions")?; let imports_not_used_as_values = match options.imports_not_used_as_values.as_str() { "preserve" => deno_ast::ImportsNotUsedAsValues::Preserve, @@ -174,7 +175,7 @@ pub fn ts_config_to_transpile_and_emit_options( } else { SourceMapOption::None }; - ( + Ok(( deno_ast::TranspileOptions { use_ts_decorators: options.experimental_decorators, use_decorators_proposal: !options.experimental_decorators, @@ -195,7 +196,7 @@ pub fn ts_config_to_transpile_and_emit_options( keep_comments: false, source_map, }, - ) + )) } /// Indicates how cached source files should be handled. diff --git a/cli/factory.rs b/cli/factory.rs index 2d685ce76e..1a0584eeac 100644 --- a/cli/factory.rs +++ b/cli/factory.rs @@ -563,7 +563,7 @@ impl CliFactory { let (transpile_options, emit_options) = crate::args::ts_config_to_transpile_and_emit_options( ts_config_result.ts_config, - ); + )?; Ok(Arc::new(Emitter::new( self.emit_cache()?.clone(), self.parsed_source_cache().clone(), diff --git a/cli/tools/bundle.rs b/cli/tools/bundle.rs index e8b9076ea3..7701b60248 100644 --- a/cli/tools/bundle.rs +++ b/cli/tools/bundle.rs @@ -147,7 +147,7 @@ fn bundle_module_graph( let (transpile_options, emit_options) = crate::args::ts_config_to_transpile_and_emit_options( ts_config_result.ts_config, - ); + )?; deno_emit::bundle_graph( graph, deno_emit::BundleOptions { diff --git a/cli/tools/compile.rs b/cli/tools/compile.rs index 75572cd427..7f31b90358 100644 --- a/cli/tools/compile.rs +++ b/cli/tools/compile.rs @@ -76,7 +76,7 @@ pub async fn compile( let (transpile_options, emit_options) = crate::args::ts_config_to_transpile_and_emit_options( ts_config_for_emit.ts_config, - ); + )?; let parser = parsed_source_cache.as_capturing_parser(); let eszip = eszip::EszipV2::from_graph( graph, diff --git a/cli/tools/repl/session.rs b/cli/tools/repl/session.rs index 8aa632949f..671dad4b50 100644 --- a/cli/tools/repl/session.rs +++ b/cli/tools/repl/session.rs @@ -255,7 +255,7 @@ impl ReplSession { let (transpile_options, _) = crate::args::ts_config_to_transpile_and_emit_options( ts_config_for_emit.ts_config, - ); + )?; let experimental_decorators = transpile_options.use_ts_decorators; let mut repl_session = ReplSession { npm_resolver, diff --git a/tests/specs/run/invalid_emit_options/__test__.jsonc b/tests/specs/run/invalid_emit_options/__test__.jsonc new file mode 100644 index 0000000000..7b5c5e1b67 --- /dev/null +++ b/tests/specs/run/invalid_emit_options/__test__.jsonc @@ -0,0 +1,5 @@ +{ + "args": "run main.ts", + "output": "main.out", + "exitCode": 1 +} diff --git a/tests/specs/run/invalid_emit_options/deno.json b/tests/specs/run/invalid_emit_options/deno.json new file mode 100644 index 0000000000..b50b3c6506 --- /dev/null +++ b/tests/specs/run/invalid_emit_options/deno.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "jsx": 1234 + } +} diff --git a/tests/specs/run/invalid_emit_options/main.out b/tests/specs/run/invalid_emit_options/main.out new file mode 100644 index 0000000000..21a2735d8c --- /dev/null +++ b/tests/specs/run/invalid_emit_options/main.out @@ -0,0 +1,4 @@ +error: Failed to parse compilerOptions + +Caused by: + invalid type: integer `1234`, expected a string diff --git a/tests/specs/run/invalid_emit_options/main.ts b/tests/specs/run/invalid_emit_options/main.ts new file mode 100644 index 0000000000..e69de29bb2