From 66d9f5d8d42b09948f1b1d202c3840391bc16479 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 29 Nov 2026 07:18:41 +0000 Subject: [PATCH] Keep bodies that call relative-path externals out of other modules 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 Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D Signed-off-by: Christoph Knittel --- changes/cross-module-relative-external.fix.md | 1 + compiler/backend/lam_stats_export.ml | 14 +++++++ .../cross_module_relative_external/.gitignore | 1 + .../cross_module_relative_external/input.js | 40 +++++++++++++++++++ .../rescript.json | 14 +++++++ .../src/app/App.res | 3 ++ .../src/ffi/Hidden.res | 7 ++++ .../src/ffi/helper.js | 3 ++ 8 files changed, 83 insertions(+) create mode 100644 changes/cross-module-relative-external.fix.md create mode 100644 tests/build_tests/projects/cross_module_relative_external/.gitignore create mode 100644 tests/build_tests/projects/cross_module_relative_external/input.js create mode 100644 tests/build_tests/projects/cross_module_relative_external/rescript.json create mode 100644 tests/build_tests/projects/cross_module_relative_external/src/app/App.res create mode 100644 tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res create mode 100644 tests/build_tests/projects/cross_module_relative_external/src/ffi/helper.js diff --git a/changes/cross-module-relative-external.fix.md b/changes/cross-module-relative-external.fix.md new file mode 100644 index 0000000000..cfd5b2dc96 --- /dev/null +++ b/changes/cross-module-relative-external.fix.md @@ -0,0 +1 @@ +- 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. diff --git a/compiler/backend/lam_stats_export.ml b/compiler/backend/lam_stats_export.ml index b3f042572c..73e3dd853d 100644 --- a/compiler/backend/lam_stats_export.ml +++ b/compiler/backend/lam_stats_export.ml @@ -26,6 +26,19 @@ let single_na = Js_cmj_format.single_na +(* An external with a package-relative path, such as [@module("./x.js")], is + imported relative to its own file, so a body that calls one breaks when + inlined in a module in another directory. The type checker keeps such an + external itself out of other modules (Primitive.no_cross_module_inline); + this keeps out the functions that call it, including the wrapper it + exports the external through. *) +let rec calls_relative_external (lam : Lambda.t) = + (match lam with + | Lprim {primitive = Pjs_call {prim_name; ffi}} -> + External_ffi_types.is_relative ffi ~prim_name + | _ -> false) + || Lambda_traverse.shallow_exists calls_relative_external lam + let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) : Js_cmj_format.cmj_value Map_string.t = List.fold_left @@ -61,6 +74,7 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) : optlam | Some lambda -> if not !((Js_config.current ()).cross_module_inline) then None + else if calls_relative_external lambda then None else if Lam_analysis.safe_to_inline lambda (* when inlning a non function, we have to be very careful, diff --git a/tests/build_tests/projects/cross_module_relative_external/.gitignore b/tests/build_tests/projects/cross_module_relative_external/.gitignore new file mode 100644 index 0000000000..5c11fb2e43 --- /dev/null +++ b/tests/build_tests/projects/cross_module_relative_external/.gitignore @@ -0,0 +1 @@ +src/**/*.mjs diff --git a/tests/build_tests/projects/cross_module_relative_external/input.js b/tests/build_tests/projects/cross_module_relative_external/input.js new file mode 100644 index 0000000000..402a508014 --- /dev/null +++ b/tests/build_tests/projects/cross_module_relative_external/input.js @@ -0,0 +1,40 @@ +// @ts-check + +// With -bs-cross-module-opt, a body that calls an external with a path +// relative to its own file must not be inlined into a module in another +// directory, where that path would point elsewhere. + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuildOrThrow, execClean } = setup(import.meta.dirname); + +await execBuildOrThrow(); + +const app = await fs.readFile( + path.join(import.meta.dirname, "src", "app", "App.mjs"), + "utf8", +); + +assert.doesNotMatch(app, /helper\.js/, "App must not import Hidden's helper"); +assert.match(app, /Hidden\.raw\(1\)/, "the external is called through Hidden"); +assert.match( + app, + /Hidden\.wrapped\(2\)/, + "its wrapper is called through Hidden", +); +assert.match( + app, + /from "node:path"/, + "a body calling a package-path external is still inlined", +); + +// App.mjs is a build output tsc cannot resolve, so the URL is computed at run time +const mod = await import(new URL("./src/app/App.mjs", import.meta.url).href); +assert.equal(mod.direct, 10); +assert.equal(mod.wrapped, 21); +assert.equal(mod.base, "b.txt"); + +await execClean(); diff --git a/tests/build_tests/projects/cross_module_relative_external/rescript.json b/tests/build_tests/projects/cross_module_relative_external/rescript.json new file mode 100644 index 0000000000..17c3ec773e --- /dev/null +++ b/tests/build_tests/projects/cross_module_relative_external/rescript.json @@ -0,0 +1,14 @@ +{ + "name": "cross_module_relative_external", + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true + }, + "suffix": ".mjs", + "compiler-flags": ["-bs-cross-module-opt"], + "warnings": { "error": true } +} diff --git a/tests/build_tests/projects/cross_module_relative_external/src/app/App.res b/tests/build_tests/projects/cross_module_relative_external/src/app/App.res new file mode 100644 index 0000000000..d41efcf075 --- /dev/null +++ b/tests/build_tests/projects/cross_module_relative_external/src/app/App.res @@ -0,0 +1,3 @@ +let direct = Hidden.raw(1) +let wrapped = Hidden.wrapped(2) +let base = Hidden.base("/a/b.txt") diff --git a/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res b/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res new file mode 100644 index 0000000000..b8360cdc1c --- /dev/null +++ b/tests/build_tests/projects/cross_module_relative_external/src/ffi/Hidden.res @@ -0,0 +1,7 @@ +// A path relative to this file: the call must stay in this module. +@module("./helper.js") external raw: int => int = "raw" +@inline let wrapped = x => raw(x) + 1 + +// A package path: still inlined into other modules. +@module("node:path") external basename: string => string = "basename" +@inline let base = p => basename(p) diff --git a/tests/build_tests/projects/cross_module_relative_external/src/ffi/helper.js b/tests/build_tests/projects/cross_module_relative_external/src/ffi/helper.js new file mode 100644 index 0000000000..964edce5ef --- /dev/null +++ b/tests/build_tests/projects/cross_module_relative_external/src/ffi/helper.js @@ -0,0 +1,3 @@ +export function raw(x) { + return x * 10; +}