Skip to content

Call instead of inlining a body that names a module the caller can't load - #9478

Merged
cknitt merged 1 commit into
no-relative-externals-in-cmjfrom
inline-fallback-unloadable-module
Nov 30, 2026
Merged

cknitt merged 1 commit into
no-relative-externals-in-cmjfrom
inline-fallback-unloadable-module

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

A function body stored in a .cmj for other modules to inline can name modules of packages its own package depends on. A package that depends on it usually doesn't have those on its load path, so inlining the body failed the build:

QMod not found, it means either the module does not exist or it is a namespace

The caller now calls the function instead, as it would without a stored body. This is PR 4 of the plan in #8733 (problem 2.9). The check is in Lam_compile_env.query_external_id_info, which every inlining site goes through. It looks a module up without registering it, since registered modules with side effects get imported.

New test: tests/build_tests/projects/cross_module_unloadable_module, where app depends on p and p on q. PMod.f, which names QMod, is called; PMod.k, which names no module, is still inlined. The test fails without this change.

Part of stack #9486, after #9475. At this point the stored bodies are those of -bs-cross-module-opt; from #9479 on they're @inline bodies, which this covers too.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@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), call a function from another package instead of inlining it when its body names a module of a package the caller doesn't depend on. The build used to fail with "not found, it means either the module does not exist or it is a namespace".

Semantic Diff

1 commit · Full page

Call instead of inlining a body that names a module the caller can't load

3a025b4 · cknitt + Claude · Nov 29, 2026, 07:52 UTC

Why

A body stored in a .cmj can name modules of packages its own package depends on. A package depending on that one usually does not have them on its load path, so inlining the body failed the build. This is PR 4 of the plan in #8733 (problem 2.9).

JS modified (with -bs-cross-module-opt; app depends on p, p on q)

// node_modules/p/src/PMod.res
@inline let f = x => QMod.g(x) + 1
@inline let k = x => x * 2

// app/src/App.res
let viaQ = PMod.f(2)
let local = PMod.k(2)

diagnostic · Before

QMod not found, it means either the module does not exist or it is a namespace

js · After · app/src/App.mjs

import * as PMod from "p/src/PMod.mjs";

let viaQ = PMod.f(2);

let local = 4;

Review

  • Does looking a module up change the imports? No. The check reads this compile's module cache and the load path, and does not register the module, since registered modules with side effects get imported.

@cknitt
cknitt changed the base branch from master to no-relative-externals-in-cmj 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>
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 672,645,907 → 672,762,505 (+0.02%).

Generated projects: 630,257,572 → 630,418,600 (+0.03%).

Benchmark Base Head Change
build/Namespace/clean 84,577,593 84,709,903 +0.16%
packages/@rescript/runtime/Primitive_list.resi 91,889 92,021 +0.14%
packages/@rescript/runtime/Primitive_float_extern.res 93,722 93,854 +0.14%
packages/@rescript/runtime/Stdlib_Blob.res 93,939 94,071 +0.14%
packages/@rescript/runtime/Stdlib_File.res 95,748 95,880 +0.14%
packages/@rescript/runtime/Primitive_promise.res 97,098 97,230 +0.14%
packages/@rescript/runtime/Primitive_int_extern.res 97,951 98,083 +0.13%
packages/@rescript/runtime/Stdlib_Iterable.resi 98,095 98,227 +0.13%
packages/@rescript/runtime/Stdlib_AsyncIterable.resi 98,244 98,376 +0.13%
packages/@rescript/runtime/Primitive_string_extern.res 98,259 98,391 +0.13%
24 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Iterable.res 98,979 99,111 +0.13%
packages/@rescript/runtime/Stdlib_AsyncIterable.res 99,050 99,182 +0.13%
packages/@rescript/runtime/Obj.res 99,199 99,331 +0.13%
packages/@rescript/runtime/Primitive_exceptions.resi 100,317 100,449 +0.13%
packages/@rescript/runtime/Primitive_hash.resi 102,734 102,866 +0.13%
packages/@rescript/runtime/Primitive_dict.res 102,751 102,883 +0.13%
packages/@rescript/runtime/Primitive_module.resi 103,176 103,308 +0.13%
packages/@rescript/runtime/RescriptTools.res 104,614 104,746 +0.13%
packages/@rescript/runtime/Stdlib_TaggedTemplate.resi 106,060 106,192 +0.12%
packages/@rescript/runtime/Stdlib_ArrayBuffer.res 107,575 107,707 +0.12%
packages/@rescript/runtime/Stdlib_ArrayBuffer.resi 110,858 110,990 +0.12%
packages/@rescript/runtime/Stdlib_Lazy.resi 112,150 112,282 +0.12%
packages/@rescript/runtime/RescriptTools_ExtractCodeBlocks.res 112,175 112,307 +0.12%
packages/@rescript/runtime/Primitive_array_extern.res 114,259 114,391 +0.12%
packages/@rescript/runtime/Primitive_object_extern.res 114,770 114,902 +0.12%
packages/@rescript/runtime/Stdlib_TaggedTemplate.res 115,509 115,641 +0.11%
packages/@rescript/runtime/Stdlib_Bool.resi 116,370 116,502 +0.11%
packages/@rescript/runtime/Stdlib_Intl_Segments.res 116,638 116,770 +0.11%
packages/@rescript/runtime/Primitive_object.resi 117,091 117,223 +0.11%
packages/@rescript/runtime/Dom_storage2.res 120,224 120,356 +0.11%
packages/@rescript/runtime/Jsx.res 121,779 121,911 +0.11%
packages/@rescript/runtime/Primitive_option.resi 122,181 122,313 +0.11%
packages/@rescript/runtime/Stdlib_JsExn.resi 122,267 122,399 +0.11%
packages/@rescript/runtime/Stdlib_WeakSet.res 123,211 123,343 +0.11%

Instructions

In total: 9,226,022,495 → 9,225,746,195 (±0.00%).

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

Output size

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

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

Pull request 3a025b4 compared with base c4fcf27 in this run.

@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.45%. Comparing base (66d9f5d) to head (3a025b4).

Files with missing lines Patch % Lines
compiler/backend/lam_compile_env.ml 90.00% 2 Missing ⚠️
Additional details and impacted files
@@                      Coverage Diff                      @@
##           no-relative-externals-in-cmj    #9478   +/-   ##
=============================================================
  Coverage                         82.45%   82.45%           
=============================================================
  Files                               465      465           
  Lines                             65966    65984   +18     
=============================================================
+ Hits                              54390    54406   +16     
- Misses                            11576    11578    +2     
Files with missing lines Coverage Δ
compiler/backend/lam_compile_env.ml 86.36% <90.00%> (+0.94%) ⬆️
🚀 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.

@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:20.628857Z 6e3616e 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. 👍

Reviewed commit: b94c6dee72

ℹ️ 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@9478

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 3a025b4

@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. Hooray!

Reviewed commit: 6e3616e37e

ℹ️ 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 inline-fallback-unloadable-module branch from 6e3616e to 72eea42 Compare November 29, 2026 09:57
…load

Why:
    A body stored in a .cmj can name modules of packages its own package
    depends on. A package depending on that one usually does not have them
    on its load path, so inlining the body failed the build. This is PR 4
    of the plan in #8733 (problem 2.9).

JS modified (with -bs-cross-module-opt; app depends on p, p on q):
Example:
    // node_modules/p/src/PMod.res
    @inline let f = x => QMod.g(x) + 1
    @inline let k = x => x * 2

    // app/src/App.res
    let viaQ = PMod.f(2)
    let local = PMod.k(2)
Before (diagnostic):
    QMod not found, it means either the module does not exist or it is a namespace
After (js): app/src/App.mjs
    import * as PMod from "p/src/PMod.mjs";

    let viaQ = PMod.f(2);

    let local = 4;

Review: Does looking a module up change the imports? No. The check reads
this compile's module cache and the load path, and does not register the
module, since registered modules with side effects get imported.

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 inline-fallback-unloadable-module branch from 72eea42 to 3a025b4 Compare November 29, 2026 10:07
@cknitt
cknitt marked this pull request as ready for review November 29, 2026 12:08
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12
@cknitt
cknitt merged commit baa47d5 into master Nov 30, 2026
42 of 45 checks passed
@cknitt
cknitt deleted the inline-fallback-unloadable-module 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