Skip to content

fix(lint): compare contract snapshots by TypeScript tokens, not bytes, so the ORM's minified snapshots pass - #481

Merged
wmadden-electric merged 3 commits into
mainfrom
fix/snapshot-lint-ignores-formatting
Dec 2, 2026
Merged

wmadden-electric merged 3 commits into
mainfrom
fix/snapshot-lint-ignores-formatting

Conversation

@wmadden-electric

Copy link
Copy Markdown
Contributor

The CI check that compares each migration snapshot's contract.d.ts with the project's own contract.d.ts now compares the two as TypeScript tokens, not byte for byte. Prisma ORM writes snapshot files minified on purpose, while the project's file is formatted for people to read, so the check failed on correct files. It would have failed again each time prisma migration plan writes a snapshot in one of this repository's example apps. The pull request also puts back the minified snapshots an earlier fix had overwritten.

Background

In an app that uses the ORM, each contract is kept in two places: the project's emitted contract.json and contract.d.ts, and a copy under migrations/snapshots/<hash>/, which each migration imports its "before" and "after" contracts from. The ORM writes both. scripts/lint-contract-snapshots.mjs, run in CI's Lint job, checks that copies of one contract carry the same types. It exists because the hashes cover contract.json only, so a re-emit can change the types in contract.d.ts without changing any hash, and nothing then updates the snapshot's copy. The upgrade to ORM 8.0.0-rc.1 left every snapshot's types out of date that way.

It compared the files byte for byte. The move to ORM 8.0.0-rc.27 (#479) had the ORM rewrite four snapshots in its minified form. Three of them failed the check; the fourth, in the auth example, is not compared, because its contract differs from the project's in the auth module's version. I made the check pass by copying the formatted file over those three. That was the wrong fix: the minified form is the ORM's choice, to keep pull requests short.

What changes

  • The comparison. A new scripts/declaration-tokens.mjs parses each file with TypeScript, which the scripts already depend on, and compares the tokens in order. Whitespace and comments are ignored, and a string literal compares by its value, whatever its quotes. Everything else counts, including member order, so this is a strict check that tolerates layout only. The ORM's minified file is its formatted output with the whitespace and comments collapsed, so trailing commas and semicolons match too: the check passes on all three snapshots today. A file that does not parse, such as one with merge conflict markers, is reported by name.
  • The snapshots. The three snapshots I had overwritten are back to the minified files the ORM wrote.
  • The fix-it message no longer says to copy the formatted file over snapshots. It says how to have the ORM write a stale snapshot again:
    • An extension's snapshot (one an installed extension such as the auth module ships): delete its folder and run pnpm contracts:emit.
    • The application's own snapshot: delete its folder and run prisma migration plan --from <ref>, naming a ref file that points at that snapshot's hash. The ORM then stops with an error that gives the steps for writing the snapshot again. prisma contract emit does not write a deleted application snapshot; I checked that in the orm-demo example.
  • The agent rule for contract files (.agents/rules/emitted-contracts.mdc) said snapshots are frozen copies that may be edited by hand. It now says the ORM writes them, minified, and that nobody edits them.
  • .drive/deferred.md already lists a planned CI check that snapshots match what contract emit writes. Its entry now says that check cannot take over from this one: the ORM rewrites a snapshot only when its contract.json is out of date, so a snapshot whose contract.d.ts alone is stale would pass it.

Alternatives considered: running the repository's formatter over both files before comparing ties the check to the formatter's settings and to files the formatter skips on purpose; having snapshots import the project's types would defeat a snapshot's purpose, which is to keep a contract's types as they were.

Testing

  • scripts/declaration-tokens.test.mjs (6 tests): minified and formatted files with the same declarations compare equal; a changed type, string, modifier, member order, template literal text, string escape or JSDoc-syntax type does not; a file with conflict markers or an unterminated string is refused.
  • The lint passes on the repository's snapshots, fails when a declaration in one is changed, and names the file when one does not parse.
  • Two review rounds found two ways the first version could pass files that really differ: text after a ${...} in a template literal type was read as code, so a // there hid every later declaration, and JSDoc type syntax such as ?string was dropped. Both are fixed and tested.

Agent: herodotus-23

🤖 Generated with Claude Code

…o the ORM's minified snapshots pass

Signed-off-by: willbot <w.a.madden+machine@gmail.com>
Signed-off-by: Will Madden <madden@prisma.io>
… not parse, and say how to fix a stale snapshot

Signed-off-by: willbot <w.a.madden+machine@gmail.com>
Signed-off-by: Will Madden <madden@prisma.io>
…named, and the fix-it message covers extension snapshots

Signed-off-by: willbot <w.a.madden+machine@gmail.com>
Signed-off-by: Will Madden <madden@prisma.io>
@wmadden-electric
wmadden-electric requested a review from a team as a code owner December 2, 2026 21:52
@prisma-gizmo

prisma-gizmo Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

✅ Gizmo reviewed ac9b25c — posted 1 inline comment(s) this pass.

Open findings: 🟡 1 minor

Change walkthrough

Overview. This PR changes the CI snapshot check (scripts/lint-contract-snapshots.mjs) to compare each contract's committed contract.d.ts copies as TypeScript tokens instead of bytes, so the snapshots the ORM deliberately writes minified pass without being overwritten with formatted output. It also restores the three minified snapshots an earlier fix had replaced, rewrites the fix-it guidance, and updates the agent rule accordingly.

The comparison. The new scripts/declaration-tokens.mjs validates a file with ts.transpileModule diagnostics (a conflict-markered or unterminated file throws and is reported by name), then walks the AST collecting leaf tokens: whitespace and comments vanish, string literals compare by cooked value so quoting style is irrelevant, while member order, modifiers, semicolons, and trailing commas all still count. Reading template-literal-type head/tail as raw text keeps // inside a template hole from hiding later declarations, and JSDoc type syntax such as ?string is kept — both were holes in the first draft and are now regression-tested. typescript is an existing root devDependency and .mjs scripts aren't typechecked, so the import fits the repo's setup.

The lint script. Per-group comparison now tokenizes each contract.d.ts; parse failures are reported as problems rather than crashing or comparing, and the fix-it message distinguishes extension snapshots (delete folder, re-run pnpm contracts:emit) from the application's own (delete folder, prisma migration plan --from <ref>), no longer telling anyone to copy the formatted file over a snapshot.

Docs. .agents/rules/emitted-contracts.mdc:11 now says the ORM writes snapshot copies minified and nobody edits them, and .drive/deferred.md:377 records that the planned emit-based snapshot check can't replace this one (a contract.d.ts-only-stale snapshot would pass it).

Verification. All six tests in scripts/declaration-tokens.test.mjs pass under the pinned TypeScript 6.0.3, and the lint passes on the repository (4 contracts agree). The three restored snapshots are confirmed single-line minified files; the auth example's formatted snapshot is intentionally outside the comparison since its contract content differs.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: d94c0e8c-a1fe-41fd-b4f9-12ec76462d3c
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@pkg-pr-new

pkg-pr-new Bot commented Dec 2, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@prisma/composer@481
npm i https://pkg.pr.new/@prisma/composer-cli@481
npm i https://pkg.pr.new/@prisma/composer-prisma-cloud@481

commit: ac9b25c

@prisma-gizmo prisma-gizmo Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New findings: 🟡 1 minor · trace

Comment on lines +90 to +97
const tokensOf = (file) => {
try {
return declarationTokens(readFileSync(file, 'utf8'));
} catch (error) {
problems.push(`${file}\n does not parse: ${error.message}`);
return undefined;
}
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Minor · tests — The lint script's changed comparison and reporting paths have no automated test

The PR changes how copies are compared (tokens instead of bytes), adds the does-not-parse report, and rewrites the fix-it message, but scripts/declaration-tokens.test.mjs only covers the token functions. Nothing tests the script itself: the differs from report, the does not parse report, the skip-the-group path when the first typed file fails to parse, or the new fix-it text — these were verified by hand per the PR description. Sibling lint scripts are tested end-to-end in a temp git repo, including failure output: scripts/lint-control-bytes.test.mjs (git init, git add, spawn the script) and scripts/lint-casts.test.mjs, so the wiring is established and feasible here (trackedJson() shells out to git ls-files).

Recommended fix

Add a small end-to-end test following the scripts/lint-control-bytes.test.mjs pattern: init a temp git repo, track two directories each with a contract.json (same canonical content) plus contract.d.ts copies (one formatted, one minified with the same declarations → exit 0), then assert exit 1 with the differs from and does not parse messages when a declaration is changed or conflict markers are introduced.

@wmadden-electric
wmadden-electric merged commit 1858053 into main Dec 2, 2026
25 checks passed
@wmadden-electric
wmadden-electric deleted the fix/snapshot-lint-ignores-formatting branch December 2, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants