Skip to content

Dynamic module import errors #7403

Closed
@CarlOlson

Description

@CarlOlson

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

> await-module-test@0.0.0 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);

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions