Skip to content

fix: preserve topological lib order in shared jsoo runtime - #14438

Merged
shonfeder merged 2 commits into
ocaml:mainfrom
vouillon:runtime-fix
May 11, 2026
Merged

shonfeder merged 2 commits into
ocaml:mainfrom
vouillon:runtime-fix

Conversation

@vouillon

@vouillon vouillon commented May 6, 2026

Copy link
Copy Markdown
Member

The sort in Runtime_key.Decoded.of_libs made the digest invariant under input reordering, but it also reordered the libs fed back to jsoo_runtime_files in setup_shared_runtime_rule. Since later JS files override earlier ones in the runtime, alphabetical order produced a different runtime than the topological order each consumer would have built on its own, silently switching which conflicting binding wins.

Drop the sort. Stanzas with the same closure and the same requires_link order still share; stanzas that disagree on order get separate runtimes rather than a shared one with arbitrary contents.

Lib_name.compare (Lib.name a) (Lib.name b))
|> List.map ~f:Lib.name
in
let lib_names = List.map libs_with_runtime ~f:Lib.name in

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Need a comment here to avoid regressions. The best would be to have a regression test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Maybe promote the test before and after the fix so that we see the fix and its effect in the same commit

@hhugo hhugo added the jsoo Js_of_ocaml support in dune label May 7, 2026
Two libs where alphabetical and topological orders disagree: lib_a
depends on lib_z, both contribute a runtime.js. Verifies the
build-runtime command lists lib_z (the dep) before lib_a, matching
what each consumer would have produced unshared.

Signed-off-by: Jérôme Vouillon <[email protected]>
@vouillon

vouillon commented May 7, 2026

Copy link
Copy Markdown
Member Author

Should that be in 3.23.1?

@hhugo

hhugo commented May 7, 2026

Copy link
Copy Markdown
Contributor

Should that be in 3.23.1?

I think it should, Maybe comment on #14441

@vouillon vouillon mentioned this pull request May 7, 2026
20 tasks done
@hhugo hhugo mentioned this pull request May 7, 2026
2 tasks done
@shonfeder
shonfeder requested a review from hhugo May 10, 2026 21:56

@shonfeder shonfeder left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this has been reviewed by a domain expert, I think it's good to merge and it just needs a changelog entry. @hhugo or @vouillon, could you provide that, since you'll know best how to communicate the relevant fix to users?

Thanks!

The sort in Runtime_key.Decoded.of_libs made the digest invariant under
input reordering, but it also reordered the libs fed back to
jsoo_runtime_files in setup_shared_runtime_rule. Since later JS files
override earlier ones in the runtime, alphabetical order produced a
different runtime than the topological order each consumer would have
built on its own, silently switching which conflicting binding wins.

Drop the sort. Stanzas with the same closure and the same requires_link
order still share; stanzas that disagree on order get separate runtimes
rather than a shared one with arbitrary contents.

Signed-off-by: Jérôme Vouillon <[email protected]>
@vouillon

Copy link
Copy Markdown
Member Author

I have added the changelog entry.

@shonfeder

Copy link
Copy Markdown
Member

Very clear, that. Thank you! 🙏

@shonfeder
shonfeder merged commit 5634e49 into ocaml:main May 11, 2026
12 checks passed
@vouillon
vouillon deleted the runtime-fix branch May 11, 2026 20:54
shonfeder added a commit that referenced this pull request May 12, 2026
Backport #14438 onto 3.23.1-rc
shonfeder added a commit to ocaml/opam-repository that referenced this pull request May 19, 2026
CHANGES:

### Fixed

- Fix the `menhir` opam dependency injection introduced in 3.23. Dune
  now only fills in the lower bound `{>= "20180523"}` on an existing
  user-declared `menhir` dependency; it no longer adds `menhir` as a
  new dependency to packages that did not declare it themselves.
  (ocaml/dune#14434, fixes ocaml/dune#14428, @robinbb)

- Gate the `dune` version-bound deduplication in generated opam files
  (introduced in 3.23) on `(lang dune 3.23)`. Projects at earlier lang
  versions get the prior `And [...]` shape — e.g.
  `{>= "3.17" & >= "3.20"}` — restoring 3.22 behaviour and avoiding a
  silent change to opam output on dune-binary upgrade. (ocaml/dune#14436,
  @robinbb)

- Preserve library order when building a shared jsoo standalone runtime.
  (ocaml/dune#14438, @vouillon)

- Fix the fallback to the secondary compiler, allowing recovering of support for
  packages with upper bounds on OCaml less than 4.14. Packages depending on dune
  3.23.1 or later and with an upper bound on the OCaml compiler that is less
  than 4.14, will now be able to use the latest dune, but dune will be built
  with the secondary compiler at version 4.14. (ocaml/dune#14443, @Alizter)

- Fix the bootstrap on NetBSD by including `<sys/wait.h>` in `lev_stubs.c`,
  matching the existing FreeBSD/OpenBSD guard.
  (ocaml/dune#14512, fixes ocaml/dune#14484, @0-wiz-0)
pull Bot pushed a commit to metanivek/dune that referenced this pull request May 19, 2026
The sort in `Runtime_key.Decoded.of_libs` made the digest invariant
under input reordering, but it also reordered the libs fed back to
`jsoo_runtime_files` in `setup_shared_runtime_rule`. Since later JS
files override earlier ones in the runtime, alphabetical order produced
a different runtime than the topological order each consumer would have
built on its own, silently switching which conflicting binding wins.

Drop the sort. Stanzas with the same closure and the same requires_link
order still share; stanzas that disagree on order get separate runtimes
rather than a shared one with arbitrary contents.

(cherry picked from commit 5634e49)
Signed-off-by: Shon Feder <[email protected]>
@Alizter Alizter added this to the 3.23.1 milestone Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jsoo Js_of_ocaml support in dune

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants