Skip to content

[12.x] Fix rewatch compiling one more level of unchanged dependents on every build - #9545

Open
sohamm20 wants to merge 4 commits into
rescript-lang:maintenance/12.xfrom
sohamm20:rewatch-expired-deps-cmi
Open

sohamm20 wants to merge 4 commits into
rescript-lang:maintenance/12.xfrom
sohamm20:rewatch-expired-deps-cmi

Conversation

@sohamm20

@sohamm20 sohamm20 commented Dec 6, 2026

Copy link
Copy Markdown

Checklist

Fixes #9487

Problem

When a module compiles again but its .cmi doesn't change, rewatch correctly skips its dependents in that build. At the start of the next build, mark_modules_with_expired_deps_dirty compares the dependent's .cmt with the dependency's .cmt. That .cmt was rewritten, so every skipped dependent looks expired and compiles. Their .cmi doesn't change either, so the next build compiles the level below them, and so on. In a project with about 2,800 modules, one body edit made every following no-change build compile 770 to 2,400 modules (16 to 40 s).

Comparing against the dependency's .cmi time, as the comment in that function intends, isn't enough on its own:

  • For a module with an interface file, rewatch compiles the .resi on every compile, and bsc rewrites the .cmi with identical bytes and a new mtime.
  • Where the lib/bs → lib/ocaml copy doesn't keep the mtime (Linux), every compile gives the .cmi a new time.

Change

  • compile: rewatch already hashes the lib/ocaml .cmi before and after compiling a module. When the hash is unchanged, it now restores the .cmi's previous mtime, so the .cmi time only moves when the interface changes.
  • mark_modules_with_expired_deps_dirty: a dependent is expired when its .cmt is older than the dependency's .cmi, not its .cmt. The same change applies to the namespace branch. This also fixes the first match, which read (module.last_compiled_cmt, module.last_compiled_cmt).

A dependent still compiles whenever a dependency's interface changed after it last compiled, including after an interrupted build. Interface changes within a build are still found by the existing hash check.

Tests

  • tests/compile.sh: after a dependency compiles with its interface unchanged, the next build doesn't compile its dependent. One case uses a plain module (Dep02 → Dep01) and one a module with a .resi (InternalDep → Main). Reverting the comparison fails the first case; removing the mtime restore fails the second.
  • Snapshots rename-interface-file.txt and rename-file-with-interface.txt: Compiled 3 modules → Compiled 2 modules. The extra module was this bug: the steps before them leave the Dep02 → Dep01 → Main chain partly compiled, and each build compiled one more level of it with no source change.
  • cargo test passes (69), cargo fmt --check is clean, and the other suite scripts pass (watch, lock, suffix, format, clean, experimental, compiler-args). Run on macOS arm64 with the 12.3.1 bsc.

Manual checks:

  • The 5-module chain from rewatch: a no-op recompile makes each later no-change build recompile one more level of dependents #9487: after touch A.res, the builds compiled 1 1 1 1 1 0 modules before the fix and 1 0 0 0 0 0 after. The same holds when A has a .resi and its body changes.
  • An @inline value that changes still updates the dependent's JS in the same build.
  • A dependent whose .cmt is older than the dependency's .cmi, as after an interrupted build, still compiles.
  • On a project of about 2,800 modules, no-change builds compile 0 modules after every edit, and an interface change still compiles everything that depends on it. After a series of edits, all 4,579 generated .js files matched a clean build.
  • In watch mode, saving an unrelated file after a body edit compiles 1 module instead of 2.

… build

A module that compiles again with an unchanged interface gets a new .cmt, and the next build compared its dependents with that .cmt. Every dependent the previous build had rightly skipped then looked expired and compiled, one dependency level per build. Mark a dependent expired when its .cmt is older than the dependency's .cmi instead, and keep the .cmi's old mtime when a compile leaves its content unchanged, since bsc rewrites it for modules with an interface file and the copy to lib/ocaml gives it a new time on some platforms.

Fixes rescript-lang#9487

Signed-off-by: Soham Mirikar <82722112+sohamm20@users.noreply.github.com>
…endents later

Nothing caught the extra compiles of rescript-lang#9487. One case covers a plain module (Dep02 -> Dep01) and one a module with a .resi (InternalDep -> Main); reverting the .cmi comparison fails the first, and dropping the mtime restore fails the second.

Signed-off-by: Soham Mirikar <82722112+sohamm20@users.noreply.github.com>
The extra module was this bug: the steps before these snapshots leave the Dep02 -> Dep01 -> Main chain partly compiled, and each build compiled one more level of it with no source change.

Signed-off-by: Soham Mirikar <82722112+sohamm20@users.noreply.github.com>
The fix changes what users see on every build, so it needs release notes. maintenance/12.x records fixes in CHANGELOG.md directly, as rescript-lang#9318 and rescript-lang#9319 did, rather than in changes/ fragments.

Signed-off-by: Soham Mirikar <82722112+sohamm20@users.noreply.github.com>
@pkg-pr-new

pkg-pr-new Bot commented Dec 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/darwin-arm64

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

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@9545

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 99ff385

This branch has not been deployed

No deployments
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.

1 participant