Skip to content

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

Backlog

Description

@fhammerschmidt

Summary

Since #8582, import(...) accepts an external binding and emits a direct dynamic import of the JavaScript module, following the @scope path. This works for externals bound to a package, but not for externals bound to a relative file such as @module("./mathLib.js"). Those fail with an error that says the argument is a local value.

Reproduced with 13.0.0-alpha.7; the logic is unchanged on master.

Reproduction

// Works: package module
@module("mathlib") @scope("Math")
external floor: float => float = "floor"
let f = import(floor)
// => let f = import("mathlib").then(m => m.Math.floor);
// Fails: relative module, with or without @scope
@module("./mathLib.js") @scope("Math")
external floor: float => float = "floor"
let f = import(floor)
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.

Expected: let f = import("./mathLib.js").then(m => m.Math.floor);, the same as for a package.

The tests added in #8582 (tests/tests/src/import_external.res) only cover package modules, so this case isn't exercised.

Cause

An external whose path is package-relative is flagged by Primitive.no_cross_module_inline, because other modules must not inline the relative path. Typemod therefore wraps a structure-level relative external as include (struct external x = ... end : {let x: t}) (compiler/typing/typemod.ml, around line 1477), so later references see x as a Val_reg value, even within the same file.

Translcore.import_source_of_arg only produces Import_external for a Texp_ident whose val_kind is Val_prim. The wrapped external falls through to of_global_path, which requires a value from another file and raises the error above.

Workaround

Put the binding in its own file and import it from another module:

// MathBindings.res
@module("./mathLib.js") @scope("Math")
external floor: float => float = "floor"

// App.res
let f = import(MathBindings.floor)
// => import("./MathBindings.res.mjs").then(m => m.floor)

This compiles, but it dynamically loads the ReScript wrapper module, which statically imports ./mathLib.js, rather than importing ./mathLib.js directly.

Possible fix

Resolve the import against the underlying primitive when the referenced value is a relative external wrapped by Typemod in the same compilation unit. The relative path is valid there, since the import is emitted in the declaring file. At minimum, the error message could say that externals with relative module paths aren't supported, instead of calling them local values.

Found while documenting dynamic imports of externals for the v13 docs (rescript-lang/rescript-lang.org#1493).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions