Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 63 additions & 14 deletions core/scripts/check-arch-docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,87 @@ function fileChangedIn(ref, file) {
return out.length > 0;
}

export function hasDocsNotNeededTrailer(ref = baseRef()) {
assertBaseRef(ref);
const out = execFileSync('git', ['log', `${ref}..HEAD`, '--pretty=%B'], {
const TRAILER = /Docs-not-needed:/i;

function gitLog(ref) {
return execFileSync('git', ['log', `${ref}..HEAD`, '--pretty=%B'], {
cwd: ROOT,
encoding: 'utf8',
});
return /Docs-not-needed:/i.test(out);
}

export function filesNeedDocs({ ref = baseRef(), map = null } = {}) {
export function hasDocsNotNeededTrailer(ref = baseRef(), logText = null) {
assertBaseRef(ref);
return TRAILER.test(logText ?? gitLog(ref));
}

// The pull request this run belongs to (#3322): the merge queue squashes its title and body into the
// commit it judges, so on a `pull_request` run the trailer has to be in that text as well as in the
// branch commits. `GITHUB_EVENT_PATH` is the runner's own copy of the event; a `pull_request` run
// whose event cannot be read counts as a body without the trailer, so the gate stays red rather
// than passing on a text it could not see.
export function pullRequestEvent() {
if (process.env.GITHUB_EVENT_NAME !== 'pull_request') return undefined;
const empty = { name: 'pull_request', title: '', body: '' };
const path = process.env.GITHUB_EVENT_PATH;
if (!path) return empty;
try {
const event = JSON.parse(readFileSync(path, 'utf8'));
return {
name: 'pull_request',
title: String(event?.pull_request?.title ?? ''),
body: String(event?.pull_request?.body ?? ''),
};
} catch {
return empty;
}
}

export function filesNeedDocs({
ref = baseRef(),
map = null,
event = pullRequestEvent(),
logText = null,
files = null,
changed = null,
} = {}) {
assertBaseRef(ref);
const subsystemMap = map || loadSubsystemMap();
const files = changedFiles(ref);
if (files.length === 0) return { ok: true, missing: [], files: [] };
if (hasDocsNotNeededTrailer(ref)) return { ok: true, missing: [], files };
const touched = files ?? changedFiles(ref);
const inLog = hasDocsNotNeededTrailer(ref, logText);
// Only a pull request has a second place; a merge-queue entry and a local run judge the log alone.
const inBody = event?.name === 'pull_request'
? TRAILER.test(`${event.title ?? ''}\n${event.body ?? ''}`)
: true;
const trailer = { log: inLog, body: inBody, event: event?.name ?? 'local' };
if (touched.length === 0) return { ok: true, missing: [], files: [], trailer };
if (inLog && inBody) return { ok: true, missing: [], files: touched, trailer };
const isChanged = changed ?? (file => fileChangedIn(ref, file));
const missing = [];
for (const file of files) {
for (const file of touched) {
const match = longestMatch(file, subsystemMap);
if (!match) continue;
if (fileChangedIn(ref, match.docPath)) continue;
if (isChanged(match.docPath)) continue;
missing.push({ file, subsystem: match.subsystem, doc: match.docPath });
}
return { ok: missing.length === 0, missing, files };
return { ok: missing.length === 0, missing, files: touched, trailer };
}

// #1076: the trailer this gate looks for is read from `git log <base>..HEAD`, and on a merge-queue
// entry that range holds the squash of the pull request title and body — the branch commits are not in
// it. So "add a trailer" sent people to the commit they had just pushed, and the entry went red again.
// The refusal names both places, and says why the body counts.
export function failureLines(missing, root = ROOT) {
// The refusal names both places, and says why the body counts. Since #3322 a `pull_request` run reads
// both, and names the one that is missing rather than leaving it to the queue.
export function failureLines(missing, root = ROOT, trailer = null) {
const lines = ['arch-docs: FAIL — owned file(s) changed without subsystem doc update:'];
for (const m of missing) lines.push(` ${m.file} → ${m.subsystem} (${relative(root, m.doc)})`);
if (trailer?.event === 'pull_request' && trailer.log !== trailer.body) {
lines.push(trailer.body
? 'arch-docs: the pull request body has `Docs-not-needed:`, but the branch commits do not.'
: 'arch-docs: the branch commits have `Docs-not-needed:`, but the pull request title and body do not.');
lines.push(' The merge queue squashes the title and body into the commit it judges, so a trailer');
lines.push(' that lives only in a branch commit is gone exactly when main is judged. Add it to both.');
}
lines.push('Fix it one of two ways:');
lines.push(' - update the subsystem doc in docs/subsystems/ in this pull request, or');
lines.push(' - add a line `Docs-not-needed: <why>` to the pull request body AND to the branch commit.');
Expand All @@ -152,7 +201,7 @@ function main() {
console.log(`arch-docs: OK (${files.length} changed file(s))`);
process.exit(0);
}
for (const line of failureLines(missing)) console.error(line);
for (const line of failureLines(missing, ROOT, result.trailer)) console.error(line);
process.exit(1);
}

Expand Down
85 changes: 85 additions & 0 deletions core/test/check-arch-docs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,88 @@ test('the gate never runs git through a shell', { skip: process.platform === 'wi
assert.equal(r.status, 0, `gate must pass: ${r.stdout}${r.stderr}`);
assert.match(r.stdout, /arch-docs: OK/);
});

// #3322: on a pull_request run the trailer has to be in the branch commits AND in the pull request
// title/body. The gate read `git log` alone, so a branch-commit trailer was green on the pull request
// and the queue entry — whose range holds the squash of the title and body — went red and ejected it.
test('#3322: a pull request needs the trailer in the branch commits and in its own title/body', async () => {
const { filesNeedDocs } = await import('../scripts/check-arch-docs.mjs');
const map = new Map([['demo', { path: 'docs/subsystems/demo.md', ownedPaths: ['core/x.js'] }]]);
const gate = ({ log = '', body = '', files = ['core/x.js'], changed = () => false } = {}) =>
filesNeedDocs({
ref: 'HEAD',
map,
files,
changed,
logText: log,
event: { name: 'pull_request', title: 'Change the thing', body },
});
// Branch commits only: this is the bug — green on the pull request, red in the queue it feeds.
const commitOnly = gate({ log: 'Docs-not-needed: comments only' });
assert.equal(commitOnly.ok, false, 'a branch-commit trailer alone must not pass the pull request');
assert.deepEqual(commitOnly.trailer, { log: true, body: false, event: 'pull_request' });
assert.deepEqual(commitOnly.missing.map((m) => m.file), ['core/x.js']);
// Both places: the one shape the queue can read too.
assert.equal(
gate({ log: 'Docs-not-needed: comments only', body: 'Docs-not-needed: comments only' }).ok,
true,
);
// Body only: the branch commits are not there either, so the pull request stays red.
const bodyOnly = gate({ body: 'Docs-not-needed: comments only' });
assert.equal(bodyOnly.ok, false);
assert.deepEqual(bodyOnly.trailer, { log: false, body: true, event: 'pull_request' });
// Neither: red as before.
assert.equal(gate().ok, false);
// A merge-queue entry and a local run keep judging the log alone.
for (const event of [{ name: 'merge_group' }, undefined]) {
const entry = filesNeedDocs({
ref: 'HEAD',
map,
files: ['core/x.js'],
changed: () => false,
logText: 'Docs-not-needed: comments only',
event,
});
assert.equal(entry.ok, true);
}
// The documented remedy still works: the subsystem doc in the same pull request.
assert.equal(gate({ changed: () => true }).ok, true);
// No owned file changed: nothing to document, whatever the event says.
assert.equal(gate({ files: ['README.md'] }).ok, true);
});

test('#3322: the refusal names the place where the trailer is missing', async () => {
const { failureLines } = await import('../scripts/check-arch-docs.mjs');
const missing = [{ file: 'core/x.js', subsystem: 'demo', doc: join(tmpdir(), 'docs/subsystems/demo.md') }];
const commitOnly = failureLines(missing, tmpdir(), { log: true, body: false, event: 'pull_request' }).join('\n');
assert.match(commitOnly, /branch commits have `Docs-not-needed:`, but the pull request title and body do not/);
const bodyOnly = failureLines(missing, tmpdir(), { log: false, body: true, event: 'pull_request' }).join('\n');
assert.match(bodyOnly, /pull request body has `Docs-not-needed:`, but the branch commits do not/);
// A merge-queue entry has no second place, so it keeps the plain refusal.
const entry = failureLines(missing, tmpdir(), { log: false, body: true, event: 'merge_group' }).join('\n');
assert.doesNotMatch(entry, /branch commits have/);
});

test('#3322: a pull_request run reads the event the runner wrote', async (t) => {
const { pullRequestEvent } = await import('../scripts/check-arch-docs.mjs');
const dir = mkdtempSync(join(tmpdir(), 'arch-event-'));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const path = join(dir, 'event.json');
writeFileSync(path, JSON.stringify({ pull_request: { title: 'A title', body: 'Docs-not-needed: x' } }));
const savedName = process.env.GITHUB_EVENT_NAME;
const savedPath = process.env.GITHUB_EVENT_PATH;
t.after(() => {
if (savedName === undefined) delete process.env.GITHUB_EVENT_NAME;
else process.env.GITHUB_EVENT_NAME = savedName;
if (savedPath === undefined) delete process.env.GITHUB_EVENT_PATH;
else process.env.GITHUB_EVENT_PATH = savedPath;
});
process.env.GITHUB_EVENT_NAME = 'push';
assert.equal(pullRequestEvent(), undefined, 'only a pull_request run has a second place');
process.env.GITHUB_EVENT_NAME = 'pull_request';
process.env.GITHUB_EVENT_PATH = path;
assert.deepEqual(pullRequestEvent(), { name: 'pull_request', title: 'A title', body: 'Docs-not-needed: x' });
// An event the run cannot read counts as a body without the trailer, never as a pass.
process.env.GITHUB_EVENT_PATH = join(dir, 'missing.json');
assert.deepEqual(pullRequestEvent(), { name: 'pull_request', title: '', body: '' });
});
2 changes: 1 addition & 1 deletion docs/subsystems/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Tooling for setup checks, the architecture-docs gate and the container image (#3

| File | Role |
| --- | --- |
| `core/scripts/check-arch-docs.mjs` | owned_paths ↔ docs gate. Its refusal names both places a `Docs-not-needed:` trailer has to appear — the pull request body and the branch commit — because the gate reads `git log <base>..HEAD` and, on a merge-queue entry, that range is the squashed body (#1076). It hands git its arguments as a list (`execFileSync`), never a joined command line through a shell: on Windows that shell is cmd.exe, where `^` escapes the next character, so `origin/main^{commit}` reached git as `origin/main{commit}` and every scheduled windows-latest row reported `cannot diff against origin/main` on a checkout that had it (#1954). |
| `core/scripts/check-arch-docs.mjs` | owned_paths ↔ docs gate. On a `pull_request` run it requires the `Docs-not-needed:` trailer in **both** the branch commits (`git log <base>..HEAD`) and the pull request title/body (`GITHUB_EVENT_PATH`), because the merge queue judges the squash of that title and body and a trailer in a branch commit alone was green on the pull request and red in the queue (#3322). A `merge_group` entry and a local run keep the `git log` judgement, and the refusal names the place that is missing (#1076). It hands git its arguments as a list (`execFileSync`), never a joined command line through a shell: on Windows that shell is cmd.exe, where `^` escapes the next character, so `origin/main^{commit}` reached git as `origin/main{commit}` and every scheduled windows-latest row reported `cannot diff against origin/main` on a checkout that had it (#1954). |
| `core/test/check-arch-docs.test.js` | Tests for the gate. |
| `core/test/install-doctor.test.js` | Install doctor tests. |
| `plugins/README.md` | Plugin list. |
Expand Down
77 changes: 63 additions & 14 deletions patches/ds-bot/files/scripts/check-arch-docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -98,38 +98,87 @@ function fileChangedIn(ref, file) {
return out.length > 0;
}

export function hasDocsNotNeededTrailer(ref = baseRef()) {
assertBaseRef(ref);
const out = execFileSync('git', ['log', `${ref}..HEAD`, '--pretty=%B'], {
const TRAILER = /Docs-not-needed:/i;

function gitLog(ref) {
return execFileSync('git', ['log', `${ref}..HEAD`, '--pretty=%B'], {
cwd: ROOT,
encoding: 'utf8',
});
return /Docs-not-needed:/i.test(out);
}

export function filesNeedDocs({ ref = baseRef(), map = null } = {}) {
export function hasDocsNotNeededTrailer(ref = baseRef(), logText = null) {
assertBaseRef(ref);
return TRAILER.test(logText ?? gitLog(ref));
}

// The pull request this run belongs to (#3322): the merge queue squashes its title and body into the
// commit it judges, so on a `pull_request` run the trailer has to be in that text as well as in the
// branch commits. `GITHUB_EVENT_PATH` is the runner's own copy of the event; a `pull_request` run
// whose event cannot be read counts as a body without the trailer, so the gate stays red rather
// than passing on a text it could not see.
export function pullRequestEvent() {
if (process.env.GITHUB_EVENT_NAME !== 'pull_request') return undefined;
const empty = { name: 'pull_request', title: '', body: '' };
const path = process.env.GITHUB_EVENT_PATH;
if (!path) return empty;
try {
const event = JSON.parse(readFileSync(path, 'utf8'));
return {
name: 'pull_request',
title: String(event?.pull_request?.title ?? ''),
body: String(event?.pull_request?.body ?? ''),
};
} catch {
return empty;
}
}

export function filesNeedDocs({
ref = baseRef(),
map = null,
event = pullRequestEvent(),
logText = null,
files = null,
changed = null,
} = {}) {
assertBaseRef(ref);
const subsystemMap = map || loadSubsystemMap();
const files = changedFiles(ref);
if (files.length === 0) return { ok: true, missing: [], files: [] };
if (hasDocsNotNeededTrailer(ref)) return { ok: true, missing: [], files };
const touched = files ?? changedFiles(ref);
const inLog = hasDocsNotNeededTrailer(ref, logText);
// Only a pull request has a second place; a merge-queue entry and a local run judge the log alone.
const inBody = event?.name === 'pull_request'
? TRAILER.test(`${event.title ?? ''}\n${event.body ?? ''}`)
: true;
const trailer = { log: inLog, body: inBody, event: event?.name ?? 'local' };
if (touched.length === 0) return { ok: true, missing: [], files: [], trailer };
if (inLog && inBody) return { ok: true, missing: [], files: touched, trailer };
const isChanged = changed ?? (file => fileChangedIn(ref, file));
const missing = [];
for (const file of files) {
for (const file of touched) {
const match = longestMatch(file, subsystemMap);
if (!match) continue;
if (fileChangedIn(ref, match.docPath)) continue;
if (isChanged(match.docPath)) continue;
missing.push({ file, subsystem: match.subsystem, doc: match.docPath });
}
return { ok: missing.length === 0, missing, files };
return { ok: missing.length === 0, missing, files: touched, trailer };
}

// #1076: the trailer this gate looks for is read from `git log <base>..HEAD`, and on a merge-queue
// entry that range holds the squash of the pull request title and body — the branch commits are not in
// it. So "add a trailer" sent people to the commit they had just pushed, and the entry went red again.
// The refusal names both places, and says why the body counts.
export function failureLines(missing, root = ROOT) {
// The refusal names both places, and says why the body counts. Since #3322 a `pull_request` run reads
// both, and names the one that is missing rather than leaving it to the queue.
export function failureLines(missing, root = ROOT, trailer = null) {
const lines = ['arch-docs: FAIL — owned file(s) changed without subsystem doc update:'];
for (const m of missing) lines.push(` ${m.file} → ${m.subsystem} (${relative(root, m.doc)})`);
if (trailer?.event === 'pull_request' && trailer.log !== trailer.body) {
lines.push(trailer.body
? 'arch-docs: the pull request body has `Docs-not-needed:`, but the branch commits do not.'
: 'arch-docs: the branch commits have `Docs-not-needed:`, but the pull request title and body do not.');
lines.push(' The merge queue squashes the title and body into the commit it judges, so a trailer');
lines.push(' that lives only in a branch commit is gone exactly when main is judged. Add it to both.');
}
lines.push('Fix it one of two ways:');
lines.push(' - update the subsystem doc in docs/subsystems/ in this pull request, or');
lines.push(' - add a line `Docs-not-needed: <why>` to the pull request body AND to the branch commit.');
Expand All @@ -152,7 +201,7 @@ function main() {
console.log(`arch-docs: OK (${files.length} changed file(s))`);
process.exit(0);
}
for (const line of failureLines(missing)) console.error(line);
for (const line of failureLines(missing, ROOT, result.trailer)) console.error(line);
process.exit(1);
}

Expand Down
Loading
Loading