Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/cross-module-relative-external.fix.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 14 additions & 0 deletions compiler/backend/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/**/*.mjs
40 changes: 40 additions & 0 deletions tests/build_tests/projects/cross_module_relative_external/input.js
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let direct = Hidden.raw(1)
let wrapped = Hidden.wrapped(2)
let base = Hidden.base("/a/b.txt")
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function raw(x) {
return x * 10;
}
Loading