Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Port transpileModule, transpileDeclaration - #4849

Merged
Andrew Branch (andrewbranch) merged 6 commits into
microsoft:mainfrom
andrewbranch:api-transpile
Aug 11, 2026
Merged

Andrew Branch (andrewbranch) merged 6 commits into
microsoft:mainfrom
andrewbranch:api-transpile

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

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))

Copilot AI balanced review requested due to automatic review settings August 7, 2026 22:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread internal/transpile/transpile_test.go Outdated
"gotest.tools/v3/assert"
)

func TestTranspileModule(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we port over the transpile test runner instead of relying on unit tests?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This exposed a couple real emit diffs, which I don’t have time to address here before the beta.

@andrewbranch
Andrew Branch (andrewbranch) added this pull request to the merge queue Aug 11, 2026
Merged via the queue into microsoft:main with commit 6426ce8 Aug 11, 2026
21 checks passed
@andrewbranch
Andrew Branch (andrewbranch) deleted the api-transpile branch August 11, 2026 02:21
Jake Bailey (jakebailey) pushed a commit to jakebailey/TypeScript that referenced this pull request Aug 13, 2026
@johnnyreilly

Copy link
Copy Markdown

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:

Hit and worked around a real quirk in the current typescript-go nightly: transpileModule throws a spurious TS5069 whenever isolatedDeclarations is present in its options at all (regardless of declaration/composite) — confirmed via a standalone repro script, so isolatedDeclarations is stripped before calling transpileModule (it's irrelevant to JS emit anyway) and only passed to transpileDeclaration.

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


isolatedDeclarations + declaration/composite together still trip TS5069 in API.transpileModule

Environment: [email protected] (nightly), Node v24.18.0, macOS. Using typescript/unstable/sync's API class.

Repro

const { API } = require('typescript/unstable/sync');
const api = new API({});

const contents = [
  'export function add(a: number, b: number): number {',
  '\treturn a + b;',
  '}',
  '',
  'export const value: number = 42;',
  '',
].join('\n');

const result = api.transpileModule(contents, {
  compilerOptions: { declaration: true, isolatedDeclarations: true },
  fileName: '/tmp/app.ts',
  reportDiagnostics: true,
});

console.log(result.diagnostics);

Expected: no diagnostics — declaration is specified, so per the diagnostic's own wording ("cannot be specified without specifying option 'declaration' or option 'composite'"), this should pass.

Actual: always returns

[{"pos":-1,"end":-1,"code":5069,"category":1,
  "text":"Option 'isolatedDeclarations' cannot be specified without specifying option 'declaration' or option 'composite'."}]

Evidence it's specific to this combination, not isolatedDeclarations validation generally

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.

@andrewbranch

Copy link
Copy Markdown
Member Author

Thanks John! Sounds like a bug, will take a look.

@johnnyreilly

Copy link
Copy Markdown

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:

transpileDeclaration panics on Windows given an absolute fileName ("mixed posix and windows paths") — transpileModule handles the identical input fine

Environment: typescript@next (nightly, ~2026-08-20 build; locally reproduced against 7.1.0-dev.20260819.1), Windows Server 2025 (GitHub Actions windows-2025-vs2026 runner). Using typescript/unstable/sync's API class.

Repro

const { API } = require('typescript/unstable/sync');
const api = new API({});

const contents = [
  'export function add(a: number, b: number): number {',
  '\treturn a + b;',
  '}',
  '',
  'export const value: number = 42;',
  '',
].join('\n');

// Any absolute fileName panics on Windows - OS-native or forward-slash-normalized, doesn't matter:
api.transpileDeclaration(contents, {
  compilerOptions: { declaration: true, isolatedDeclarations: true },
  fileName: 'C:\\Users\\me\\project\\app.ts',   // or 'C:/Users/me/project/app.ts' - same result
  reportDiagnostics: true,
});

Expected: same behavior as transpileModule given the identical fileName and compilerOptions — either a normal TranspileOutput, or at worst a Diagnostic explaining what's wrong with the input.

Actual: the request throws, with this embedded in the error text:

panic: mixed posix and windows paths
goroutine 1 [running]:
runtime/debug.Stack()
	runtime/debug/stack.go:26 +0x5e
github.com/microsoft/typescript-go/internal/api.(*SyncConn).handleRequest.func1()
	github.com/microsoft/typescript-go/internal/api/conn_sync.go:120 +0x58
panic({0x7ff75b7332c0?, 0x7ff75bb61660?})
	runtime/panic.go:860 +0x13a
github.com/microsoft/typescript-go/internal/vfs/vfstest.FromMapWithClock[...](0x2d8eb78eb7a8, 0x1, {0x7ff75bb68620, 0x2d8eb5c04bd0})
	github.com/microsoft/typescript-go/internal/vfs/vfstest/vfstest.go:137 +0x905
github.com/microsoft/typescript-go/internal/vfs/vfstest.FromMap[...](0x2d8eb78eb7a8, 0x1)
	github.com/microsoft/typescript-go/internal/vfs/vfstest/vfstest.go:71 +0x9b
github.com/microsoft/typescript-go/internal/transpile.transpileWorker({0x7ff75bb6a458, 0x2d8eb5ae6a80}, {0x2d8eb7a7c4d0, 0x67}, {0x2d8eb5c53b08?, {0x2d8eb5a49810?, 0x2d8eb7809520?}, 0x60?}, 0x1)
	github.com/microsoft/typescript-go/internal/transpile/transpile.go:198 +0x3f5
github.com/microsoft/typescript-go/internal/transpile.TranspileDeclaration(...)
	github.com/microsoft/typescript-go/internal/transpile/transpile.go:115
github.com/microsoft/typescript-go/internal/api.transpileOutput(...)
	github.com/microsoft/typescript-go/internal/api/session.go:1265 +0x47
github.com/microsoft/typescript-go/internal/api.(*Session).handleTranspile(...)
	github.com/microsoft/typescript-go/internal/api/session.go:1243
github.com/microsoft/typescript-go/internal/api.(*Session).HandleRequest(...)
	github.com/microsoft/typescript-go/internal/api/session.go:622 +0x2aa5
github.com/microsoft/typescript-go/internal/api.(*SyncConn).handleRequest(...)
	github.com/microsoft/typescript-go/internal/api/conn_sync.go:136 +0x3d4
github.com/microsoft/typescript-go/internal/api.(*SyncConn).Run(...)
	github.com/microsoft/typescript-go/internal/api/conn_sync.go:73 +0xff
github.com/microsoft/typescript-go/internal/api.(*StdioServer).Run(...)
	github.com/microsoft/typescript-go/internal/api/server.go:123 +0x6b7
main.runAPI(...)
	github.com/microsoft/typescript-go/cmd/tsgo/api.go:56 +0x3e5
main.runMain()
	github.com/microsoft/typescript-go/cmd/tsgo/main.go:26 +0xc6
main.main()
	github.com/microsoft/typescript-go/cmd/tsgo/main.go:15 +0x13

What we tried, and what actually mattered

We hit this while integrating transpileModule/transpileDeclaration into ts-loader's Windows CI. The panic was 100% reproducible and identical across every variant of fileName we tried, except the last:

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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants