Skip to content

Commit ca47142

Browse files
noreplyclaude
andcommitted
fork: schema check guards re-exporting barrels and fails closed on untraced names
A name imported from a package root that no file is seen to declare now guards the whole barrel and everything it re-exports, instead of dropping out of the guarded set. The tracer also knows export async function, a local export { type X }, export { default as X } from, and destructured exports. The barrels on the way to a declaring file are guarded too, so a barrel-only edit cannot re-point a name a migration imports. On a branch holding commits already on main, a real edit to a check file is reported as one, and the message no longer assumes main was rebased. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a7328e4 commit ca47142

2 files changed

Lines changed: 244 additions & 46 deletions

File tree

‎.github/scripts/fork-schema-check.cjs‎

Lines changed: 101 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// strands installs that return to upstream. Usage: fork-schema-check.cjs <base> <head>.
33
// Refused: migrations, schema setup and what they import; shipped source files holding a schema
44
// statement; a PR copy of this check or of .gitattributes that differs from the base's copy; and a
5-
// branch built on an old (since rebased) base. Files are read as git blobs.
5+
// branch holding commits already on the base (built on an old, since rebased base, or copying its
6+
// commits). Files are read as git blobs.
67

78
const { execFileSync } = require("node:child_process");
89
const path = require("node:path");
@@ -57,7 +58,9 @@ const isShippedSource = (file) => SHIPPED_SOURCE.test(file) && !isTest(file);
5758
/**
5859
* Files the schema roots import, transitively: relative and package subpath imports, and for a
5960
* package root (`@t3tools/contracts`) the files that declare the names imported from it. The
60-
* root's barrel itself is not added, so the rest of the package stays free to change.
61+
* barrels on the way to a declaring file are guarded too (a barrel edit could re-point a name),
62+
* but their other re-exports are not followed, so the rest of the package stays free to change.
63+
* A name that cannot be traced guards the whole barrel and everything it re-exports.
6164
* @param {{ files: string[]; has(file: string): boolean; read(file: string): string }} tree
6265
* @returns {Set<string>}
6366
*/
@@ -67,14 +70,18 @@ function schemaClosure(tree) {
6770
(file) => SCHEMA_ROOTS.some((pattern) => pattern.test(file)) && !isTest(file),
6871
);
6972
const closure = new Set(queue);
73+
const walked = new Set(queue);
7074
while (queue.length > 0) {
7175
const file = queue.pop();
7276
const text = tree.read(file);
7377
const names = importedNames(text);
7478
for (const match of text.matchAll(IMPORT_SPECIFIER)) {
75-
for (const target of resolveImport(tree, packages, file, match[1], names.get(match[1]))) {
76-
if (!isTest(target) && !closure.has(target)) {
79+
const reached = resolveImport(tree, packages, file, match[1], names.get(match[1]));
80+
for (const target of reached.barrels) if (!isTest(target)) closure.add(target);
81+
for (const target of reached.files) {
82+
if (!isTest(target) && !walked.has(target)) {
7783
closure.add(target);
84+
walked.add(target);
7885
queue.push(target);
7986
}
8087
}
@@ -122,15 +129,21 @@ function importedNames(text) {
122129
return names;
123130
}
124131

125-
/** The files an import reaches: one file, or for a package root the files declaring `names`. */
132+
/**
133+
* The files an import reaches: one file, or for a package root the files declaring `names`
134+
* (`files`, whose own imports matter) and the barrels re-exporting them (`barrels`, guarded only).
135+
* @returns {{ files: string[]; barrels: string[] }}
136+
*/
126137
function resolveImport(tree, packages, from, specifier, names) {
138+
const none = { files: [], barrels: [] };
139+
const whole = (file) => ({ files: [file], barrels: [] });
127140
if (specifier.startsWith(".")) {
128141
const target = resolveFile(tree, path.posix.join(path.posix.dirname(from), specifier));
129-
return target ? [target] : [];
142+
return target ? whole(target) : none;
130143
}
131144
const parts = specifier.match(/^(@[^/]+\/[^/]+)(?:\/(.+))?$/);
132145
const pkg = parts && packages.get(parts[1]);
133-
if (!pkg || !pkg.exports) return [];
146+
if (!pkg || !pkg.exports) return none;
134147
const exports =
135148
typeof pkg.exports === "string" || "import" in pkg.exports || "default" in pkg.exports
136149
? { ".": pkg.exports }
@@ -140,36 +153,42 @@ function resolveImport(tree, packages, from, specifier, names) {
140153
typeof entry === "string" ? entry : entry && (entry.import ?? entry.default ?? entry.types);
141154
const file =
142155
typeof target === "string" ? resolveFile(tree, path.posix.join(pkg.dir, target)) : undefined;
143-
if (!file) return [];
144-
if (parts[2] || !names || names.has("*") || names.has("default")) return [file];
156+
if (!file) return none;
157+
if (parts[2] || !names || names.has("*") || names.has("default")) return whole(file);
145158
const found = new Set();
146-
for (const name of names) declaringFiles(tree, packages, file, name, found, new Set());
147-
return [...found];
159+
const barrels = new Set();
160+
for (const name of names) {
161+
// A name no file is seen to declare fails closed: the barrel is walked whole.
162+
if (!declaringFiles(tree, packages, file, name, found, barrels, new Map())) return whole(file);
163+
}
164+
return { files: [...found], barrels: [...barrels] };
148165
}
149166

150-
/** Follows `export * from` and `export { name } from` until a file declares `name`. */
151-
function declaringFiles(tree, packages, file, name, found, seen) {
152-
if (seen.has(`${file}\0${name}`)) return;
153-
seen.add(`${file}\0${name}`);
167+
/**
168+
* Follows `export * from` and `export { name } from` until a file declares `name`. Adds declaring
169+
* files to `found` and the re-exporting files on the way to `barrels`.
170+
* @returns {boolean} whether `name` was traced to a declaring file from `file`
171+
*/
172+
function declaringFiles(tree, packages, file, name, found, barrels, seen) {
173+
const key = `${file}\0${name}`;
174+
if (seen.has(key)) return seen.get(key);
175+
seen.set(key, false);
154176
const text = tree.read(file);
155-
const word = name.replace(/\$/g, "\\$");
156-
const declares = new RegExp(
157-
String.raw`\bexport\s+(?:declare\s+)?(?:abstract\s+)?(?:const|let|var|function\*?|class|type|interface|enum|namespace)\s+${word}(?![\w$])`,
158-
);
159-
const local = [...text.matchAll(/\bexport\s+(?:type\s+)?\{([^}]*)\}(?!\s*from)/g)].some(
160-
(match) => match[1].split(",").some((part) => part.trim().split(/\s+as\s+/).pop() === name),
161-
);
162-
if (declares.test(text) || local) {
177+
if (declaresName(text, name)) {
163178
found.add(file);
164-
return;
179+
seen.set(key, true);
180+
return true;
165181
}
182+
let traced = false;
166183
for (const match of text.matchAll(
167184
/\bexport\s+(?:type\s+)?(\*(?:\s+as\s+([\w$]+))?|\{[^}]*\})\s*from\s*["']([^"'\n]+)["']/g,
168185
)) {
169186
const [, clause, namespace, specifier] = match;
170-
const targets = resolveImport(tree, packages, file, specifier, new Set(["*"]));
187+
const targets = resolveImport(tree, packages, file, specifier, new Set(["*"])).files;
171188
if (clause.startsWith("*") && !namespace) {
172-
for (const target of targets) declaringFiles(tree, packages, target, name, found, seen);
189+
for (const target of targets) {
190+
if (declaringFiles(tree, packages, target, name, found, barrels, seen)) traced = true;
191+
}
173192
continue;
174193
}
175194
const exported = namespace
@@ -183,9 +202,36 @@ function declaringFiles(tree, packages, file, name, found, seen) {
183202
if (!exported) continue;
184203
for (const target of targets) {
185204
if (exported === "*") found.add(target);
186-
else declaringFiles(tree, packages, target, exported, found, seen);
205+
if (exported === "*" || declaringFiles(tree, packages, target, exported, found, barrels, seen)) {
206+
traced = true;
207+
}
187208
}
188209
}
210+
if (traced) barrels.add(file);
211+
seen.set(key, traced);
212+
return traced;
213+
}
214+
215+
/**
216+
* Whether `text` declares and exports `name` itself: a declaration (`export async function`,
217+
* `export const { name } = ...` and the like), a local `export { [type] x as name }`, or for
218+
* `default` an `export default`.
219+
*/
220+
function declaresName(text, name) {
221+
const word = name.replace(/\$/g, "\\$");
222+
const bound = String.raw`(?<![\w$])${word}(?![\w$])`;
223+
const declarations = [
224+
String.raw`\bexport\s+(?:declare\s+)?(?:abstract\s+)?(?:async\s+)?(?:const|let|var|function\s*\*?|class|type|interface|enum|namespace|module)\s*${bound}`,
225+
// Destructuring: `export const { a, b: name } = o` or `export const [name] = list`.
226+
String.raw`\bexport\s+(?:const|let|var)\s*[{[][^=;]*?${bound}[^=;]*?[}\]]\s*(?::[^=;]*)?=`,
227+
];
228+
if (name === "default") declarations.push(String.raw`\bexport\s+default\b`);
229+
if (declarations.some((pattern) => new RegExp(pattern).test(text))) return true;
230+
return [...text.matchAll(/\bexport\s+(?:type\s+)?\{([^}]*)\}(?!\s*from)/g)].some((match) =>
231+
match[1]
232+
.split(",")
233+
.some((part) => part.trim().replace(/^type\s+/, "").split(/\s+as\s+/).pop().trim() === name),
234+
);
189235
}
190236

191237
function resolveFile(tree, file) {
@@ -195,44 +241,46 @@ function resolveFile(tree, file) {
195241
}
196242

197243
/**
198-
* @param {ReadonlyArray<{ path: string; before?: string; after?: string; sameAsBase?: boolean }>} changes
244+
* @param {ReadonlyArray<{ path: string; before?: string; after?: string; sameAsBase?: boolean; fromOldMain?: boolean }>} changes
199245
* one entry per path the branch changes since the merge-base; `before`/`after` are the file
200-
* texts (undefined when absent); `sameAsBase` says the branch's copy equals the base's copy
246+
* texts (undefined when absent); `sameAsBase` says the branch's copy equals the base's copy;
247+
* `fromOldMain` says the branch's copy is the one a commit already on main left there
201248
* @param {{ closure: ReadonlySet<string>; baseHasCheck: boolean; staleCommits?: number }} context
202249
* `staleCommits`: the branch's commits that the base already holds as rebased copies
203250
* @returns {string[]} one reason per refused file
204251
*/
205252
function findSchemaChanges(changes, { closure, baseHasCheck, staleCommits = 0 }) {
206253
const reasons = [];
207-
const rebase =
208-
"this branch is based on an old main that has since been rebased; rebase it onto main";
209254
if (staleCommits > 0) {
210255
reasons.push(
211-
`${staleCommits} commit(s) of this branch are already on main as rebased copies, so ${rebase}.`,
256+
`${staleCommits} commit(s) of this branch are already on main (it is based on an old main, ` +
257+
"or it copies main's commits), so rebase it onto main.",
212258
);
213259
}
260+
const also = staleCommits > 0 ? " (also rebase onto main)" : "";
214261
for (const change of changes) {
215262
const file = change.path;
216263
const guarded = CHECK_FILES.includes(file) || GITATTRIBUTES.test(file);
217264
// The branch carries main's copy (e.g. it branched before main was rebased): nothing to judge.
218265
if (guarded && change.sameAsBase) continue;
219-
if (guarded && staleCommits > 0) {
220-
reasons.push(`${file}: differs from main's copy because ${rebase}.`);
266+
// Main's own older copy, carried by commits already on main: only the rebase is needed.
267+
if (guarded && staleCommits > 0 && change.fromOldMain) {
268+
reasons.push(`${file}: differs from main's copy, because this branch needs a rebase onto main.`);
221269
continue;
222270
}
223271
if (CHECK_FILES.includes(file)) {
224272
if (baseHasCheck) {
225273
reasons.push(
226274
`${file}: changes the schema check itself. Only the owner may do that: the check stays ` +
227-
"red, and the owner merges past it.",
275+
`red, and the owner merges past it${also}.`,
228276
);
229277
}
230278
continue;
231279
}
232280
if (GITATTRIBUTES.test(file)) {
233281
reasons.push(
234282
`${file}: changes how git treats files, which can hide changes from review. Only the ` +
235-
"owner may do that.",
283+
`owner may do that${also}.`,
236284
);
237285
continue;
238286
}
@@ -321,16 +369,20 @@ function blobAt(rev, file) {
321369
}
322370

323371
/**
324-
* The branch's commits (merge-base..head) that the base already holds as rebased copies, matched
325-
* by author, author time and subject: a rebase keeps all three. Needs no file contents.
372+
* The branch's commits (merge-base..head) that the base already holds as copies (rebased or
373+
* cherry-picked), matched by author, author time and subject: both keep all three. Needs no file
374+
* contents. Returns the branch-side commit ids.
326375
*/
327-
function staleCommitCount(mergeBase, base, head) {
376+
function staleCommits(mergeBase, base, head) {
328377
const ids = (range) =>
329-
git(["log", "--no-merges", "--format=%an%x00%ae%x00%at%x00%s", range])
378+
git(["log", "--no-merges", "--format=%H%x00%an%x00%ae%x00%at%x00%s", range])
330379
.split("\n")
331-
.filter(Boolean);
332-
const onBase = new Set(ids(`${mergeBase}..${base}`));
333-
return ids(`${mergeBase}..${head}`).filter((id) => onBase.has(id)).length;
380+
.filter(Boolean)
381+
.map((line) => ({ sha: line.slice(0, line.indexOf("\0")), id: line.slice(line.indexOf("\0")) }));
382+
const onBase = new Set(ids(`${mergeBase}..${base}`).map((commit) => commit.id));
383+
return ids(`${mergeBase}..${head}`)
384+
.filter((commit) => onBase.has(commit.id))
385+
.map((commit) => commit.sha);
334386
}
335387

336388
function main(argv) {
@@ -346,11 +398,16 @@ function main(argv) {
346398
// Only shipped source is searched for statements, so only its texts are read.
347399
const text = (entry, sha) => (sha && isShippedSource(entry.path) ? readBlob(sha) : undefined);
348400
const guarded = (file) => CHECK_FILES.includes(file) || GITATTRIBUTES.test(file);
401+
const stale = staleCommits(mergeBase, base, head);
402+
// A guarded file's copy that one of those commits left: main's older copy, not a branch edit.
403+
const fromOldMain = (entry) =>
404+
stale.some((sha) => blobAt(sha, entry.path) === entry.newBlob);
349405
const changes = entries.map((entry) => ({
350406
path: entry.path,
351407
before: text(entry, entry.oldBlob),
352408
after: text(entry, entry.newBlob),
353409
sameAsBase: guarded(entry.path) ? blobAt(base, entry.path) === entry.newBlob : undefined,
410+
fromOldMain: guarded(entry.path) && stale.length > 0 ? fromOldMain(entry) : undefined,
354411
}));
355412
const closure = new Set([
356413
...schemaClosure(readTree(mergeBase)),
@@ -359,7 +416,7 @@ function main(argv) {
359416
const reasons = findSchemaChanges(changes, {
360417
closure,
361418
baseHasCheck: treeHas(base, CHECK_SCRIPT),
362-
staleCommits: staleCommitCount(mergeBase, base, head),
419+
staleCommits: stale.length,
363420
});
364421
if (reasons.length === 0) {
365422
console.log(`No database schema changes (${changes.length} changed files checked).`);

0 commit comments

Comments
 (0)