typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

[bug] failing to resolve `react/jsx-runtime` when type checking TSX files

Open mjames-c opened this issue 9 months ago • 4 comments

Apologies if this is considered a duplicate of https://github.com/microsoft/typescript-go/issues/765 -- but I believe it is slightly different due to a lack of compilerOptions.jsx support in typescript-go (vs compilerOptions.jsxImportSource)...

The company I work for has a TS project with over 90k+ files. I recently ran tsgo against this project and was very impressed with how quick it was. However, it reporting over 38k errors when type checking our TSX files with form:

This JSX tag requires the module path 'react/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.
...
Found 41250 errors in 35304 files.

I believe this could be due to tsgo currently not supporting our usage of the compilerOptions.jsx tsconfig property so wanted to create an issue for tracking support.

{
  "compilerOptions": {
    "jsx": "react-jsx",
  }
}

mjames-c avatar Apr 08 '25 01:04 mjames-c

react/jsx-runtime

Does this file exist somewhere, under node_modules perhaps? Curious if its a .js or .d.ts or what

tmm1 avatar Apr 08 '25 01:04 tmm1

The proper fix is outlined in #765, but this may work as a hacky workaround:

diff --git i/internal/compiler/program.go w/internal/compiler/program.go
index afa7ae60b..75a0d9086 100644
--- i/internal/compiler/program.go
+++ w/internal/compiler/program.go
@@ -3,6 +3,7 @@ package compiler
 import (
        "fmt"
        "slices"
+       "strings"
        "sync"

        "github.com/microsoft/typescript-go/internal/ast"
@@ -252,6 +253,8 @@ func (p *Program) GetResolvedModule(file *ast.SourceFile, moduleReference string
        if resolutions, ok := p.resolvedModules[file.Path()]; ok {
                if resolved, ok := resolutions[module.ModeAwareCacheKey{Name: moduleReference, Mode: core.ModuleKindCommonJS}]; ok {
                        return p.findSourceFile(resolved.ResolvedFileName, FileIncludeReason{FileIncludeKindImport, 0})
+               } else if strings.HasSuffix(moduleReference, "solid/jsx-runtime") || strings.HasSuffix(moduleReference, "react/jsx-runtime") {
+                       return p.findSourceFile(moduleReference+".d.ts", FileIncludeReason{FileIncludeKindImport, 0})
                }
        }
        return nil

In my case I also had to add imports for JSX in some files because of the missing synthetic import.

tmm1 avatar Apr 08 '25 02:04 tmm1

Does this file exist somewhere, under node_modules perhaps? Curious if its a .js or .d.ts or what

@tmm1 the declaration file is distributed in the @types/react npm package (node_modules/@types/react/jsx-runtime.d.ts) and the runtime JS file is in the react npm package (node_modules/react/jsx-runtime.js)

mjames-c avatar Apr 08 '25 22:04 mjames-c

Stuck a draft on #780 if you could test that to see if it works.

jakebailey avatar Apr 09 '25 03:04 jakebailey