Skip to content
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

Inline method wrapping Scala.js dynamicImport throws "stub" exception #22162

Open
raquo opened this issue Dec 8, 2024 · 0 comments
Open

Inline method wrapping Scala.js dynamicImport throws "stub" exception #22162

raquo opened this issue Dec 8, 2024 · 0 comments

Comments

@raquo
Copy link
Contributor

raquo commented Dec 8, 2024

Compiler version

3.3.4, 3.5.2, 3.6.4-RC1-bin-20241205-c61897d-NIGHTLY

Minimized code

Full runnable project with this code and instructions on how to run scala.js + vite is available in the attached .zip archive.

Main.scala

package example

import org.scalajs.dom

import scala.annotation.nowarn
import scala.scalajs.js

@nowarn inline def dynamicImport[F](inline f: F): js.Promise[F] = {
  js.dynamicImport(f)
}

@main def main(): Unit =

  js.timers.setTimeout(2000) {
    val _ = js.dynamicImport({ dom.console.log("THIS IS js.dynamicImport") })
    val _ = dynamicImport({ dom.console.log("THIS IS local dynamicImport") }) // this throws
    // val _ = Importer.dynamicImport({ dom.console.log("THIS IS Importer.dynamicImport") }) // this throws after `sbt clean` only
  }

  dom.document.querySelector("#app").innerText = "Scala.js loaded. Open browser console to see `stub` error."

Importer.scala

package example

import scala.annotation.nowarn
import scala.scalajs.js

object Importer {

  @nowarn inline def dynamicImport[F](inline f: F): js.Promise[F] = {
    js.dynamicImport(f)
  }
}

build.sbt (extract)

    scalaJSUseMainModuleInitializer := true,
    scalaJSLinkerConfig ~= {
      _.withModuleKind(ModuleKind.ESModule)
        .withModuleSplitStyle(ModuleSplitStyle.SmallModulesFor(List("example")))
    }

Output

Browser console at runtime:

Uncaught java.lang.Error: stub
  ...
THIS IS js.dynamicImport

Expectation

Browser console at runtime:

THIS IS js.dynamicImport
THIS IS local dynamicImport

One more failure mode

In addition to the local dynamicImport never working, there is another, intermittent failure mode when the dynamicImport method is placed in a separate file (Importer.scala). Steps to reproduce that particular mode:

  • comment out the line with the local dynamicImport
  • uncomment the line with Importer.dynamicImport
  • run sbt clean
  • run the project as usual with ~fastLinkJS and npm run dev
  • you will see the same stub error in the browser console
  • add an empty // comment in Main.scala
  • after Vite reloads the page, the stub error is gone, and Importer.dynamicImport works as expected, printing out THIS IS Importer.dynamicImport
  • if you do sbt clean again, the error comes back on the next compile.
Screenshot 2024-12-07 at 11 42 05 PM

Full project

@raquo raquo added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 8, 2024
@sjrd sjrd self-assigned this Dec 8, 2024
@Gedochao Gedochao added area:scala.js and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants