Skip to content

Dynamic module import errors #7403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
CarlOlson opened this issue Apr 24, 2025 · 0 comments · Fixed by #7452
Closed

Dynamic module import errors #7403

CarlOlson opened this issue Apr 24, 2025 · 0 comments · Fixed by #7452
Assignees
Labels
Milestone

Comments

@CarlOlson
Copy link

CarlOlson commented Apr 24, 2025

Doing a dynamic import will fail if the module to be imported has a module alias:

// Demo
  module Utils = await MathUtils

// MathUtils.res
// This causes a failure even if `Something.res` is empty!
module S = Something

Here is a repository demonstrating the bug: https://github.com/CarlOlson/await-module-test

I tried to follow the example here: https://rescript-lang.org/blog/first-class-dynamic-import-support

Here is the output of the build:

$ npm run res:build

> [email protected] res:build
> rescript

>>>> Start compiling
Dependency Finished
rescript: [9/9] src/Demo.cmj
FAILED: src/Demo.cmj

  We've found a bug for you!
  /home/carl/git/rescript-await-module/await-module-test/src/Demo.res:2:24-32

  1 │ let main = async () => {
  2 │   module Utils = await MathUtils
  3 │   let twoPlusTwo = Utils.add(2, 2)
  4 │   Js.log(twoPlusTwo)

  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.

FAILED: cannot make progress due to previous errors.
>>>> Finish compiling (exit: 1)

The wording is a bit different, but it exists on 12.0.0-alpha.11.

Annotating the type fixes the compiler warning, but optimizes the await away:

// Demo.res
  module Utils: (module type of MathUtils) = await MathUtils

// Demo.res.mjs
  var twoPlusTwo = MathUtils.add(2, 2);

// Expected Demo.res.mjs
  var Utils = await import("./MathUtils.res.mjs");
  var twoPlusTwo = Utils.add(2, 2);
@cknitt cknitt added the bug label May 5, 2025
@cknitt cknitt added this to the v12 milestone May 5, 2025
cristianoc added a commit that referenced this issue May 9, 2025
Fixes #7403

When a module containing an alias (e.g. `module VS = VariantSpreads`) is
dynamically imported, the compiler now correctly extracts the original
module identifier from the Llet expression.
cristianoc added a commit that referenced this issue May 9, 2025
Fixes #7403

When a module containing an alias (e.g. `module VS = VariantSpreads`) is
dynamically imported, the compiler now correctly extracts the original
module identifier from the Llet expression.
cristianoc added a commit that referenced this issue May 12, 2025
Fixes #7403

When a module containing an alias (e.g. `module VS = VariantSpreads`) is
dynamically imported, the compiler now correctly extracts the original
module identifier from the Llet expression.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants