rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

Async import - unbound module type

Open CarlOlson opened this issue 1 year ago • 2 comments

Importing code from another workspace often creates an error:

  4 ┆ switch value {
  5 ┆ | Some(arr) =>
  6 ┆   module Array = await Belt.Array
  7 ┆   Js.log(arr->Array.keepMap(x => x))
  8 ┆ | None => ()

  Unbound module type __Belt_Array__
  1. Here is the smallest example I've found: playgound
let fn = async value => {
  switch value {
  | Some(_) =>
    module Array = await Belt.Array
    ()
  | None => ()
  }
}
  1. However, this does not error: playground
let fn = async value => {
  module Array = await Belt.Array
  switch value {
  | Some(_) => ()
  | None => ()
  }
}
  1. Nor does this: playground
module type T = module type of Belt.Array

let fn = async value => {
  switch value {
  | Some(arr) =>
    module Array: T = await Belt.Array
    Js.log(arr->Array.keepMap(x => x))
  | None => ()
  }
}

But, (3) doesn't even produce an async import:

// Generated by ReScript, PLEASE EDIT WITH CARE
'use strict';

let Belt_Array = require("rescript/lib/js/Belt_Array.js");

async function fn(value) {
  if (value !== undefined) {
    console.log(Belt_Array.keepMap(value, x => x));
    return;
  }
  
}

exports.fn = fn;
/* No side effect */

This has been tested on 11.1.4 and 12.0.0-alpha.5, but the master branch has not been tested.

While importing Belt or Js asynchronously isn't desired, this error happens in my project in several useful locations.

CarlOlson avatar Nov 25 '24 06:11 CarlOlson

cc @mununki

zth avatar Nov 26 '24 09:11 zth

On v12.0.0-beta.6, annotating an async function with its return type triggers this bug:

// Unbound module type __Belt_Array__
let fn = async (): int => {
  module Array = await Belt.Array

  42
}
// No error
let fn: unit => promise<int> = async () => {
  module Array = await Belt.Array

  42
}
// No error
let fn = async () => {
  module Array = await Belt.Array

  42
}

mediremi avatar Jun 12 '25 14:06 mediremi