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

[bug] JSX support in `preserve` mode w/ custom `jsxImportSource` causing `TS2875`

Open tmm1 opened this issue 8 months ago • 8 comments

I have a tsconfig.json approximately like:

"compilerOptions": {
        "module": "es2022",
        "jsx": "preserve",
        "jsxImportSource": "external/solid",
        "baseUrl": ".",
        "paths": {
                "external/*": ["./external/*"]
        },
        "target": "es2022",
error TS2875: This JSX tag requires the module path 'external/solid/jsx-runtime' to exist, but none could be found. Make sure you have types for the appropriate package installed.

even though this file exists:

❯ cat external/solid/jsx-runtime.d.ts
export * from "./solid/jsx";

❯ ls -l external/solid/solid/jsx.d.ts
-rw-r--r--  1 tmm1  staff  73590 Apr  4 17:34 external/solid/solid/jsx.d.ts

Is this due to compilerOptions.paths?

tmm1 avatar Apr 07 '25 22:04 tmm1

baseUrl is not supported: #474

jakebailey avatar Apr 07 '25 22:04 jakebailey

Thanks. I removed baseUrl, and also tried to add paths['*'] = ['*'], but the behavior remains the same.

tmm1 avatar Apr 08 '25 00:04 tmm1

Ah, well I don't think jsxImportSource has been implemented yet.

jakebailey avatar Apr 08 '25 00:04 jakebailey

The error comes from:

https://github.com/microsoft/typescript-go/blob/34dd1030316c6f25c5af8fdf2bef671fa0413a8d/internal/checker/jsx.go#L1249-L1259

where getJSXImplicitImportBase does plumb the value of jsximportsource through. So the issue is likely in resolveExternalModule here?

https://github.com/microsoft/typescript-go/blob/34dd1030316c6f25c5af8fdf2bef671fa0413a8d/internal/checker/checker.go#L14208-L14209

	// !!! The following only implements simple module resolution
	sourceFile := c.program.GetResolvedModule(ast.GetSourceFileOfNode(location), moduleReference)

tmm1 avatar Apr 08 '25 00:04 tmm1

There are some references to "the synthesized JSX runtime import" which I think may be missing on the Corsa side.

Atleast when I look at the program's resolvedModules, there is no entry for jsx-runtime which causes the failure.


Imports synthesized here:

https://github.com/microsoft/TypeScript/blob/0693cc72e6ddb6c7468a14e05888efa2f43ac7b0/src/compiler/program.ts#L3512-L3516

            const jsxImport = getJSXRuntimeImport(getJSXImplicitImportBase(options, file), options);
            if (jsxImport) {
                // synthesize `import "base/jsx-runtime"` declaration
                (imports ||= []).push(createSyntheticImport(jsxImport, file));
            }

and unimplemented here:

https://github.com/microsoft/typescript-go/blob/ce957e2f150a08e1de55caa4b7938db160b1e497/internal/parser/references.go#L11-L12

tmm1 avatar Apr 08 '25 01:04 tmm1

A little stuck because collectExternalModuleReferences is in internal/parser but the original version needs access to compiler options.

Also createSyntheticImport needs to be ported which is probably best done by someone familiar with the parser internals. A *ast.NodeFactory reference will be required for that function.

cc @ahejlsberg

tmm1 avatar Apr 08 '25 02:04 tmm1

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

jakebailey avatar Apr 09 '25 03:04 jakebailey

Works as advertised!

tmm1 avatar Apr 09 '25 03:04 tmm1