From e70341e65e9f4e44811210c9b24e67a29b2c497a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 25 Oct 2024 13:56:40 -0400 Subject: [PATCH] fix(check): ignore resolving `jsxImportSource` when jsx is not used in graph (#26548) --- cli/tsc/mod.rs | 46 ++++++++++++++++--- .../__test__.jsonc | 4 ++ .../jsx_import_source_not_in_graph/deno.json | 6 +++ .../jsx_import_source_not_in_graph/main.out | 1 + .../jsx_import_source_not_in_graph/main.ts | 1 + 5 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 tests/specs/check/jsx_import_source_not_in_graph/__test__.jsonc create mode 100644 tests/specs/check/jsx_import_source_not_in_graph/deno.json create mode 100644 tests/specs/check/jsx_import_source_not_in_graph/main.out create mode 100644 tests/specs/check/jsx_import_source_not_in_graph/main.ts diff --git a/cli/tsc/mod.rs b/cli/tsc/mod.rs index 0e3387494a..de91889b62 100644 --- a/cli/tsc/mod.rs +++ b/cli/tsc/mod.rs @@ -5,6 +5,7 @@ use crate::args::TypeCheckMode; use crate::cache::FastInsecureHasher; use crate::node; use crate::npm::CliNpmResolver; +use crate::npm::ResolvePkgFolderFromDenoReqError; use crate::util::checksum; use crate::util::path::mapped_specifier_for_tsc; @@ -35,6 +36,7 @@ use deno_runtime::deno_node::NodeResolver; use deno_semver::npm::NpmPackageReqReference; use node_resolver::errors::NodeJsErrorCode; use node_resolver::errors::NodeJsErrorCoded; +use node_resolver::errors::ResolvePkgSubpathFromDenoModuleError; use node_resolver::NodeModuleKind; use node_resolver::NodeResolution; use node_resolver::NodeResolutionMode; @@ -45,6 +47,7 @@ use std::fmt; use std::path::Path; use std::path::PathBuf; use std::sync::Arc; +use thiserror::Error; mod diagnostics; @@ -688,12 +691,30 @@ fn op_resolve_inner( Some(ResolutionResolved { specifier, .. }) => { resolve_graph_specifier_types(specifier, &referrer, state)? } - _ => resolve_non_graph_specifier_types( - &specifier, - &referrer, - referrer_kind, - state, - )?, + _ => { + match resolve_non_graph_specifier_types( + &specifier, + &referrer, + referrer_kind, + state, + ) { + Ok(maybe_result) => maybe_result, + Err( + err @ ResolveNonGraphSpecifierTypesError::ResolvePkgFolderFromDenoReq( + ResolvePkgFolderFromDenoReqError::Managed(_), + ), + ) => { + // it's most likely requesting the jsxImportSource, which isn't loaded + // into the graph when not using jsx, so just ignore this error + if specifier.ends_with("/jsx-runtime") { + None + } else { + return Err(err.into()); + } + } + Err(err) => return Err(err.into()), + } + } }; let result = match maybe_result { Some((specifier, media_type)) => { @@ -818,12 +839,23 @@ fn resolve_graph_specifier_types( } } +#[derive(Debug, Error)] +enum ResolveNonGraphSpecifierTypesError { + #[error(transparent)] + ResolvePkgFolderFromDenoReq(#[from] ResolvePkgFolderFromDenoReqError), + #[error(transparent)] + ResolvePkgSubpathFromDenoModule(#[from] ResolvePkgSubpathFromDenoModuleError), +} + fn resolve_non_graph_specifier_types( raw_specifier: &str, referrer: &ModuleSpecifier, referrer_kind: NodeModuleKind, state: &State, -) -> Result, AnyError> { +) -> Result< + Option<(ModuleSpecifier, MediaType)>, + ResolveNonGraphSpecifierTypesError, +> { let npm = match state.maybe_npm.as_ref() { Some(npm) => npm, None => return Ok(None), // we only support non-graph types for npm packages diff --git a/tests/specs/check/jsx_import_source_not_in_graph/__test__.jsonc b/tests/specs/check/jsx_import_source_not_in_graph/__test__.jsonc new file mode 100644 index 0000000000..de0339cfb4 --- /dev/null +++ b/tests/specs/check/jsx_import_source_not_in_graph/__test__.jsonc @@ -0,0 +1,4 @@ +{ + "args": "check main.ts", + "output": "main.out" +} diff --git a/tests/specs/check/jsx_import_source_not_in_graph/deno.json b/tests/specs/check/jsx_import_source_not_in_graph/deno.json new file mode 100644 index 0000000000..219ef515f6 --- /dev/null +++ b/tests/specs/check/jsx_import_source_not_in_graph/deno.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "npm:preact" + } +} diff --git a/tests/specs/check/jsx_import_source_not_in_graph/main.out b/tests/specs/check/jsx_import_source_not_in_graph/main.out new file mode 100644 index 0000000000..1830c3186d --- /dev/null +++ b/tests/specs/check/jsx_import_source_not_in_graph/main.out @@ -0,0 +1 @@ +Check file:///[WILDLINE]/main.ts diff --git a/tests/specs/check/jsx_import_source_not_in_graph/main.ts b/tests/specs/check/jsx_import_source_not_in_graph/main.ts new file mode 100644 index 0000000000..bf6b817963 --- /dev/null +++ b/tests/specs/check/jsx_import_source_not_in_graph/main.ts @@ -0,0 +1 @@ +console.log("Hello");