perf(build): avoid duplicate native declaration transforms

Native SDK preparation transformed declarations separately for diagnostics,
then repeated preflight and transformation during in-memory emission. Keep
configuration and requested semantic validation, collect declaration errors
from EmitResult, and reject errors or skipped emission before publication.
Remove the unused includeDeclaration diagnostic option.

Measured cold Node 24 preparation fell from 156.294s to 89.817s; sampled
process-group memory fell from 8.845 to 6.961 GiB. The existing 300-second
limit and Go memory settings are unchanged. This removes proven duplicate
work; it does not conclusively reproduce the historical CI timeout.

Proof: real native tests reject TS4094 and lazy-global TS2318 in both
modes, reject TS2322 in all mode, and accept valid declarations. Focused
suite: 14 passed/3 platform skips, 6.176s wall. Sibling preparation and
tsdown integration: 52 passed/7 platform skips. Changed-file checks,
types/lint, and final independent review through P2 pass.

The native API global-diagnostics handler already forces semantic checking
before collecting fileless diagnostics; source inspection and real API
probes resolve the apparent late-global diagnostic gap. Declaration member
order and synthesized readonly modifiers vary across unchanged original
compiler runs, so generated byte equivalence is not claimed.
This commit is contained in:
Peter Steinberger 2026-09-26 10:03:55 -07:00
parent c32d26f3e2
commit 614aa3acff
4 changed files with 56 additions and 6 deletions

View file

@ -69,6 +69,9 @@ emission. Its filesystem callbacks make candidates outside the checkout appear
missing before native resolution can read them. Source and package-manifest reads
are captured directly; admission does not depend on parsing resolution traces.
The compiler version is pinned in `package.json` because this API is unstable.
Configuration and requested semantic checks run before emission. Declaration
errors come from the in-memory emit result, avoiding a separate declaration
transform solely for diagnostics. Any error prevents artifact publication.
Nested physical worktrees are supported with their own
`pnpm install --frozen-lockfile`, even when ancestor directories contain

View file

@ -136,9 +136,9 @@ export async function compileNativeProject({
declaration: true,
emitDeclarationOnly: emit,
noEmit: !emit,
// Declaration-only callers already validate the structural diagnostic
// set below; emission must not add a second semantic check.
noEmitOnError: diagnostics === "all",
// Validate the requested diagnostics below, then use the in-memory
// emit result for declaration errors without a second preflight.
noEmitOnError: false,
noCheck: false,
},
}),
@ -154,7 +154,6 @@ export async function compileNativeProject({
}
const errors = await collectNativeTypeScriptDiagnosticsAsync(project, {
includeSemantic: diagnostics === "all",
includeDeclaration: emit,
});
view.assertValid();
if (errors.length) {

View file

@ -1,7 +1,7 @@
import type { Project as AsyncProject } from "typescript/unstable/async";
import { DiagnosticCategory, type Diagnostic, type Project } from "typescript/unstable/sync";
type DiagnosticOptions = { includeSemantic?: boolean; includeDeclaration?: boolean };
type DiagnosticOptions = { includeSemantic?: boolean };
function diagnosticQueries(options: DiagnosticOptions) {
return [
@ -11,7 +11,6 @@ function diagnosticQueries(options: DiagnosticOptions) {
"getSyntacticDiagnostics",
"getBindDiagnostics",
...(options.includeSemantic === false ? [] : ["getSemanticDiagnostics" as const]),
...(options.includeDeclaration ? ["getDeclarationDiagnostics" as const] : []),
] as const;
}

View file

@ -122,6 +122,55 @@ function createNativeFixture(root: string, declared = root) {
return { native, write, compile };
}
it.each(
(["all", "declarations"] as const).flatMap((diagnostics) => [
{
diagnostics,
kind: "declaration transform",
source: "export const factory = () => class { private value = 1; };",
error: /TS4094: Property 'value' of exported anonymous class type/u,
},
{
diagnostics,
kind: "lazy global",
source: "export function* values() { yield 1; }",
error: /TS2318: Cannot find global type 'IterableIterator'/u,
},
]),
)(
"rejects $kind errors in $diagnostics diagnostic mode",
async ({ diagnostics, source, error }) => {
const root = fs.realpathSync.native(roots.make("native-declaration-errors-"));
const fixture = createNativeFixture(root);
fixture.write("src/index.ts", source);
const boundary = createDeclarationInputBoundary(root);
await expect(
emitNativeDeclarations({
cwd: root,
compilerRoot: root,
configFile: path.join(root, "tsconfig.json"),
roots: [path.join(root, "src/index.ts")],
diagnostics,
compilerOptions: { lib: ["es5"] },
assertInput: (file) => boundary.assert(file),
}),
).rejects.toThrow(error);
},
);
it("rejects semantic errors before returning valid native declarations", async () => {
const root = fs.realpathSync.native(roots.make("native-declaration-semantics-"));
const fixture = createNativeFixture(root);
fixture.write("src/index.ts", 'export const count: number = "wrong";');
await expect(fixture.compile()).rejects.toThrow(/TS2322: Type 'string' is not assignable/u);
fixture.write("src/index.ts", "export const count: number = 42;");
const emitted = await fixture.compile();
expect(emitted.declarations.get(path.join(root, "src/index.ts"))?.code).toContain(
"export declare const count: number;",
);
});
it("bounds optional SDK relative imports and manifest probes to the checkout", async () => {
const ancestor = fs.realpathSync.native(roots.make("native-declaration-optional-imports-"));
const root = path.join(ancestor, ".worktrees/validation");