Skip to content

Explain that import() cannot load a relative external from its own file - #9516

Merged
cknitt merged 1 commit into
masterfrom
fix/relative-external-import-9515
Dec 4, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix/relative-external-import-9515

Conversation

@cknitt

@cknitt cknitt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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 let import(x) take such an external in its own file, but the import was never lazy: the output had both import * as MathLibJs from "./mathLib.js" and import("./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

Tests

  • relative_external_test.res: a getter is read once and a tag call keeps this undefined.
  • dynamic_import_relative_external.res: the new error.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gH77iLiiwQFwN8mRDU

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Explain that an external with a relative path, such as @module("./mathLib.js"), cannot be passed to import() in its own file, instead of calling it a local value.

Semantic Diff

1 commit · Full page

✨ Explain that import() cannot load a relative external from its own file

dd70697 · cknitt + Claude · Dec 04, 2026, 07:02 UTC · new

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)

@module("./mathLib.js") @scope("Math")
external floor: float => float = "floor"
let f = import(floor)

js · Before

let MathLibJs = require("./mathLib.js");

function floor(prim) {
  return MathLibJs.Math.floor(prim);
}

let f = import("./mathLib.js").then(m => m.Math.floor);

diagnostic · After

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)

@module("./file.js") external foo: int = "foo"
let bar = foo + 1

js

 let foo = FileJs.foo;
 
-let bar = FileJs.foo + 1 | 0;
+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.

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

⚠️ Compiler benchmarks

The benchmarks couldn't run for pull request dd70697; the run shows why.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.56%. Comparing base (af21a60) to head (dd70697).

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           
Files with missing lines Coverage Δ
compiler/typing/primitive.ml 93.33% <100.00%> (+2.42%) ⬆️
compiler/typing/translcore.ml 88.53% <100.00%> (+0.04%) ⬆️
compiler/typing/typemod.ml 77.77% <ø> (ø)
🚀 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 Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9516

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: dd70697

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@cknitt
cknitt force-pushed the fix/relative-external-import-9515 branch from bad5845 to 4b6e97e Compare December 3, 2026 15:22

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

Requested by Christoph's dot.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 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-12-03T19:39:55.611672Z c780522 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. Another round soon, please!

Reviewed commit: 4b6e97ecb8

ℹ️ 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 marked this pull request as ready for review December 3, 2026 15:40
@cknitt
cknitt force-pushed the fix/relative-external-import-9515 branch from 4b6e97e to 2ca4372 Compare December 3, 2026 19:06
@cknitt cknitt added the changelog:skip No release notes needed label Dec 3, 2026
@cknitt
cknitt force-pushed the fix/relative-external-import-9515 branch from 2ca4372 to c780522 Compare December 3, 2026 19:07

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

Requested by Christoph's dot for the updated green commit c78052296f.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: c78052296f

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

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>
@cknitt
cknitt force-pushed the fix/relative-external-import-9515 branch from c780522 to dd70697 Compare December 4, 2026 07:02
@cknitt cknitt removed the changelog:skip No release notes needed label Dec 4, 2026
@cknitt cknitt changed the title Preserve captured relative externals outside dynamic import Explain that import() cannot load a relative external from its own file Dec 4, 2026
@cknitt
cknitt requested a review from fhammerschmidt December 4, 2026 07:39
@cknitt
cknitt merged commit e489471 into master Dec 4, 2026
30 of 35 checks passed
@cknitt
cknitt deleted the fix/relative-external-import-9515 branch December 4, 2026 09:11
JonoPrest added a commit to JonoPrest/rescript-compiler that referenced this pull request Dec 4, 2026
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>
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.

Relative externals are reread and gain a receiver after the dynamic import fix

2 participants