Files
copilotkit__copilotkit/scripts/__tests__/validate-dts-ambient.test.ts
Benjamin Taylor 8951232a0f fix(runtime): stop emitting a require() statement in published .d.cts
A consumer who imports @copilotkit/runtime and compiles with strict +
skipLibCheck: false gets 81 errors from our published declarations, 71 of
them TS1036 "Statements are not allowed in ambient contexts". Cause: the
tsdown banner that guarantees reflect-metadata loads before type-graphql
was returned as a string, and tsdown applies a string banner to every
emitted chunk -- declarations included. So all 87 published .d.cts files
began with `require("reflect-metadata");`, which is a statement and
illegal in an ambient context.

Returning an object instead lets tsdown route the banner by chunk kind, so
JS keeps its reflect-metadata prologue and declarations get nothing. The
fileName condition is gone too: tsdown's resolveChunkAddon reassigns its
own closure variable on the first call, so a function banner is evaluated
once and reused, meaning that condition was really deciding the banner for
the entire build from whichever chunk was emitted first. Keying on format
alone is order-independent.

This was invisible to us because every scaffolder sets skipLibCheck: true,
and because .d.mts got the legal `import "reflect-metadata";` form -- ESM
consumers never saw a single TS1036.

Adds a check-dts target that parses the built declarations and fails on any
top-level statement, wired into the existing package-quality job so the
class cannot come back silently.

Refs OSS-899
2026-08-21 08:24:46 -05:00

133 lines
3.7 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import {
findAmbientViolations,
formatViolations,
listDeclarationFiles,
} from "../validate-dts-ambient.js";
function setupDist(files: Record<string, string>): string {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "validate-dts-"));
for (const [relative, contents] of Object.entries(files)) {
const full = path.join(root, relative);
fs.mkdirSync(path.dirname(full), { recursive: true });
fs.writeFileSync(full, contents);
}
return root;
}
describe("findAmbientViolations", () => {
let dist: string;
afterEach(() => {
if (dist) fs.rmSync(dist, { recursive: true, force: true });
});
it("flags the reflect-metadata require banner that shipped in OSS-899", () => {
dist = setupDist({
"index.d.cts": [
'require("reflect-metadata");',
'import { Foo } from "./foo.cjs";',
"export { Foo };",
].join("\n"),
});
expect(findAmbientViolations(dist)).toEqual([
{ file: "index.d.cts", line: 1, snippet: 'require("reflect-metadata");' },
]);
});
it("accepts the ESM form of the same banner, which is a legal side-effect import", () => {
dist = setupDist({
"index.d.mts": [
'import "reflect-metadata";',
"export type A = string;",
].join("\n"),
});
expect(findAmbientViolations(dist)).toEqual([]);
});
it("accepts every declaration form a real .d.ts uses", () => {
dist = setupDist({
"index.d.ts": [
'import type { X } from "./x.js";',
'import Y = require("./y");',
"type A = X;",
"interface B { a: A }",
"declare enum C { One }",
"declare class D {}",
"declare function e(): void;",
"declare const f: number;",
'declare module "g" {}',
"declare global {}",
"export { A, B, C, D, e, f };",
"export default Y;",
"export * from './h.js';",
"export as namespace pkg;",
";",
].join("\n"),
});
expect(findAmbientViolations(dist)).toEqual([]);
});
it("reports control flow and assignment statements with their line numbers", () => {
dist = setupDist({
"a.d.ts": ["type A = 1;", "if (A) {}"].join("\n"),
"nested/b.d.mts": ["export type B = 2;", "", "globalThis.x = 1;"].join(
"\n",
),
});
expect(findAmbientViolations(dist)).toEqual([
{ file: "a.d.ts", line: 2, snippet: "if (A) {}" },
{
file: path.join("nested", "b.d.mts"),
line: 3,
snippet: "globalThis.x = 1;",
},
]);
});
it("ignores non-declaration files that sit next to the declarations", () => {
dist = setupDist({
"index.cjs": 'require("reflect-metadata");',
"index.d.cts": "export type A = string;",
"index.d.cts.map": '{"version":3}',
});
expect(listDeclarationFiles(dist)).toEqual([
path.join(dist, "index.d.cts"),
]);
expect(findAmbientViolations(dist)).toEqual([]);
});
});
describe("formatViolations", () => {
it("returns an empty string when there is nothing to report", () => {
expect(formatViolations([], "dist")).toBe("");
});
it("names the file, line, and offending source", () => {
const message = formatViolations(
[
{
file: "lib/logger.d.cts",
line: 1,
snippet: 'require("reflect-metadata");',
},
],
"dist",
);
expect(message).toContain("Found 1 statement(s)");
expect(message).toContain("TS1036");
expect(message).toContain(
`${path.join("dist", "lib/logger.d.cts")}:1 require("reflect-metadata");`,
);
});
});