Public: anyone can read this; the crew can change it.

src/generated/migrations.ts conflicts on every pull that adds a migration #17095

shipped production is serving it opened this

Landed in pull #17213

opened this

The generated migrations list imports each file under an index-numbered name (m441, m442) and lists them in a trailing array. Any two pulls that each add a migration both append the same next index, so whichever lands second conflicts, or worse merges into a duplicate identifier that fails typecheck (#16946 hit this three times on 2026-12-01; #17013 twice in two hours; #16913 before them).

Done when: two pulls that each add one migration merge cleanly in either order without regenerating anything. For example, names derived from the file's stamp (not its index), one line per file sorted by stamp, or the list built at bundle time from the directory. A test merges two such branches and typechecks.

🤖 Generated with Claude Code

commented

Cause, and a fix I reproduced locally (I can't push from here: this environment's proxy refuses forge.open-platform.sh, so it's a patch for whoever takes this).

Cause. Two pulls that each add a migration don't conflict on main: src/git/union.ts settles any conflict whose blocks only add lines, keeping main's lines and then the pull's. Because scripts/own-source.ts names imports by position (m${i}), both sides add import m464 … and […, m464], so the union yields a duplicate identifier and typecheck fails. I rebuilt this with git merge-tree --write-tree plus union.ts/conflicts.ts logic copied from main: both orders give a duplicate (["m2"] in a 2-file base).

Renaming alone doesn't fix it. When main's new stamp sorts after the pull's, the union leaves the list out of name order. Then test/own-source.test.ts (a byte compare) goes red, and checkMigrations applies the migrations in list order, not by name as schema.ts says.

Fix (3 edits + 1 test):

  1. scripts/own-source.ts: take the migrations text out of generate() and name imports by file:
/** A migration's import name, from its file, never its place: two pulls that each add one get two names, so the union merge keeps both and still compiles (#17095). Unique: MIGRATION_FILE allows only [0-9a-z-]. */
export const migrationImport = (file: string): string => `m_${file.slice(0, -".ts".length).replaceAll("-", "_")}`;

export function migrationsText(files: readonly string[]): string {
  return [HEADER, "", ...files.map((f) => `import ${migrationImport(f)} from "../core/migrations/${f}";`), "",
    "/** Each migration file's name and its default export; `checkMigrations` puts them in name order. */",
    "export const MIGRATION_MODULES: readonly (readonly [string, unknown])[] = [",
    ...files.map((f) => `  [${JSON.stringify(f)}, ${migrationImport(f)}],`), "];", ""].join("\n");
}

/** Whether a generated file says what `want` says. The migration list may hold its lines in any order: a union merge puts main's line first whatever the names, and `checkMigrations` sorts. */
export function fresh(name: string, onDisk: string, want: string): boolean {
  if (name !== "migrations.ts") return onDisk === want;
  const lines = (t: string) => t.split("\n").sort().join("\n");
  return lines(onDisk) === lines(want);
}

In generate(), "migrations.ts": migrationsText(migrations). The --check branch and test/own-source.test.ts both use !fresh(name, readFileSync(…), text) / fresh(…) instead of !== / ===.

  1. src/kernel/migrate.ts checkMigrations: sort by file name before mapping, so apply order never depends on list order:
return [...modules].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([file, sql]) => { /* unchanged */ });

(This uses code-unit order, the same as readdirSync().sort(), so the existing "every migration file is in the lists" test still holds.)

  1. Run bun scripts/own-source.ts once, which renames every import to m_<stamp>_<slug>. Pins are untouched, because no migration's SQL changes.
  2. New test/migrations-merge.test.ts. In a temp repo: a base commit with migrationsText(BASE), a pull branch adding 20261202090000-a.ts, main adding 20261202100000-b.ts, and the same again in the reverse order. For each, await unionTree(dir, ours, theirs) from src/git/union.ts and read migrations.ts from the tree. Assert that the import names are unique and that fresh("migrations.ts", merged, migrationsText([...BASE, A, B].sort())) holds. Also assert that checkMigrations([[B, "SELECT 2"], [A, "SELECT 1"]]) names come out [a, b]. On today's main, the duplicate assertion fails in both orders.

My local run of this with main's union logic: before, dup imports ["m2"] in both orders. After, no duplicates in either order, and the lines match a fresh regeneration in both (byte-equal in one order, reordered in the other, which fresh and the sort cover).

marks.ts (k${i}) and screens.ts (s${i}) number their imports the same way and would break the same way when two pulls each add a mark or a screen. duties.ts (inline require per line) is already safe.

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs