Repository navigation
Expand file tree
/
Copy pathcheck-dist-completeness.test.ts
More file actions
325 lines (289 loc) · 12.9 KB
/
Copy pathcheck-dist-completeness.test.ts
File metadata and controls
325 lines (289 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import { describe, expect, it } from 'vitest';
import { uiTurboConfigPath } from '../workspace-root.mjs';
import { UI_TSC_BUILT_PACKAGE_FLOOR } from './helpers/ui-package-population.mjs';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
// Plain-JS CI helper. Its types are INFERRED from the .mjs source by
// `tsconfig.scripts.json` (`allowJs`), so no `@ts-expect-error` here —
// re-adding one is now itself an error (TS2578). See objectui#3494.
import {
analyze,
auditPackage,
buildsWithTsc,
discoverTscPackages,
expectedEmit,
report,
} from '../check-dist-completeness.mjs';
/**
* objectui#6703. `packages/types/dist` was seen holding 4 of its 40 top-level
* `.d.ts` files while the build that wrote it exited 0, and the damage surfaced
* as ordinary-looking type errors in `permissions` and `mobile`.
*
* The mechanism, measured rather than assumed: every `"build": "tsc"` package
* here is `composite`, so `tsc` records what it emitted in a
* `tsconfig.tsbuildinfo` that TypeScript resolves NEXT TO `tsconfig.json` —
* outside the `dist/` it describes. Delete the `dist/` and leave that file, and
* `tsc` believes the record, emits nothing and exits 0. It never stats its own
* outputs.
*
* Two locks live here. The unit cases below pin the judgement on real
* directories built in a tmpdir, because the defect IS a question about files
* on disk and a mocked `fs` would answer it the way the mock was written. The
* repo-state cases at the bottom pin the WIRING — a gate that no build script
* invokes is indistinguishable from one that passes, and the wiring is the
* whole reason this check runs before turbo can cache a green empty build.
*/
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
/** A minimal real tsc project: N sources, a tsconfig that emits to `dist/`. */
function makeProject(dir: string, sources: string[]): void {
fs.mkdirSync(path.join(dir, 'src'), { recursive: true });
for (const s of sources) {
fs.writeFileSync(path.join(dir, 'src', s), `export const ${s.replace(/\W/g, '_')} = 1;\n`);
}
fs.writeFileSync(
path.join(dir, 'package.json'),
`${JSON.stringify({ name: '@fixture/pkg', scripts: { build: 'tsc' } }, null, 2)}\n`,
);
fs.writeFileSync(
path.join(dir, 'tsconfig.json'),
`${JSON.stringify(
{
compilerOptions: {
outDir: './dist',
rootDir: './src',
declaration: true,
composite: true,
module: 'ESNext',
moduleResolution: 'bundler',
},
include: ['src/**/*'],
},
null,
2,
)}\n`,
);
}
/** Pretend `tsc` ran: write the outputs the gate should expect, or some of them. */
function emit(dir: string, outputs: string[]): void {
for (const o of outputs) {
fs.mkdirSync(path.dirname(o), { recursive: true });
fs.writeFileSync(o, '');
}
fs.writeFileSync(path.join(dir, 'tsconfig.tsbuildinfo'), '{}');
}
function withTmp<T>(fn: (dir: string) => T): T {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'dist-completeness-'));
try {
return fn(dir);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}
describe('buildsWithTsc', () => {
it('recognises a bare tsc build', () => {
expect(buildsWithTsc({ scripts: { build: 'tsc' } })).toBe(true);
});
it('still recognises the build after this very gate is appended to it', () => {
// The wiring below rewrites every one of these scripts. If the predicate
// stopped matching its own output, `--all` would silently scan nothing.
expect(
buildsWithTsc({ scripts: { build: 'tsc && node ../../scripts/check-dist-completeness.mjs' } }),
).toBe(true);
});
it('does not claim a vite build', () => {
expect(buildsWithTsc({ scripts: { build: 'vite build' } })).toBe(false);
expect(buildsWithTsc({ scripts: {} })).toBe(false);
expect(buildsWithTsc({})).toBe(false);
});
});
describe('expectedEmit — derived from TypeScript, never written down', () => {
it('asks the compiler what each input emits rather than counting files', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts', 'b.ts', 'c.ts']);
const { inputs, outputs, buildInfoPath } = expectedEmit(dir);
expect(inputs).toHaveLength(3);
// declaration:true, no maps -> exactly .js + .d.ts per input.
expect(outputs.map((o: string) => path.relative(dir, o)).sort()).toEqual([
'dist/a.d.ts',
'dist/a.js',
'dist/b.d.ts',
'dist/b.js',
'dist/c.d.ts',
'dist/c.js',
]);
// The buildinfo sits OUTSIDE the outDir it is a record of. That is the
// whole defect: `rm -rf dist` cannot take it, so the two desync.
expect(path.relative(dir, buildInfoPath as string)).toBe('tsconfig.tsbuildinfo');
});
});
it('follows the tsconfig it is given instead of a fixed output shape', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts']);
const cfg = JSON.parse(fs.readFileSync(path.join(dir, 'tsconfig.json'), 'utf8'));
cfg.compilerOptions.declaration = false;
cfg.compilerOptions.composite = false;
cfg.compilerOptions.sourceMap = true;
fs.writeFileSync(path.join(dir, 'tsconfig.json'), JSON.stringify(cfg, null, 2));
expect(expectedEmit(dir).outputs.map((o: string) => path.relative(dir, o)).sort()).toEqual([
'dist/a.js',
'dist/a.js.map',
]);
});
});
});
describe('auditPackage', () => {
it('passes a dist that holds every file tsc emits', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts', 'b.ts']);
emit(dir, expectedEmit(dir).outputs);
const audit = auditPackage({ name: '@fixture/pkg', dir }, { root: dir });
expect(audit.state).toBe('complete');
expect(audit.missing).toEqual([]);
expect(audit.expected).toBe(4);
});
});
it('fails the reported shape: some outputs present, the buildinfo still claiming all of them', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts', 'b.ts', 'c.ts']);
const { outputs } = expectedEmit(dir);
emit(dir, outputs.slice(0, 2));
const audit = auditPackage({ name: '@fixture/pkg', dir }, { root: dir });
expect(audit.state).toBe('incomplete');
expect(audit.missing).toHaveLength(4);
expect(audit.buildInfoPresent).toBe(true);
});
});
it('fails a buildinfo with NO dist at all — the state an empty turbo cache entry restores', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts']);
fs.writeFileSync(path.join(dir, 'tsconfig.tsbuildinfo'), '{}');
const audit = auditPackage({ name: '@fixture/pkg', dir }, { root: dir });
expect(audit.state).toBe('incomplete');
expect(audit.missing).toEqual(['dist/a.js', 'dist/a.d.ts']);
});
});
it('skips a package that was simply never built — no dist AND no buildinfo', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts']);
const audit = auditPackage({ name: '@fixture/pkg', dir }, { root: dir });
expect(audit.state).toBe('never-built');
expect(audit.missing).toEqual([]);
});
});
it('reports a type-check-only project apart from an unbuilt one', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts']);
const cfg = JSON.parse(fs.readFileSync(path.join(dir, 'tsconfig.json'), 'utf8'));
cfg.compilerOptions.noEmit = true;
cfg.compilerOptions.composite = false;
fs.writeFileSync(path.join(dir, 'tsconfig.json'), JSON.stringify(cfg, null, 2));
// `fields` and `apps/console` are real instances: their `build` opens with
// `tsc` as a CHECK and emits through vite. Collapsing this into
// "never built" would let a package that stopped emitting read as one
// that had merely not been built yet.
expect(auditPackage({ name: '@fixture/pkg', dir }, { root: dir }).state).toBe('no-emit');
});
});
});
describe('report', () => {
it('exits 1 and names the missing files, the stale record, and the repair', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts', 'b.ts']);
const { outputs } = expectedEmit(dir);
emit(dir, outputs.slice(0, 1));
const audit = auditPackage({ name: '@fixture/pkg', dir }, { root: dir });
const { exitCode, lines } = report([audit], { root: dir });
const text = lines.join('\n');
expect(exitCode).toBe(1);
expect(text).toContain('@fixture/pkg');
expect(text).toContain('tsconfig.tsbuildinfo');
expect(text).toContain('rm -f tsconfig.tsbuildinfo');
// The cascade is the expensive half: without this sentence the reader
// goes looking for the type error in the package that reported it.
expect(text).toContain('It fails in the packages that');
});
});
it('exits 0 with a count, so a run that verified nothing cannot read as a pass', () => {
withTmp((dir) => {
makeProject(dir, ['a.ts', 'b.ts']);
emit(dir, expectedEmit(dir).outputs);
const audit = auditPackage({ name: '@fixture/pkg', dir }, { root: dir });
const { exitCode, lines } = report([audit], { root: dir });
expect(exitCode).toBe(0);
expect(lines.join('\n')).toContain('4 emitted files verified');
});
});
});
describe('analyze', () => {
it('says so and passes when the package in cwd does not build with tsc', () => {
withTmp((dir) => {
fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({ name: '@fixture/vite', scripts: { build: 'vite build' } }),
);
const { exitCode, lines, audits } = analyze({ root: dir, cwd: dir });
expect(exitCode).toBe(0);
expect(audits).toEqual([]);
expect(lines.join('\n')).toContain('does not build with tsc');
});
});
it('throws rather than passing when the walk finds no packages', () => {
withTmp((dir) => {
// An empty scan satisfies every assertion while looking at nothing, which
// is the one outcome a gate must never report as success.
expect(() => analyze({ root: dir, all: true })).toThrow(/no workspace package/);
});
});
});
describe('this repository', () => {
const tscPackages = discoverTscPackages(repoRoot) as Array<{ name: string; dir: string }>;
it('finds the tsc-built packages', () => {
// A floor against an empty walk, not a census: about two thirds of the 11
// measured on objectstack 6a7c0fe3, so retiring a package leaves it green.
expect(tscPackages.length).toBeGreaterThanOrEqual(UI_TSC_BUILT_PACKAGE_FLOOR);
});
it.each(tscPackages.map((p) => [p.name, p.dir] as const))(
'every emitting tsc package runs this check in its own build: %s',
(name, dir) => {
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
if (expectedEmit(dir).outputs.length === 0) return; // type-check-only; nothing to verify
// Placement is load-bearing, not a preference. turbo caches a task that
// exits 0, so a `tsc` that short-circuits gets its empty `dist/` RECORDED
// as a successful build — and that entry then replays as `cache hit …
// FULL TURBO` into every worktree sharing the store. Checking inside the
// build is what stops the entry from being written at all.
expect(manifest.scripts.build).toContain('check-dist-completeness.mjs');
},
);
it.each(tscPackages.map((p) => [p.name, p.dir] as const))(
'a clean script takes the buildinfo with the dist it describes: %s',
(name, dir) => {
const manifest = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8'));
const clean = manifest.scripts?.clean;
if (typeof clean !== 'string') return;
// Scoped to projects that actually WRITE a buildinfo. `fields` opens its
// build with `tsc` but is `noEmit` with neither `composite` nor
// `incremental`, so TypeScript resolves no buildinfo path for it and
// there is nothing for its `clean` to desync from.
if (!expectedEmit(dir).buildInfoPath) return;
// `rm -rf dist` alone leaves the record of what was emitted behind, so
// `pnpm clean && pnpm build` produced an EMPTY dist and exit 0 — measured
// on 4357ec7 before this was fixed.
expect(clean).toContain('tsconfig.tsbuildinfo');
},
);
it('turbo treats the gate as an input of the builds that run it', () => {
const turbo = JSON.parse(fs.readFileSync(uiTurboConfigPath(), 'utf8'));
// Otherwise editing the gate does not invalidate a single cached build, and
// the new rule is enforced only where nothing was cached.
expect(turbo.tasks.build.inputs).toContain('$TURBO_ROOT$/scripts/check-dist-completeness.mjs');
});
it('has a root alias pointing at the script that exists', () => {
const pkg = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
const alias = pkg.scripts['check:dist-completeness'];
expect(alias).toBe('node scripts/check-dist-completeness.mjs --all');
expect(fs.existsSync(path.join(repoRoot, 'scripts/check-dist-completeness.mjs'))).toBe(true);
});
});