Skip to content

Keep bodies that call relative-path externals out of other modules - #9475

Merged
cknitt merged 1 commit into
rebuild-on-cmj-changefrom
no-relative-externals-in-cmj
Nov 30, 2026
Merged

cknitt merged 1 commit into
rebuild-on-cmj-changefrom
no-relative-externals-in-cmj

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

With -bs-cross-module-opt, a function that calls an external with a relative path, such as @module("./helper.js"), is no longer stored in the .cmj for other modules to inline. This is PR 3 of the plan in #8733 (problem 2.8).

The import path of such an external is relative to its own file. The type checker already keeps the external itself out of other modules. But the wrapper it's exported through, and any small function calling it, were still stored. A module in another directory that used them then imported ./helper.js relative to itself:

// src/app/App.mjs, using src/ffi/Hidden.res
import * as HelperJs from "./helper.js";   // the file is in src/ffi/
let direct = HelperJs.raw(1);

Now it calls Hidden.raw(1) and Hidden.wrapped(2), as it does without the flag. A body that calls an external with a package path, such as @module("node:path"), is still inlined. The runtime's and Belt's JavaScript, which are built with the flag, don't change.

New test: tests/build_tests/projects/cross_module_relative_external builds such a project, checks App.mjs, and runs it. It fails without this change.

Part of stack #9486, after #9473. At this point in the stack, the bodies stored for other modules are those of -bs-cross-module-opt; #9479 then stores @inline bodies without the flag, and #9480 removes the flag. The guard here applies to all stored bodies, so it covers both.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@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-29T09:32:38.968760Z 65e54b1 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.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • When inlining functions from other modules (until now only with -bs-cross-module-opt), leave out those that call an external with a relative path, such as @module("./helper.js"). Inlined into a module in another directory, their import path pointed to the wrong file.

Semantic Diff

1 commit · Full page

Keep bodies that call relative-path externals out of other modules

66d9f5d · cknitt + Claude · Nov 29, 2026, 07:18 UTC

Why

An external with a package-relative path, such as @module("./helper.js"), is imported relative to its own file. The type checker keeps the external itself out of other modules, but with -bs-cross-module-opt the wrapper it is exported through, and any small function calling it, were stored in the .cmj and inlined into modules in other directories, which then imported ./helper.js relative to themselves. This is PR 3 of the plan in #8733 (problem 2.8).

JS modified (with -bs-cross-module-opt)

// src/ffi/Hidden.res
@module("./helper.js") external raw: int => int = "raw"
@inline let wrapped = x => raw(x) + 1

// src/app/App.res
let direct = Hidden.raw(1)
let wrapped = Hidden.wrapped(2)

js · src/app/App.mjs

 import * as Hidden from "../ffi/Hidden.mjs";
-import * as HelperJs from "./helper.js";
 
-let direct = HelperJs.raw(1);
+let direct = Hidden.raw(1);
 
-let wrapped = HelperJs.raw(2) + 1 | 0;
+let wrapped = Hidden.wrapped(2);

Review

  • Does inlining change otherwise? No. A body that calls an external with a package path, such as @module("node:path"), is still inlined, and the runtime's and Belt's JS, built with the flag, are unchanged.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: bfab7c0aae

ℹ️ 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".

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 672,641,564 → 672,643,362 (±0.00%).

Generated projects: 630,251,795 → 630,251,889 (±0.00%).

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

Instructions

In total: 9,225,692,499 → 9,226,022,431 (±0.00%).

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

Output size

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

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

Pull request 66d9f5d compared with base bcbd50d in this run.

@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.45%. Comparing base (2c97086) to head (66d9f5d).

Additional details and impacted files
@@                  Coverage Diff                   @@
##           rebuild-on-cmj-change    #9475   +/-   ##
======================================================
  Coverage                  82.44%   82.45%           
======================================================
  Files                        465      465           
  Lines                      65961    65966    +5     
======================================================
+ Hits                       54383    54390    +7     
+ Misses                     11578    11576    -2     
Files with missing lines Coverage Δ
compiler/backend/lam_stats_export.ml 94.11% <100.00%> (+0.63%) ⬆️

... and 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.

@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@9475

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 66d9f5d

@github-actions

Copy link
Copy Markdown

@cknitt
cknitt marked this pull request as ready for review November 29, 2026 07:45
@cknitt
cknitt force-pushed the no-relative-externals-in-cmj branch from bfab7c0 to fb1d307 Compare November 29, 2026 08:03
@cknitt
cknitt changed the base branch from master to rebuild-on-cmj-change November 29, 2026 08:04
@cknitt
cknitt added this pull request to stack #9474 November 29, 2026 08:04
cknitt added a commit that referenced this pull request Nov 29, 2026
The changelog entries of #9475 and #9478 began with "With
-bs-cross-module-opt", which this pull request removes. Both fixes apply
to every inlined body, so the entries now say so without the flag.

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 no-relative-externals-in-cmj branch from fb1d307 to 65e54b1 Compare November 29, 2026 09:26
@cknitt
cknitt removed this pull request from stack #9474 November 29, 2026 09:28
@cknitt
cknitt added this pull request to stack #9486 November 29, 2026 09:29
@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. More of your lovely PRs please.

Reviewed commit: 65e54b1470

ℹ️ 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 force-pushed the no-relative-externals-in-cmj branch from 65e54b1 to 0cdf1e6 Compare November 29, 2026 09:57
Why:
    An external with a package-relative path, such as
    `@module("./helper.js")`, is imported relative to its own file. The
    type checker keeps the external itself out of other modules, but with
    -bs-cross-module-opt the wrapper it is exported through, and any small
    function calling it, were stored in the .cmj and inlined into modules in
    other directories, which then imported `./helper.js` relative to
    themselves. This is PR 3 of the plan in #8733 (problem 2.8).

JS modified (with -bs-cross-module-opt):
Example:
    // src/ffi/Hidden.res
    @module("./helper.js") external raw: int => int = "raw"
    @inline let wrapped = x => raw(x) + 1

    // src/app/App.res
    let direct = Hidden.raw(1)
    let wrapped = Hidden.wrapped(2)
Before (js): src/app/App.mjs
    import * as Hidden from "../ffi/Hidden.mjs";
    import * as HelperJs from "./helper.js";

    let direct = HelperJs.raw(1);

    let wrapped = HelperJs.raw(2) + 1 | 0;
After (js): src/app/App.mjs
    import * as Hidden from "../ffi/Hidden.mjs";

    let direct = Hidden.raw(1);

    let wrapped = Hidden.wrapped(2);

Review: Does inlining change otherwise? No. A body that calls an external
with a package path, such as `@module("node:path")`, is still inlined, and
the runtime's and Belt's JS, built with the flag, are unchanged.

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 no-relative-externals-in-cmj branch from 0cdf1e6 to 66d9f5d Compare November 29, 2026 10:07
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12
@cknitt
cknitt merged commit b293c4c into master Nov 30, 2026
42 of 45 checks passed
@cknitt
cknitt deleted the no-relative-externals-in-cmj 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