breadboard icon indicating copy to clipboard operation
breadboard copied to clipboard

Top level await declarations prevent module typed compilation

Open j3ffgray opened this issue 1 year ago • 5 comments

Behavior

Deploying a project using a framework like Svelte requires a package.json set to "type: "module".

ESM modules don't support top level await functions, two of which are present in GeminiKit.

Steps to Reproduce the Problem

  1. Using sveltekit and svelte-adapter-appengine, deploy to App Engine.
  2. This causes the error noted below.
  3. Running locally works as intended, perhaps a vite configuration tweak could solve the issue

I haven't found a solution yet, and I'm not familiar enough with the structure of the kits yet, so I don't have a fix but wanted to note it, as it prevents deploying experiments with certain OS tools.

Specifications

  • Version: Svelte Kit 2.0.0, Svelte 4.2.7, Vite 5.0.3, Svelte Adapter Appengine 1.1.0

Error response when running npm run build

> Using svelte-adapter-appengine
✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@google-labs/gemini-kit/dist/src/index.js:11:16:
      11 │ const adapter = await GraphToKitAdapter.create(kit, KIT_BASE_URL, []);
         ╵                 ~~~~~

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@google-labs/gemini-kit/dist/src/boards/gemini-pro-vision.js:66:15:
      66 │ export default await board(() => {
         ╵                ~~~~~

error during build:
Error: Build failed with 2 errors:
node_modules/@google-labs/gemini-kit/dist/src/boards/gemini-pro-vision.js:66:15: ERROR: Top-level await is currently not supported with the "cjs" output format
node_modules/@google-labs/gemini-kit/dist/src/index.js:11:16: ERROR: Top-level await is currently not supported with the "cjs" output format
    at failureErrorWithLog (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1651:15)
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1059:25
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1004:52
    at buildResponseToResult (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1057:7)
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1086:16
    at responseCallbacks.<computed> (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:704:9)
    at handleIncomingPacket (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:764:9)
    at Socket.readFromStdout (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:680:7)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)

j3ffgray avatar Jun 24 '24 04:06 j3ffgray

ESM most definitely support top-level awaits! This error is typically due to some misconfiguration. I've seen it happen before, and the fix is usually messing with esbuild/rollup config files.

dglazkov avatar Jun 24 '24 05:06 dglazkov

The underlying issue is that the the bundler/compiler used by svelte-adapter-appengine is too pessimistic about the target on which this code is going to run, so it bails.

AppEngine supports node22 now, which most definitely supports top-level awaits.

For example, here's the fix for this problem for esbuild/vite:

https://github.com/breadboard-ai/breadboard/blob/8dbb1cfe75e4158a689abf2c75b91be26efb2b66/packages/board-server/vite.config.ts#L4-L5

dglazkov avatar Jun 24 '24 05:06 dglazkov

Hmmm, yeah, updating the vite.config.ts was the first thing I tried. I also tried manually setting the version of Node in the svelte config (for the app engine adapter), but I'm still receiving the build errors.

I'll keep poking around, thanks for the quick response!

j3ffgray avatar Jun 24 '24 14:06 j3ffgray

One thing that might help -- I see that you're targeting CommonJS (cjs) -- why not use ESM?

dglazkov avatar Jun 24 '24 16:06 dglazkov

I'm not attempting to target cjs, and I'm no wizard at Vite or Rollup, mostly just using the App Engine adapter Halfdanj created and using its stock configuration.

Any tips would be appreciated, but in the meantime, I've swapped Sveltekit adapters in favor of sveltekit-static, which has its own issues, but allowed me to deploy something to App engine that can run for the time being.

j3ffgray avatar Jun 24 '24 16:06 j3ffgray