Repository navigation
fix(lint): compare contract snapshots by TypeScript tokens, not bytes, so the ORM's minified snapshots pass - #481
Conversation
…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>
|
✅ Gizmo reviewed ac9b25c — posted 1 inline comment(s) this pass. Open findings: 🟡 1 minor Change walkthroughOverview. This PR changes the CI snapshot check (scripts/lint-contract-snapshots.mjs) to compare each contract's committed The comparison. The new scripts/declaration-tokens.mjs validates a file with The lint script. Per-group comparison now tokenizes each 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 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. |
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
commit: |
| const tokensOf = (file) => { | ||
| try { | ||
| return declarationTokens(readFileSync(file, 'utf8')); | ||
| } catch (error) { | ||
| problems.push(`${file}\n does not parse: ${error.message}`); | ||
| return undefined; | ||
| } | ||
| }; |
There was a problem hiding this comment.
🟡 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.
The CI check that compares each migration snapshot's
contract.d.tswith the project's owncontract.d.tsnow 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 timeprisma migration planwrites 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.jsonandcontract.d.ts, and a copy undermigrations/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 covercontract.jsononly, so a re-emit can change the types incontract.d.tswithout 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
scripts/declaration-tokens.mjsparses 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.pnpm contracts:emit.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 emitdoes not write a deleted application snapshot; I checked that in theorm-demoexample..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.mdalready lists a planned CI check that snapshots match whatcontract emitwrites. Its entry now says that check cannot take over from this one: the ORM rewrites a snapshot only when itscontract.jsonis out of date, so a snapshot whosecontract.d.tsalone 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.${...}in a template literal type was read as code, so a//there hid every later declaration, and JSDoc type syntax such as?stringwas dropped. Both are fixed and tested.Agent: herodotus-23
🤖 Generated with Claude Code