|
31 | 31 | * every `CHANGELOG.md` (release-owned). |
32 | 32 | */ |
33 | 33 |
|
34 | | -import { readFileSync } from 'node:fs'; |
35 | | -import { execFileSync } from 'node:child_process'; |
| 34 | +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; |
| 35 | +import { execFileSync, spawnSync } from 'node:child_process'; |
| 36 | +import { tmpdir } from 'node:os'; |
36 | 37 | import { join } from 'node:path'; |
37 | 38 | import { fileURLToPath } from 'node:url'; |
38 | 39 | import process from 'node:process'; |
| 40 | +import { gitFreeEnv } from './git-env.mjs'; |
39 | 41 | import { isEntrypoint } from './invoked-as.mjs'; |
40 | 42 |
|
41 | 43 | const ROOT = join(fileURLToPath(new URL('.', import.meta.url)), '..'); |
@@ -78,8 +80,29 @@ export function findingsIn(text) { |
78 | 80 | return out; |
79 | 81 | } |
80 | 82 |
|
81 | | -function trackedFiles() { |
82 | | - return execFileSync('git', ['ls-files', '-z', '--', ...ROOT_DIR_WATCH_HINTS.map((h) => h.replace(/\/\*\*$/, ''))], { cwd: ROOT, encoding: 'utf8' }) |
| 83 | +/** |
| 84 | + * The stdout ceiling the listing below is spawned with — the value and name |
| 85 | + * `scripts/pm/dispatch-gates.mjs` gave its own git reads. |
| 86 | + * |
| 87 | + * `execFileSync`'s default is 1 MiB; past it node kills the child and throws |
| 88 | + * `ENOBUFS`, so the gate dies instead of answering. This population's listing |
| 89 | + * grows with every path added under `packages/`, `content/docs/` and |
| 90 | + * `.changeset/`, and it reached the default: one added changeset put it over |
| 91 | + * and red-flagged every PR that carried one. 64 MiB is headroom for a listing |
| 92 | + * many times this tree's; the bound stays finite so a runaway child is still |
| 93 | + * stopped. Raising it changes no verdict on a tree that already fit — only |
| 94 | + * whether a larger tree gets one. |
| 95 | + */ |
| 96 | +export const GIT_MAX_BUFFER_BYTES = 64 * 1024 * 1024; |
| 97 | + |
| 98 | +/** The tracked paths under the walk roots, read NUL-separated. `cwd`/`env` exist for the self-test's fixture index. */ |
| 99 | +export function trackedFiles({ cwd = ROOT, env = process.env } = {}) { |
| 100 | + return execFileSync('git', ['ls-files', '-z', '--', ...ROOT_DIR_WATCH_HINTS.map((h) => h.replace(/\/\*\*$/, ''))], { |
| 101 | + cwd, |
| 102 | + env, |
| 103 | + encoding: 'utf8', |
| 104 | + maxBuffer: GIT_MAX_BUFFER_BYTES, |
| 105 | + }) |
83 | 106 | .split('\0') |
84 | 107 | .filter(Boolean); |
85 | 108 | } |
@@ -178,6 +201,44 @@ function selfTest() { |
178 | 201 | // The tree: a non-empty population. |
179 | 202 | t('the population on this tree is not empty', sweep().scanned > 0); |
180 | 203 |
|
| 204 | + // The listing past spawn's default 1 MiB stdout buffer. The fixture is an |
| 205 | + // index alone — `update-index --index-info` stages paths with no files on |
| 206 | + // disk — sized past 1 MiB, under a walk root so the pathspec keeps it. The |
| 207 | + // CONTROL reads the same listing with the default buffer and must die with |
| 208 | + // ENOBUFS, so the pass below cannot come from a fixture that never reached |
| 209 | + // the size. |
| 210 | + const bufTmp = mkdtempSync(join(tmpdir(), 'closed-ceremony-maxbuffer-')); |
| 211 | + try { |
| 212 | + const env = gitFreeEnv(); |
| 213 | + const init = spawnSync('git', ['init', '-q', '.'], { cwd: bufTmp, env, encoding: 'utf8' }); |
| 214 | + t('fixture: git init', init.status === 0); |
| 215 | + const EMPTY_BLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'; |
| 216 | + const paths = Array.from({ length: 16000 }, (_, i) => |
| 217 | + `packages/fixture-${String(i).padStart(5, '0')}/src/a-long-enough-module-name-for-a-wide-listing.ts`); |
| 218 | + const staged = spawnSync('git', ['update-index', '--index-info'], { |
| 219 | + cwd: bufTmp, |
| 220 | + env, |
| 221 | + encoding: 'utf8', |
| 222 | + input: paths.map((p) => `100644 ${EMPTY_BLOB} 0\t${p}`).join('\n') + '\n', |
| 223 | + }); |
| 224 | + t('fixture: git update-index', staged.status === 0); |
| 225 | + const listingBytes = paths.reduce((n, p) => n + Buffer.byteLength(p) + 1, 0); |
| 226 | + t('the fixture listing is past the 1 MiB default buffer', listingBytes > 1024 * 1024); |
| 227 | + const atDefault = spawnSync('git', ['ls-files', '-z', '--', 'packages'], { cwd: bufTmp, env, encoding: 'utf8' }); |
| 228 | + t('CONTROL: the same listing read with the default buffer dies with ENOBUFS', atDefault.error?.code === 'ENOBUFS'); |
| 229 | + let listed = null; |
| 230 | + try { |
| 231 | + listed = trackedFiles({ cwd: bufTmp, env }); |
| 232 | + } catch { |
| 233 | + listed = null; |
| 234 | + } |
| 235 | + t('trackedFiles reads the whole listing instead of dying with ENOBUFS', listed?.length === paths.length); |
| 236 | + t('…first and last path included', listed?.[0] === paths[0] && listed?.at(-1) === paths.at(-1)); |
| 237 | + t('the ceiling stays finite and above this fixture', Number.isFinite(GIT_MAX_BUFFER_BYTES) && GIT_MAX_BUFFER_BYTES > listingBytes); |
| 238 | + } finally { |
| 239 | + rmSync(bufTmp, { recursive: true, force: true }); |
| 240 | + } |
| 241 | + |
181 | 242 | if (failures.length) { |
182 | 243 | for (const f of failures) console.error(` ✗ ${f}`); |
183 | 244 | console.error(`check-closed-ceremony-text --self-test: ${failures.length} of ${checked} assertion(s) FAILED.`); |
|
0 commit comments