Repository navigation
Port transpileModule, transpileDeclaration - #4849
Conversation
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Pull request overview
Adds single-file JavaScript and declaration transpilation to the native compiler API.
Changes:
- Implements transpilation with diagnostics, source maps, cancellation, and minimal declaration libraries.
- Exposes synchronous and asynchronous string/file APIs.
- Clarifies builder-only declaration emit naming and adds coverage.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
internal/transpile/transpile.go |
Implements transpilation. |
internal/transpile/transpile_test.go |
Tests core behavior. |
internal/execute/incremental/affectedfileshandler.go |
Uses renamed signature emit mode. |
internal/compiler/program.go |
Updates emit-mode handling. |
internal/compiler/emitter.go |
Renames builder signature mode. |
internal/api/session.go |
Handles transpilation requests. |
internal/api/proto.go |
Defines protocol methods and types. |
_packages/native-preview/test/sync/api.test.ts |
Tests synchronous APIs. |
_packages/native-preview/test/async/api.test.ts |
Tests asynchronous APIs. |
_packages/native-preview/src/api/sync/api.ts |
Exposes generated synchronous APIs. |
_packages/native-preview/src/api/async/api.ts |
Exposes asynchronous APIs. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| EmitOnlyJs | ||
| EmitOnlyDts | ||
| EmitOnlyForcedDts | ||
| EmitOnlyBuilderSignature |
There was a problem hiding this comment.
I renamed this internal enum member because it's intended to be used as an opaque description of what the builder needs, which is specifically .d.ts files without declaration maps. The other ForceDTS option (inherited from Strada) includes declaration maps.
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| func TestTranspileModule(t *testing.T) { |
There was a problem hiding this comment.
Can we port over the transpile test runner instead of relying on unit tests?
There was a problem hiding this comment.
This exposed a couple real emit diffs, which I don’t have time to address here before the beta.
) Co-authored-by: Copilot <[email protected]>
|
Hey Andrew Branch (@andrewbranch), With Claude's help I've started using these APIs in ts-loader as of this commit: TypeStrong/ts-loader@3b5af16 It was pretty much plug-and-go, so fine work on your behalf! One comment in the migration report from Claude that caught my eye was this:
Does this sound meaningful to you? See https://github.com/TypeStrong/ts-loader/blob/3b5af168c8b3fe85843854f095ecf318666125f3/src/typeScriptApi.ts#L403-L414 for a little more context. I asked Claude to come up with a meaningful / actionable explanation to share and it gave me the below
|
compilerOptions passed to transpileModule |
Result |
|---|---|
{ declaration: true } |
✅ no diagnostics |
{ declaration: true, isolatedDeclarations: true } |
❌ TS5069 (wrong — declaration is set) |
{ composite: true, isolatedDeclarations: true } |
❌ TS5069 (wrong — composite is set) |
{ isolatedDeclarations: true } (no declaration/composite) |
✅ TS5069 (correct — this one's a real error) |
So the check fires unconditionally whenever isolatedDeclarations: true is present, regardless of what declaration/composite are set to — it's not reading the caller's actual values.
Control: transpileDeclaration does not have this bug
const result = api.transpileDeclaration(contents, {
compilerOptions: { declaration: true, isolatedDeclarations: true },
fileName: '/tmp/app.ts',
reportDiagnostics: true,
});
// diagnostics: none
// outputText: "export declare function add(a: number, b: number): number;\n"Same options object, correct result. This rules out a general options-validation bug and points specifically at transpileModule.
Hypothesis (unconfirmed — this is compiled into the native tsgo binary, so I can't inspect the actual check)
transpileModule is a JS-only, single-file transform — it never emits declarations regardless of what the caller passes. My guess is it internally normalizes/clears declaration and composite to false before running the "does this project even want declarations" validation (since it doesn't intend to emit any), and that reset happens before the isolatedDeclarations-requires-declaration-or-composite check runs — so the check sees declaration: false even when the caller passed true. That would explain every row above, including why transpileDeclaration (which does care about declaration) is unaffected.
Impact
Blocks any tool trying to pass through a project's real compilerOptions unmodified to transpileModule when the project has isolatedDeclarations enabled — which is a completely ordinary, common configuration (declaration emit + isolatedDeclarations is the whole point of the option). The caller has to know to strip isolatedDeclarations specifically before calling transpileModule, which isn't discoverable without hitting this error first.
Workaround in use
Stripping isolatedDeclarations from the options object before the transpileModule call (it's irrelevant to JS emit anyway), while still passing it through unmodified to transpileDeclaration.
|
Thanks John! Sounds like a bug, will take a look. |
|
Thanks Andrew Branch (@andrewbranch)! Something that may be interesting / relevant or may not. When fixing comparison tests for Windows Claude picked up panic. I've got it to write up a hopefully helpful description:
|
fileName passed to transpileDeclaration |
Result |
|---|---|
OS-native absolute (C:\Users\...\app.ts) |
❌ panics |
Forward-slash-normalized absolute (C:/Users/...\app.ts → C:/Users/.../app.ts) |
❌ panics (identical stack) |
Forward-slash absolute + compilerOptions.configFilePath stripped |
❌ panics (identical stack) |
Forward-slash absolute + an explicit compilerOptions.rootDir set to the file's own parent directory |
❌ panics (identical stack) |
Bare basename (app.ts, no path at all) |
✅ works |
transpileModule, called with the exact same absolute, forward-slash fileName value (the second row above), works correctly on Windows and produces correct diagnostics — including ones that need real directory context, like TS6059 ("File is not under 'rootDir'"). So this isn't a "don't pass absolute paths" API contract — transpileModule clearly supports and needs them. The bug looks specific to transpileDeclaration's own path handling (per the stack trace, inside transpileWorker's use of vfstest.FromMap — plausibly deriving a declaration output path internally and mixing an OS-native path, maybe the server process's own working directory, with the forward-slash fileName we passed).
Impact
Any project on Windows using transpileDeclaration with a real absolute file path — the natural, expected way to call it, matching transpileModule's own working usage — crashes instead of transpiling. There's no way to discover the workaround (using a bare basename instead) without significant trial and error, and doing so silently drops any directory-dependent diagnostics transpileDeclaration might otherwise produce (not a problem for us specifically, since we also call transpileModule on the same file and it already covers rootDir-containment checks, but a caller using transpileDeclaration in isolation would lose that coverage entirely with no signal that anything is missing).
Workaround in use
Passing a bare basename (e.g. "app.ts") as fileName to transpileDeclaration specifically, while transpileModule keeps the real absolute path (which it needs and handles correctly).
In the TS API, these are exposed as
transpileModule(input: string, options?: TranspileOptions)transpileModuleFromFile(fileName: string, options?: TranspileOptions)transpileDeclaration(input: string, options?: TranspileOptions)transpileDeclarationFromFile(fileName: string, options?: TranspileOptions)Part of microsoft/TypeScript#63875 (cc John Reilly (@johnnyreilly))