Repository navigation
Explain that import() cannot load a relative external from its own file - #9516
Conversation
Changelog💅 Polish
Semantic Diff1 commit · Full page ✨ Explain that import() cannot load a relative external from its own filedd70697 · cknitt + Claude · Dec 04, 2026, 07:02 UTC · new
Diagnostic modified (import of a relative-path external in its own file) @module("./mathLib.js") @scope("Math")
external floor: float => float = "floor"
let f = import(floor)
let MathLibJs = require("./mathLib.js");
function floor(prim) {
return MathLibJs.Math.floor(prim);
}
let f = import("./mathLib.js").then(m => m.Math.floor);
JS modified (a zero-arity relative-path external used in its own file) @module("./file.js") external foo: int = "foo"
let bar = foo + 1
let foo = FileJs.foo;
-let bar = FileJs.foo + 1 | 0;
+let bar = foo + 1 | 0;Review
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #9516 +/- ##
=======================================
Coverage 82.56% 82.56%
=======================================
Files 468 468
Lines 66228 66235 +7
=======================================
+ Hits 54678 54685 +7
Misses 11550 11550
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9516 |
bad5845 to
4b6e97e
Compare
|
@codex review Requested by Christoph's dot. |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
4b6e97e to
2ca4372
Compare
2ca4372 to
c780522
Compare
|
@codex review Requested by Christoph's dot for the updated green commit |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Why:
An external with a relative path, such as `@module("./mathLib.js")`,
is exported as a value, so its file imports the module statically.
`import(x)` in that file was never lazy. Showing the external to the
rest of its file also re-read properties and gave tagged templates a
receiver. Its file sees a plain value again, and `import(x)` explains
why it fails. Fixes #9515.
Diagnostic modified (import of a relative-path external in its own file):
Example:
@module("./mathLib.js") @scope("Math")
external floor: float => float = "floor"
let f = import(floor)
Before (js):
let MathLibJs = require("./mathLib.js");
function floor(prim) {
return MathLibJs.Math.floor(prim);
}
let f = import("./mathLib.js").then(m => m.Math.floor);
After (diagnostic):
Invalid argument: an external with a relative path cannot be imported dynamically in its own file, because the file already imports its module statically. Move the external to a file of its own and import its value from that file.
JS modified (a zero-arity relative-path external used in its own file):
Example:
@module("./file.js") external foo: int = "foo"
let bar = foo + 1
Before (js):
let foo = FileJs.foo;
let bar = FileJs.foo + 1 | 0;
After (js):
let foo = FileJs.foo;
let bar = foo + 1 | 0;
Review: Does the exported signature change? No. Only the file's own
environment marks the value with `res.relativeExternal`, so the .cmi and
other modules see the same plain value as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gH77iLiiwQFwN8mRDU
Signed-off-by: Christoph Knittel <ck@cca.io>
c780522 to
dd70697
Compare
Why:
`@module("./shapes.js") @instanceof("Circle") type circle` names a
file beside the module that declares the type. The module that
compiles a class check adds the import, and it wrote the path as it
is, so a check in another directory or package imported a file
beside itself: another file, or none.
A class with a relative module now records the compilation unit that
declares it. A check in another unit imports the path from the
JavaScript file of that unit, which is found as an import of the unit
is, for each output format. Externals avoid the same problem by
calling through the declaring module's export; a type has no export
to go through, so the path is rewritten instead.
JS modified (class check in another directory):
Example:
// src/shapes/Shapes.res
@module("./shapes.js") @instanceof("Circle")
type circle
// src/app/App.res
@unboxed
type value =
| Circle(Shapes.circle)
| Plain({"plain": bool})
let kind = value =>
switch value {
| Circle(_) => "circle"
| Plain(_) => "plain"
}
Before (js): src/app/App.mjs
import * as ShapesJs from "./shapes.js";
function kind(value) {
if (value instanceof ShapesJs.Circle) {
return "circle";
} else {
return "plain";
}
}
After (js): src/app/App.mjs
import * as ShapesJs from "../shapes/shapes.js";
function kind(value) {
if (value instanceof ShapesJs.Circle) {
return "circle";
} else {
return "plain";
}
}
JS modified (class check in another package):
Example:
// node_modules/shapes-dep/src/hex/Dep.res, namespace ShapesDep
@module("./dep_shapes.js") @instanceof("Hexagon")
type hexagon
// src/app/Classes.res
@unboxed
type value =
| Hexagon(ShapesDep.Dep.hexagon)
| Plain({"plain": bool})
Before (js): src/app/Classes.mjs, and lib/js/src/app/Classes.cjs
import * as Dep_shapesJs from "./dep_shapes.js";
let Dep_shapesJs = require("./dep_shapes.js");
After (js): src/app/Classes.mjs, and lib/js/src/app/Classes.cjs
import * as Dep_shapesJs from "shapes-dep/src/hex/dep_shapes.js";
let Dep_shapesJs = require("shapes-dep/lib/js/src/hex/dep_shapes.js");
Representation modified (class of an @instanceof type):
Before (representation):
@module("./shapes.js") @instanceof("Circle") ↦ {module_ = Some "./shapes.js"; path = "Circle"}
@module("three") @instanceof("Mesh") ↦ {module_ = Some "three"; path = "Mesh"}
After (representation): in Shapes.res
@module("./shapes.js") @instanceof("Circle") ↦ {module_ = Some "./shapes.js"; relative_to = Some "Shapes"; path = "Circle"}
@module("three") @instanceof("Mesh") ↦ {module_ = Some "three"; relative_to = None; path = "Mesh"}
Diagnostic modified (the same relative path in two files):
Example:
// Class_types.res
module type S = {
@module("./shapes.js") @instanceof("Circle")
type t
}
// Class_use.res
module M: Class_types.S = {
@module("./shapes.js") @instanceof("Circle")
type t
}
Before (diagnostic): none
After (diagnostic):
Signature mismatch:
…
The declarations read the same, but a type they mention differs:
Actual declaration: instances of Circle from "./shapes.js" relative to Class_use
Expected declaration: instances of Circle from "./shapes.js" relative to Class_types
Review: Is a check in the declaring file itself changed? No. There the
unit is the one being compiled, the path is written as it is, and the
import is shared with the file's externals of the same path, as
before; untagged_module_class_test.mjs is unchanged.
Review: What does an annotation mean that repeats the abbreviated
type's, in another file? It names the abbreviated type's class, with
the path from that type's file, so an interface that restates
`@module("./shapes.js") @instanceof("Circle") type circle =
Shapes.circle` still compiles in any directory.
Review: Where does the import go when the output is not beside the
sources? Beside the output of the declaring module, in each format:
the path is read from the generated JavaScript file, as the path of
an external is, so the file has to be there, as for an external.
Review: Are two declarations of one file's class one class? No. A
relative module is identified with the unit that declares the type,
since the type checker does not know which directory a unit is in:
the same `"./shapes.js"` declared in two files is two classes, also in
one directory. A signature with such a type is not matched from
another file, as the diagnostic above shows, and a variant can have
both types as cases, each with its own import of the file. The
feature is not released yet (changes/instance-of-module.fix.md), so
no released program changes meaning.
Review: What if the declaring unit cannot be loaded? Its .cmj gives the
path, and a package that does not depend on the unit's package has
none. A check there is an error that asks for the dependency
(`Cannot import the class Triangle from "./hidden.js" relative to
Hidden …`), and a function with such a check is not inlined into
that package, like a body that names a module it cannot load.
Without a package (plain bsc), where the unit's .cmj or JavaScript
file may not exist, the path stays as written, as before.
Review: Can every package import the file? Only if the declaring package
lets it: a package that limits its `exports` has to export the file,
as it has to export its compiled modules.
Review: Does this touch the externals that rescript-lang#9516 changes? No. It reads
`External_ffi_types.is_package_relative_path` and leaves primitive.ml,
translcore.ml and typemod.ml alone.
Signed-off-by: Jono Prest <jjprest@gmail.com>
Fixes #9515. Replaces the feature from #9506 with a clear error.
Why
An external with a relative path, such as
@module("./mathLib.js"), is exported as a value, so its file imports the module statically. #9506 letimport(x)take such an external in its own file, but the import was never lazy: the output had bothimport * as MathLibJs from "./mathLib.js"andimport("./mathLib.js"), and bundlers don't split a file that is imported statically too. Showing the external to the rest of its file also re-read properties and gave tagged templates a receiver (#9515).Change
import(x)of such an external in its own file now says why it can't work and suggests moving the external to a file of its own. The value is marked only in the file's own environment; the exported signature and the.cmidon't change.Tests
relative_external_test.res: a getter is read once and a tag call keepsthisundefined.dynamic_import_relative_external.res: the new error.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gH77iLiiwQFwN8mRDU