Trying to add `wasm` target to my project but test...
# webassembly
m
Trying to add
wasm
target to my project but tests keep failing with this cryptic error. It's a pure kotlin project. Anyone see this before or know what's happening?
Copy code
> Task :library:encoding-core:wasmBrowserTest

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/administrator/Projects/personal/components/encoding/build/js/packages/encoding-encoding-core-wasm-test/kotlin/encoding-encoding-core-wasm-test.mjs not supported.
Instead change the require of /home/administrator/Projects/personal/components/encoding/build/js/packages/encoding-encoding-core-wasm-test/kotlin/encoding-encoding-core-wasm-test.mjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/administrator/Projects/personal/components/encoding/build/js/packages/encoding-encoding-core-wasm-test/adapter-nodejs.js:2:18)
    at async Promise.all (index 0)
    at async formattedImport (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at async Object.exports.requireOrImport (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/mocha/lib/nodejs/esm-utils.js:38:28)
    at async Object.exports.loadFilesAsync (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/mocha/lib/nodejs/esm-utils.js:91:20)
    at async singleRun (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at async Object.exports.handler (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/mocha/lib/cli/run.js:370:5)

> Task :library:encoding-core:wasmD8Test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':library:encoding-core:wasmD8Test'.
> command '/home/administrator/.gradle/d8/v8-linux64-rel-10.2.9/d8' exited with errors (exit code: 1)
s
It looks like this test is trying to load
mocha
, a Node.js and browser testing library. But it runs in a
d8
a bare-bones V8 JS shell, where mocha is not supported. Lack of
require()
was the first error it encountered, but it probably will not be the only one. Usually, when running
wasmD8Test
, Kotlin/Wasm uses uses built-in test runner without Mocha. If you haven’t configured anything specifically around
d8
and
mocha
in your build, this error is not expected, and i would like to see more information about your build scripts to solve this problem.
m
Yup, that's the configuration I'm using. Expressing
Copy code
browser { testTask { useMocha { timeout = "30s" } } }
nodejs { testTask { useMocha { timeout = "30s" } } }
d8()
yielded the same error. Tried simply expressing
Copy code
browser()
nodejs()
d8()
and got this
Copy code
> Task :library:encoding-core:wasmBrowserTest
Module parse failed: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See <https://webpack.js.org/concepts#loaders>
Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
    at /home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/dependencies/HarmonyDetectionParserPlugin.js:54:11
    at Hook.eval [as call] (eval at create (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
    at Hook.CALL_DELEGATE [as _call] (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/tapable/lib/Hook.js:14:14)
    at JavascriptParser.walkAwaitExpression (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2342:29)
    at JavascriptParser.walkExpression (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2272:10)
    at JavascriptParser.walkVariableDeclaration (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2126:33)
    at JavascriptParser.walkStatement (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1620:10)
    at JavascriptParser.walkStatements (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1481:9)
    at /home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1655:9
    at JavascriptParser.inBlockScope (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:3113:3)
  Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
      at /home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/dependencies/HarmonyDetectionParserPlugin.js:54:11
      at Hook.eval [as call] (eval at create (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
      at Hook.CALL_DELEGATE [as _call] (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/tapable/lib/Hook.js:14:14)
      at JavascriptParser.walkAwaitExpression (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2342:29)
      at JavascriptParser.walkExpression (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2272:10)
      at JavascriptParser.walkVariableDeclaration (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2126:33)
      at JavascriptParser.walkStatement (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1620:10)
      at JavascriptParser.walkStatements (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1481:9)
      at /home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1655:9
      at JavascriptParser.inBlockScope (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:3113:3)
  at /tmp/_karma_webpack_399976/commons.js:95:1
  Error: The top-level-await experiment is not enabled (set experiments.topLevelAwait: true to enabled it)
      at /home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/dependencies/HarmonyDetectionParserPlugin.js:54:11
      at Hook.eval [as call] (eval at create (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:16)
      at Hook.CALL_DELEGATE [as _call] (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/tapable/lib/Hook.js:14:14)
      at JavascriptParser.walkAwaitExpression (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2342:29)
      at JavascriptParser.walkExpression (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2272:10)
      at JavascriptParser.walkVariableDeclaration (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:2126:33)
      at JavascriptParser.walkStatement (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1620:10)
      at JavascriptParser.walkStatements (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1481:9)
      at /home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:1655:9
      at JavascriptParser.inBlockScope (/home/administrator/Projects/personal/components/encoding/build/js/node_modules/webpack/lib/javascript/JavascriptParser.js:3113:3)
      at ./kotlin/encoding-encoding-core-wasm-test.mjs (/tmp/_karma_webpack_399976/commons.js:95:7)
      at __webpack_require__ (/tmp/_karma_webpack_399976/runtime.js:23:41)
      at ./adapter-browser.js (/tmp/_karma_webpack_399976/commons.js:57:18)
      at __webpack_require__ (/tmp/_karma_webpack_399976/runtime.js:23:41)
      at __webpack_exec__ (adapter-browser.3388599486.js:4:48)
      at adapter-browser.3388599486.js:5:55
      at __webpack_require__.O (/tmp/_karma_webpack_399976/runtime.js:57:23)
      at adapter-browser.3388599486.js:6:56
      at webpackJsonpCallback (/tmp/_karma_webpack_399976/runtime.js:105:39)
      at adapter-browser.3388599486.js:2:117
java.lang.IllegalStateException: command '/home/administrator/.gradle/nodejs/node-v16.13.0-linux-x64/bin/node' exited with errors (exit code: 1)

> Task :library:encoding-core:wasmD8Test FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':library:encoding-core:wasmD8Test'.
> command '/home/administrator/.gradle/d8/v8-linux64-rel-10.2.9/d8' exited with errors (exit code: 1)
s
Browser tests don’t work, yet unfortunately because of https://youtrack.jetbrains.com/issue/KT-56159 . Fixed in upcoming 1.9.0 Current stable node version has V8 that is too old. Possible workaround is adding:
Copy code
plugins.withType<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin>().configureEach {
 extensions.configure<org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension> {
        nodeDownloadBaseUrl = "<https://nodejs.org/download/v8-canary>"
        nodeVersion = "20.0.0-v8-canary2022112061c569ba0d"
    }
}

tasks.withType<org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask>().configureEach {
    args.add("--ignore-engines")
}
But it would apply this version to the whole project, including K/JS
D8 seems to be crashing with unknown issue. Unfortunately gradle doesn’t report error messages when it is crashed (as opposed to just failing the test with exception). Running
./gradlew :library:encoding-core:wasmD8Test --info
should show the
d8
command to run it manually. I would be happy to look at it if you can share a code that reproduces the problem.
m
Definitely. Lemme get this merged and will create a separate branch for an issue ticket to enable wasm which will reproduce the issue!
kk. Reproducing the issue HERE for branch
issue/97-enable-wasm
s
A bunch of Kotlin/Wasm things got fixed in Kotlin 1.8.20-Beta. It seems that you are using older version. I wanted to test it with new version, but I couldn’t find where is should be specified.
m
gradle/libs.versions.toml
s
Updating to 1.8.20-Beta seems to break some K/Native build scripts. But
:library:encoding-core:wasmD8Test
now passes all tests!
m
Yeah I'm getting some weird errors when updating to
1.8.20-Beta
with the
js
target showing that
testTask
function is not found
Copy code
Unable to find method ''void org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBrowserDsl.testTask(kotlin.jvm.functions.Function1)''
But yes, the
wasmD8Test
run passes. Looks like I'll wait for
1.8.20
release to enable support for it. Thanks!
s
D8 should be enough for testing pure Kotlin code. Node and browser currently require a non-standard run config until corresponding issues are resolved.
m
So how would all that work then if I only express
d8
when someone depends on this library where they express
browser
and
nodejs
for their
wasm
target? I know the
js
target if you only express
nodejs
and someone depending on the library has
browser
and
nodejs
expressed, there is a compatibility error b/c of metadata incompatibility.
Copy code
{
      "target": "org.jetbrains.kotlin.gradle.targets.js.KotlinJsTarget",
      "platformType": "js",
      "extras": {
        "js": {
          "isBrowserConfigured": true,
          "isNodejsConfigured": true
        }
      }
    },
s
Good question regarding metadata. I don’t know the answer yet, I’ll ask around.