Skip to content

Compile dependents again when a module's .cmj changes - #9473

Merged
cknitt merged 2 commits into
strip-cmj-locationsfrom
rebuild-on-cmj-change
Nov 30, 2026
Merged

cknitt merged 2 commits into
strip-cmj-locationsfrom
rebuild-on-cmj-change

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

The build system now compiles dependents again when a module's .cmj changes, not only its .cmi. This is PR 2 of the plan in #8733 and the build-system part of #8689. It's stacked on #9470.

Dependents read the .cmj:

  • the values they copy in (true, false, null and undefined always, more with -bs-cross-module-opt);
  • the arities they call with;
  • whether the module has side effects, which decides whether they import it at all.

All of these can change without the interface. With a .resi, such a change used to leave dependents' JavaScript stale. Two examples, both now covered by a test:

  • A.resi has let flag: bool, and A.res changes let flag = true to false. B.mjs kept let f = true.
  • A.res gains a Console.log. B.mjs kept no import of A.mjs, so the effect never ran.

What changes in build_system/compiler_process.ml:

  • The .cmj is held in memory like the .cmi and written only when it changed.
  • A changed .cmj counts as an interface change, and moves the .cmi's modification time, so a later build sees it too.
  • A failed compile leaves the previous .cmj in place.

In watch mode this compiles more only when a .cmj actually changes. With default flags it holds no function bodies, so it changes only when something dependents read changes. #9470 keeps line shifts from changing a .cmj that does store bodies. The .cmi is dated before a changed .cmj is written, so a build killed in between still compiles the dependents next time; a unit test covers it.

New tests:

  • tests/build_tests/integration/compile/24-rebuild-dependents-for-cmj-change.sh: the two cases above, and a line shift that compiles only the edited module. It fails on master at the first case.
  • In tests/build_tests/unit/compiler_process_tests.ml: a changed .cmj is a change, with and without an interface, and a failed compile keeps the previous .cmj.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@cknitt
cknitt changed the base branch from master to strip-cmj-locations November 29, 2026 06:57
@cknitt
cknitt added this pull request to stack #9474 November 29, 2026 06:57
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Compile dependents again when a module changes in a way its interface doesn't show, such as a new value behind a .resi or a new side effect. Their JavaScript used to keep the old value, or miss the import that runs the side effect.

Semantic Diff

2 commits · Full page

Compile dependents again when a module's .cmj changes

74bd719 · cknitt + Claude · Nov 29, 2026, 06:49 UTC

Why

Dependents read a module's .cmj: the values they copy in (true, false, null and undefined always, more with -bs-cross-module-opt), the arities they call with, and whether the module has side effects, which decides whether they import it. The build system only compared .cmi files, so a change the interface doesn't show left their JavaScript stale. Without a .resi this was mostly hidden, because the .cmi holds declaration locations; with one, every such change went unnoticed. This is the build-system part of #8689, and PR 2 of the plan in #8733. #9470 keeps line shifts from changing a .cmj that stores bodies.

Representation modified (publishing a compile)

mapping · publish_compiler_artifacts

 .cmi changed ↦ dependents compiled again
-.cmj changed, .cmi unchanged ↦ nothing
-.cmj ↦ written by the compiler, checked to exist
+.cmj changed, .cmi unchanged ↦ dependents compiled again; the .cmi's modification time moves, for the next build
+.cmj ↦ held until the compile is published, written only when it changed, kept as before when the compile fails

JS modified (dependent of a module with an interface)

// A.resi
let flag: bool
// A.res, after the first build: was `let flag = true`
let flag = false
Console.log("effect")
// B.res
let f = A.flag

js · B.mjs after the second build

-let f = true;
+import * as A from "./A.mjs";
 
+let f = false;
+
 export {
   f,
 }
-/* No side effect */
+/* A Not a pure module */

Review

  • Does watch mode compile more? Only when a .cmj changes. With default flags it holds no bodies, so it changes only when an exported value's arity, one of the four copied constants or the module's purity changes, and each of those changes the dependents' JavaScript.
  • Why move the .cmi's modification time? A later build decides from .cmi times whether dependents are current, so it sees the .cmj change too without reading every .cmj.
Date the interface before writing a changed .cmj

2c97086 · cknitt + Claude · Nov 29, 2026, 09:01 UTC

A changed .cmj was written before its .cmi was dated. A build killed in between found the .cmj unchanged the next time and left the dependents stale, and a failed date reported the interface as unchanged. The .cmi is now dated, and the change recorded, right before the .cmj is written. A review by Claude (Fable model) found this.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.44%. Comparing base (fafe0cc) to head (2c97086).

Additional details and impacted files
@@                   Coverage Diff                   @@
##           strip-cmj-locations    #9473      +/-   ##
=======================================================
- Coverage                82.45%   82.44%   -0.01%     
=======================================================
  Files                      465      465              
  Lines                    65961    65961              
=======================================================
- Hits                     54385    54383       -2     
- Misses                   11576    11578       +2     

see 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 672,521,286 → 672,646,295 (+0.02%).

Generated projects: 630,054,013 → 630,258,448 (+0.03%).

Benchmark Base Head Change
build/Namespace/clean 84,416,901 84,577,929 +0.19%

Instructions

In total: 9,225,692,775 → 9,225,692,775 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,303,219 → 27,303,219 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 2c97086 compared with base f199ac4 in this run.

@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T10:09:32.249639Z 2c97086 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 9b256a5a00

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9473

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9473

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9473

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9473

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9473

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9473

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9473

commit: 2c97086

@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0cfad17664

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Why:
    Dependents read a module's .cmj: the values they copy in (true, false,
    null and undefined always, more with -bs-cross-module-opt), the arities
    they call with, and whether the module has side effects, which decides
    whether they import it. The build system only compared .cmi files, so a
    change the interface doesn't show left their JavaScript stale. Without a
    .resi this was mostly hidden, because the .cmi holds declaration
    locations; with one, every such change went unnoticed. This is the
    build-system part of #8689, and PR 2 of the plan in #8733. #9470 keeps
    line shifts from changing a .cmj that stores bodies.

Representation modified (publishing a compile):
Before (mapping): publish_compiler_artifacts
    .cmi changed ↦ dependents compiled again
    .cmj changed, .cmi unchanged ↦ nothing
    .cmj ↦ written by the compiler, checked to exist
After (mapping): publish_compiler_artifacts
    .cmi changed ↦ dependents compiled again
    .cmj changed, .cmi unchanged ↦ dependents compiled again; the .cmi's modification time moves, for the next build
    .cmj ↦ held until the compile is published, written only when it changed, kept as before when the compile fails

JS modified (dependent of a module with an interface):
Example:
    // A.resi
    let flag: bool
    // A.res, after the first build: was `let flag = true`
    let flag = false
    Console.log("effect")
    // B.res
    let f = A.flag
Before (js): B.mjs after the second build
    let f = true;

    export {
      f,
    }
    /* No side effect */
After (js): B.mjs after the second build
    import * as A from "./A.mjs";

    let f = false;

    export {
      f,
    }
    /* A Not a pure module */

Review: Does watch mode compile more? Only when a .cmj changes. With
default flags it holds no bodies, so it changes only when an exported
value's arity, one of the four copied constants or the module's purity
changes, and each of those changes the dependents' JavaScript.

Review: Why move the .cmi's modification time? A later build decides from
.cmi times whether dependents are current, so it sees the .cmj change too
without reading every .cmj.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the rebuild-on-cmj-change branch from 0cfad17 to 7587566 Compare November 29, 2026 09:57
A changed .cmj was written before its .cmi was dated. A build killed in
between found the .cmj unchanged the next time and left the dependents
stale, and a failed date reported the interface as unchanged. The .cmi is
now dated, and the change recorded, right before the .cmj is written. A
review by Claude (Fable model) found this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the rebuild-on-cmj-change branch from 7587566 to 2c97086 Compare November 29, 2026 10:07
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 2c97086fc6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12

@cristianoc cristianoc left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The module-level invalidation is a deliberate limitation worth documenting: if an exported @inline body changes A.cmj, every direct dependent of A recompiles, even if it never calls that function. This is a reasonable correctness trade-off. The .cmj also carries arities and side-effect information, and the build does not track which parts each dependent used. Per-use invalidation can be a follow-up if the extra rebuilds turn out to matter.

@cknitt
cknitt merged commit d9fd6f0 into master Nov 30, 2026
23 checks passed
@cknitt
cknitt deleted the rebuild-on-cmj-change branch November 30, 2026 07:53
cknitt added a commit that referenced this pull request Nov 30, 2026
Why:
    `@inline` on a function inlined its calls only in its own module.
    Other modules inlined it only with -bs-cross-module-opt, which also
    inlined every small closed function, annotated or not. `@inline` now
    means the same in every module: an exported `@inline` function or
    functor stores its body in the .cmj, and other modules inline it,
    flag or not. The Option helpers that #8689 marked are `@inline`. This
    is PR 5 of the plan in #8733 (proposal 3) and replaces the compiler
    part of #8689, without the `@inline(crossModule)` payload.

JS modified (another module, without -bs-cross-module-opt):
Example:
    // A.res
    @inline let g = x => x + 1
    let h = x => x * 2

    // B.res
    let a = A.g(3)
    let b = A.h(4)
Before (js): B
    let a = A.g(3);

    let b = A.h(4);
After (js): B
    let b = A.h(4);

    let a = 4;

JS modified (an Option helper):
Example:
    // tests/belt_tests/src/bs_stack_test.res
    while current->Option.isSome { … }
Before (js):
    while (Stdlib_Option.isSome(current)) {
After (js):
    while (current !== undefined) {

Representation modified (which bodies a .cmj stores):
Before (mapping): Lam_stats_export.values_of_export, Js_cmj_format.get_result
    true, false, null, undefined ↦ always
    @inline function or functor, closed ↦ with -bs-cross-module-opt
    other closed function under size 5, closed functor ↦ with -bs-cross-module-opt
After (mapping): Lam_stats_export.values_of_export, Js_cmj_format.get_result
    true, false, null, undefined ↦ always
    @inline function or functor, closed, not async, no directive ↦ always
    other closed function under size 5, closed functor ↦ with -bs-cross-module-opt

Review: What keeps a body from being stored? It must be closed, since it
is copied without its environment, not async and without a directive, and
it must not call a relative-path external (#9475). A caller that can't
load a module the body names calls the function instead (#9478).

Review: Which functions does this cover? Those defined at the top level
of a module. A function in a nested module (`module M = { @inline let f
= … }`) and an `@inline let rec` are still inlined only in their own
module.

Review: Does the caller rebuild when the body changes? Yes, through the
changed .cmj (#9473); tests/build_tests/projects/inline_function_rebuild
edits a body on the same line, which leaves the .cmi as it is.

Review: Can a JS global be captured by the import of a module whose uses
were all inlined? Not any more. Such a module is still imported when it has
side effects, so `Js_pass_external_shadow` now counts the modules imported
for their side effects as bindings in scope (`Lam_compile_env.side_effect_modules`,
which `populate_required_modules` uses too). Before, it saw only the modules
the generated code still named and printed the global bare, which then read
the import (inline_shadowed_import_test.res). Found by marking every function
in tests/tests `@inline`.


Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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