|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Issue #77: one deep structural comparison must not poison later ones. The |
| 4 | + * pinned tsgo's relater keyed recursion identities on the outer type only, so |
| 5 | + * a 40-deep interface chain (A_i.next?: A_{i+1}[]) was never cut as deeply |
| 6 | + * nested; the comparison ran into the stack-depth limit, reported TS2321, and |
| 7 | + * cached its failed sub-relations — a later, unrelated assignment inside the |
| 8 | + * same chain then failed with TS2322. Stock 6.0.3 and tsgo main (after |
| 9 | + * typescript-go#4913, carried here as the tsgo-relater-recursion-identity |
| 10 | + * stopgap) report nothing. Both assignment orders run, since the cache |
| 11 | + * poisoning is order-dependent. |
| 12 | + * |
| 13 | + * The chain cases run through stock tsc and the fork's tsc; stock must exit |
| 14 | + * clean (the fixture is still the bug's shape) and the fork's diagnostic |
| 15 | + * headers must equal stock's. The GetByString conditional-type chain |
| 16 | + * (typescript-go#2917, the same recursion-identity path) exhausts stock's |
| 17 | + * heap, so its reference is tsgo main's conformance baseline — no |
| 18 | + * .errors.txt, i.e. clean — and only the fork runs. |
| 19 | + * |
| 20 | + * Stock side: STOCK_TYPESCRIPT_PATH, else derived from STOCK_TSSERVER_PATH |
| 21 | + * (CI), else /tmp/stock-ts-p3/package/lib/typescript.js. |
| 22 | + * Exit: 0 = PASS, 1 = FAIL. |
| 23 | + */ |
| 24 | +import { spawnSync } from 'node:child_process'; |
| 25 | +import * as fs from 'node:fs'; |
| 26 | +import * as os from 'node:os'; |
| 27 | +import * as path from 'node:path'; |
| 28 | +import { fileURLToPath } from 'node:url'; |
| 29 | + |
| 30 | +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 31 | +const forkTsc = path.join(repoRoot, 'lib', 'tsc.js'); |
| 32 | +const stockTsPath = process.env.STOCK_TYPESCRIPT_PATH |
| 33 | + ?? (process.env.STOCK_TSSERVER_PATH ? path.join(path.dirname(process.env.STOCK_TSSERVER_PATH), 'typescript.js') : undefined) |
| 34 | + ?? '/tmp/stock-ts-p3/package/lib/typescript.js'; |
| 35 | +const stockTsc = path.join(path.dirname(stockTsPath), 'tsc.js'); |
| 36 | +if (!fs.existsSync(stockTsc)) { |
| 37 | + console.error(`stock tsc not found at ${stockTsc} — set STOCK_TYPESCRIPT_PATH or STOCK_TSSERVER_PATH`); |
| 38 | + process.exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +const DEPTH = 40; |
| 42 | +const chain = prefix => Array.from({ length: DEPTH }, (_, i) => |
| 43 | + `interface ${prefix}${i} {\n name: string;\n next?: ${prefix}${(i + 1) % DEPTH}[];\n}\n`).join(''); |
| 44 | +const CHAINS = chain('A') + chain('B'); |
| 45 | +const HEAD = 'declare const a: A0;\nconst assigned: B0 = a;\n'; |
| 46 | +const MID = 'declare const a20: A20;\nconst mid: B20 = a20;\n'; |
| 47 | + |
| 48 | +const GET_BY_STRING = `export type FlattenKeys<O> = { |
| 49 | + [K in keyof O & (string | number)]: O[K] extends Record<any, any> |
| 50 | + ? K | \`\${K}.\${FlattenKeys<O[K]>}\` |
| 51 | + : K; |
| 52 | +}[O extends readonly any[] |
| 53 | + ? keyof O & \`\${number}\` |
| 54 | + : keyof O & (string | number)]; |
| 55 | +export type KeySeparator = '.' | '[' | ']'; |
| 56 | +export type GetByString< |
| 57 | + Data, |
| 58 | + Path extends string | number = FlattenKeys<Data>, |
| 59 | +> = Path extends \`__proto__\${\`\${KeySeparator}\${string | number}\` | ''}\` |
| 60 | + ? never |
| 61 | + : Path extends \`\${KeySeparator}\${infer Rest}\` |
| 62 | + ? GetByString<Data, Rest> |
| 63 | + : Path extends \`\${infer Rest}\${KeySeparator}\` |
| 64 | + ? GetByString<Data, Rest> |
| 65 | + : Path extends \`\${infer Key extends keyof Data & (string | number)}\${KeySeparator}\${infer Rest}\` |
| 66 | + ? GetByString<Data[Key], Rest> |
| 67 | + : Path extends keyof Data & (string | number) |
| 68 | + ? Data[Path] |
| 69 | + : undefined; |
| 70 | +interface Duck { |
| 71 | + taxonomy: { |
| 72 | + genus: 'anas'; |
| 73 | + species: 'platyrhynchos'; |
| 74 | + }; |
| 75 | +} |
| 76 | +export type DuckSpecies = GetByString<Duck, 'taxonomy.species'>; |
| 77 | +export const species: DuckSpecies = 'platyrhynchos'; |
| 78 | +`; |
| 79 | + |
| 80 | +const cases = [ |
| 81 | + { name: 'chain-head-then-mid', source: CHAINS + HEAD + MID + 'export {};\n', stock: true }, |
| 82 | + { name: 'chain-mid-then-head', source: CHAINS + MID + HEAD + 'export {};\n', stock: true }, |
| 83 | + { name: 'get-by-string', source: GET_BY_STRING, stock: false }, |
| 84 | +]; |
| 85 | + |
| 86 | +// Diagnostic headers only (tsgo renders shallower elaborations than stock by |
| 87 | +// design); a clean run is exit 0 with no headers, so a crash never reads clean. |
| 88 | +function run(tscJs, cwd) { |
| 89 | + const r = spawnSync(process.execPath, [tscJs, '-p', 'tsconfig.json'], { cwd, encoding: 'utf8', env: { ...process.env, GODEBUG: 'asyncpreemptoff=1' } }); |
| 90 | + if (r.error) throw r.error; |
| 91 | + const out = (r.stdout ?? '').replaceAll(cwd.replaceAll('\\', '/'), '.').replaceAll('\\', '/'); |
| 92 | + const headers = out.split('\n').filter(l => l.trim() && !/^\s/.test(l)).join('\n'); |
| 93 | + return r.status === 0 ? headers : `exit ${r.status ?? r.signal}\n${headers}\n${r.stderr.slice(-500)}`; |
| 94 | +} |
| 95 | + |
| 96 | +const root = fs.mkdtempSync(path.join(os.tmpdir(), 'tnb-deep-relation-')); |
| 97 | +let failed = 0; |
| 98 | +for (const c of cases) { |
| 99 | + const dir = path.join(root, c.name); |
| 100 | + fs.mkdirSync(dir); |
| 101 | + fs.writeFileSync(path.join(dir, 'src.ts'), c.source); |
| 102 | + fs.writeFileSync(path.join(dir, 'tsconfig.json'), JSON.stringify({ compilerOptions: { strict: true, noEmit: true, target: 'es2022', types: [] }, files: ['src.ts'] })); |
| 103 | + const stock = c.stock ? run(stockTsc, dir) : ''; |
| 104 | + const fork = run(forkTsc, dir); |
| 105 | + if (stock) { |
| 106 | + console.error(`FAIL ${c.name}: stock is no longer clean — fixture is stale.\n${stock}`); |
| 107 | + failed++; |
| 108 | + } else if (fork) { |
| 109 | + console.error(`FAIL ${c.name}: fork is not clean (reference: ${c.stock ? 'stock' : 'tsgo main'}).\n${fork}`); |
| 110 | + failed++; |
| 111 | + } else { |
| 112 | + console.log(`ok ${c.name} (clean, reference: ${c.stock ? 'stock' : 'tsgo main'})`); |
| 113 | + } |
| 114 | +} |
| 115 | +fs.rmSync(root, { recursive: true, force: true }); |
| 116 | +if (failed) { |
| 117 | + console.error(`FAIL: ${failed}/${cases.length} deep-relation cases diverged`); |
| 118 | + process.exit(1); |
| 119 | +} |
| 120 | +console.log(`PASS: all ${cases.length} deep-relation cases clean`); |
0 commit comments