Repository navigation
Description
Problem
compiler/ext/config.ml holds hand-numbered magic numbers for .cmi (Caml1999I036), .cmt (Caml1999T043) and marshaled .ast files (ResImpl01315 / ResIntf01315). Every PR that changes a serialized type bumps them, so parallel PRs constantly conflict on the same lines. That's the same problem we had with CHANGELOG.md before fragments. There have been about 20 bumps since August.
The per-PR precision buys nothing:
- Rebuilds after a compiler change are driven by
bsc_hash, which both build systems store, not by the magic numbers. - Outside tools only ever see released versions, so intermediate bumps between two releases are invisible to them.
- A bump forgotten in a PR fails silently: mixing files across a layout change crashes in
Marshalinstead of giving a clean error.
The Caml1999 prefix is a separate problem. OCaml 5.5 uses Caml1999I037 / Caml1999T037, so our I036 is most likely a real OCaml release's .cmi number, and the next bump would collide with OCaml 5.5. An OCaml tool would then accept a ReScript .cmi and misread it, instead of rejecting it.
What outside tools need
Tools that read .cmi/.cmt files may rely on the magic number. They need:
- A stable number for each release.
- A change between releases only when the format may have changed, so they can tell that two releases share a format.
- Ordering, for "built by a newer compiler" checks (
cmi_format.mlalready uses it for its older/newer error).
Proposal
Bump the counters in the release-prep step, not in PRs:
-
Keep plain counters in
compiler/ext/config.ml, but switch them once to our own prefixes (e.g.ResCmi037/ResCmt043, like the.astnumbers already did), so OCaml tools reject our files.cmi_format.ml's older/newer check is updated to the new prefix. Add a changelog note because outside tools will see this. -
PRs never touch the counters.
compiler/AGENTS.mdandCONTRIBUTING.mdsay so. -
yarn changelog:release, which already produces the release PR, also bumps each counter whose sources changed since the previous release tag (git diff --quiet <tag> HEAD -- <paths>):.ast:compiler/ml/parsetree.ml.cmi:compiler/mlandcompiler/ext.cmt: the same paths
Whole directories make it unlikely that a type change slips through. The previous release tag is the record, so there's no checked-in digest file, no dune rule and no extra CI check.
Trade-offs:
- Almost every minor or major release bumps the counters, even if the format happened to stay the same. Patch releases usually don't bump, so a tool built for 12.3.0 keeps reading 12.3.1 files.
- Two dev builds between releases share a counter. Rebuilds are already driven by
bsc_hash, so only unusual mixes are affected, such as PR preview packages ofrescriptand@rescript/runtimefrom different PRs. Those crash instead of giving a clean error.
Not in scope: .cmj has no magic number yet, only a digest of its own content (js_cmj_format.ml has a TODO). It could get a counter under the same scheme later. The frozen Parsetree0 PPX numbers (Caml1999M022 / N022) must never change and stay as they are.
Alternatives considered
- A build-time digest of the serialized types plus a checked-in record. CI or the release step compares the digest with the record and bumps the counter only on a real change; dev builds could append the digest. It is more precise, but needs a dune rule, a record file and a check, all to avoid bumps that cost outside tools little.
- The compiler version as the magic number. It changes on every release, including patch releases with an unchanged format, and prereleases have no simple ordering.
- A pure content digest. Exact and never conflicts, but outside tools can't see that two releases share a format, and there's no ordering.
Activity
[-]Derive the .cmi / .cmt / .ast magic numbers instead of bumping them in every PR[/-][+]Bump the .cmi / .cmt / .ast magic numbers at release time instead of in every PR[/+]Bump the magic numbers at release time instead of in pull requests
Bump the magic numbers at release time instead of in pull requests (#…
Bump the magic numbers at release time instead of in pull requests (r…