I’ve been working on making our Elixir application build reproducibly under Bazel. After enabling deterministic, fixing the build path and normalising timestamps, we still had a bunch of BEAM files that differed between builds of identical sources.
This matters to us because we share cached build results between developers and CI. When a compilation has to run again, we want unchanged inputs to produce unchanged output. A dependency producing a slightly different BEAM file can invalidate our Dialyzer PLT and the builds downstream of it, even though nothing meaningful has changed.
The common cause was code iterating maps whose keys contained atoms.
For small maps, atom keys are ordered by their atom-table indices. For large maps, the internal hash uses those indexes so iteration order depends on them too and atoms nested inside tuple or list keys can have the same effect.
It is race condition which process happens to create a giiven atom first.
Parallel compilation is one source of variation but also the boot loader loads modules in parallel, before any of our build code runs. I found that even just two bare VM starts can assign different indices to thousands of atoms!
We found this showing up in several fairly ordinary places.. like for example:
Postgrex.ErrorCodegroups error codes into a map and then generates clauses from it.- Spark walks option maps while generating functions. Other modules had differences from struct defaults in debug information, maps escaped into literals, or a map being inspected into
@doc.
None of these generators should rely on an unspecified iteration order. Sorting in the generator CAN fix this but getting a reproducible build this way means finding every relevant call site in every dependency, including places where the map traversal happens inside the compiler. it was a bit of a wack-a-mole situation and dependencies can break it with a new vesion. So we looked into fixing it at the source.
Previous Proof Earlier Wack-a-model fixes
I got the AI to look for previous reports of undeterminism in the map. There have already been fixes at several levels:
| Reference | What it addressed |
|---|---|
| OTP #5153 | Large map literals could produce different BEAM files despite deterministic. Added deterministic encoding to term_to_binary/2 and made the compiler use it for literals. |
| Elixir #12350 | Reported unstable exports_md5 values caused by encoding large maps. Closed without merging because Elixir already avoided that particular problem through list-based struct metadata. |
| Elixir #12418 | Added explicit sorting when expanding struct fields following the map-ordering change in OTP 26. |
| Elixir #13517 | Sorted map fields when converting type descriptions to quoted syntax, making the representation deterministic and fixing a flaky test. |
| OTP #11748 → #11774 | Atom creation order affected the compiler’s type analysis and generated code. The fix made traversal of exported functions use a consistent order. |
There is also related work in public_key #11396. and a broader reproducible-builds issue, #4417, dates back to 2020.
What I tried
My first attempt was to generate a boot script that pre-interned the atoms used by the build in a fixed order. That helped, but it required a complete atom list as a build input. Both not practical and defeated the point of not compiling more than needed.
The prototype adds an opt-in emulator flag, +zdao true, for “deterministic atom order”. While i think this should be enabled under deterministic builds, I figured it would be easier to merge if it had its own flag initially. It defaults to false.
When enabled the internal hash uses the atom’s name hash rather than its table index, and small flat maps compare atom keys by term order rather than index order.
The prototype
There is a prototype based on OTP 29.1, roughly 160 lines across ten files. There is also a prebuilt Ubuntu 24.04 x86-64 release.
I’d appreciate a review of that assumption, particularly around other consumers of the internal hash and any remaining code that assumes flat-map atoms are in index order.
We are now running this fork and it has stabilized out bazel build setup for the Elixir app.
(Edited to remove AI slop)