Skip to content

Allow import() of a relative-path external in its own file - #9506

Merged
cknitt merged 1 commit into
masterfrom
import-relative-external-9405
Dec 3, 2026
Merged

cknitt merged 1 commit into
masterfrom
import-relative-external-9405

Conversation

@cknitt

@cknitt cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member

Fixes #9405.

An external with a relative path, such as @module("./mathLib.js"), is exported as a plain value, so other modules don't inline a path that is only valid from its own file. The type checker also showed it as a plain value to the rest of its own file, so import(floor) failed with an error calling it a local value.

Now only the exported signature hides the external. In its own file it is an external again, so

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

compiles to import("./mathLib.js").then(m => m.Math.floor), the same as for a package. Other files still call it through the exported binding.

A side effect: a zero-arity relative external used in its own file is now read directly (FileJs.foo + 1 instead of foo + 1), as package externals already are. That changes four generated test files.

New tests: the scoped and default cases from the issue in tests/compiler_tests/src/import_external.res.

Not covered: import(M.inner) where M is a local module of the same file and inner a relative external still fails, because M's signature hides the external.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gGnvaRviGhTLTTUbqs

Why:
    An external with a relative path, such as `@module("./mathLib.js")`,
    is exported as a plain value so other modules don't inline the path.
    The type checker also showed it as a plain value to the rest of its
    own file, so `import(floor)` failed with an error calling it a local
    value. Now only the exported signature hides the external. Fixes #9405.

Diagnostic removed (import of a relative-path external):
Example:
    @module("./mathLib.js") @scope("Math")
    external floor: float => float = "floor"
    let f = import(floor)
Before (diagnostic):
    Invalid argument: Dynamic import requires a module or module value that is a file as argument. Passing a value or local module is not allowed.

JS modified (import of a relative-path external):
Example:
    @module("./mathLib.js") @scope("Math")
    external floor: float => float = "floor"
    let f = import(floor)
After (js):
    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):
Example:
    @module("./file.js") external foo: int = "foo"
    let bar = foo + 1
Before (js):
    let foo = FileJs.foo;

    let bar = foo + 1 | 0;
After (js):
    let foo = FileJs.foo;

    let bar = FileJs.foo + 1 | 0;

Review: Can other modules now inline the relative path? No. The
exported signature still has the external as a plain value, so other
files keep calling it through the exported binding, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gGnvaRviGhTLTTUbqs
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Allow import(x) for an external x with a relative path, such as @module("./mathLib.js"), declared in the same file. It failed with an error that called x a local value.

Semantic Diff

1 commit · Full page

Allow import() of a relative-path external in its own file

8fdb9a4 · cknitt + Claude · Dec 03, 2026, 05:39 UTC

Why

An external with a relative path, such as @module("./mathLib.js"), is exported as a plain value so other modules don't inline the path. The type checker also showed it as a plain value to the rest of its own file, so import(floor) failed with an error calling it a local value. Now only the exported signature hides the external. Fixes #9405.

Diagnostic removed (import of a relative-path external)

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

diagnostic · Before

Invalid argument: Dynamic import requires a module or module value that is a file as argument. Passing a value or local module is not allowed.

JS modified (import of a relative-path external)

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

js · After

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

js

 let foo = FileJs.foo;
 
-let bar = foo + 1 | 0;
+let bar = FileJs.foo + 1 | 0;

Review

  • Can other modules now inline the relative path? No. The exported signature still has the external as a plain value, so other files keep calling it through the exported binding, as before.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 653,176,259 → 653,178,805 (±0.00%).

Generated projects: 627,030,951 → 627,030,164 (±0.00%).

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

Instructions

In total: 8,963,760,124 → 8,964,113,806 (±0.00%).

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

Output size

In total: 27,441,259 → 27,441,437 (±0.00%).

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

Pull request 8fdb9a4 compared with base 0a686a9 in this run.

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 82.54%. Comparing base (0a686a9) to head (8fdb9a4).

Files with missing lines Patch % Lines
compiler/typing/typemod.ml 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9506      +/-   ##
==========================================
- Coverage   82.54%   82.54%   -0.01%     
==========================================
  Files         469      469              
  Lines       66224    66226       +2     
==========================================
+ Hits        54666    54667       +1     
- Misses      11558    11559       +1     
Files with missing lines Coverage Δ
compiler/typing/typemod.ml 77.58% <66.66%> (-0.07%) ⬇️
🚀 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@9506

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 8fdb9a4

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@cknitt

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@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-03T06:05:44.067452Z 8fdb9a4 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: 8fdb9a4f02

ℹ️ 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 requested a review from fhammerschmidt December 3, 2026 06:28
@cknitt
cknitt marked this pull request as ready for review December 3, 2026 06:28
@cknitt
cknitt merged commit 82cb108 into master Dec 3, 2026
22 checks passed
@cknitt
cknitt deleted the import-relative-external-9405 branch December 3, 2026 13:19
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.

Dynamic import of an external with a relative @module path fails with a misleading error

2 participants