pglite icon indicating copy to clipboard operation
pglite copied to clipboard

Improve error with pointer to docs when PGlite is unable to load WASM binary

Open Arrow7000 opened this issue 1 year ago • 10 comments

I'm getting an error when running a transaction in the browser. The function I'm running:

export const replaceSchema = (db: PGlite, schemaStr: SchemaString) => {
  return db.transaction((tx) => {
    tx.exec(`DROP SCHEMA public CASCADE;`);

    return tx.exec(schemaStr);
  });
};

The error:

Uncaught (in promise) RuntimeError: Aborted(CompileError: WebAssembly.instantiate(): expected magic word 00 61 73 6d, found 3c 21 64 6f @+0). Build with -sASSERTIONS for more info.
    at abort (@electric-sql_pglite.js?v=74e038dd:461:

Arrow7000 avatar Aug 14 '24 14:08 Arrow7000

That looks like a WASM problem. Are you using vite and not excluding it from the optimisations?

  optimizeDeps: {
    exclude: ['@electric-sql/pglite'],
  },

AntonOfTheWoods avatar Aug 15 '24 04:08 AntonOfTheWoods

Yes, adding this to the vite.config.js file did the trick for me. Thank you @AntonOfTheWoods

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    exclude: ['@electric-sql/pglite'],
  },
});

thorwebdev avatar Aug 15 '24 08:08 thorwebdev

Although this is due to a bundler and outside of PGlite, we should try and improve the error message here, to indicate what the fix is.

samwillis avatar Aug 15 '24 11:08 samwillis

Are you using vite and not excluding it from the optimisations?

😅 that is exactly what I was doing. I had no idea I had to exclude it from optimisations. Yeah, a better error message would be amazing, but it does work now! Thanks!

Arrow7000 avatar Aug 15 '24 13:08 Arrow7000

I have a similar issue with Next.js:

RuntimeError: Aborted(). Build with -sASSERTIONS for more info.

I have try to apply a similar change like in Vite optimizeDeps.exclude In my next.js config, I have the following line:

serverComponentsExternalPackages: ['@electric-sql/pglite'],

PGlite works perfectly with the in-memory FS and it didn't work with Node FS. So, I don't have any issue with the in-memory version, the issue is only with the Node FS.

Any idea?

ixartz avatar Aug 27 '24 19:08 ixartz

Possibly related https://github.com/electric-sql/pglite/issues/322#issuecomment-2372563526

lirbank avatar Sep 24 '24 23:09 lirbank

@lirbank I also applied serverComponentsExternalPackages (I'm currently at Next.js 14) to make it work. It working with the in-memory FS: https://pglite.dev/docs/filesystems#in-memory-fs It doesn't work with https://pglite.dev/docs/filesystems#node-fs

In your case, does it work with https://pglite.dev/docs/filesystems#node-fs?

ixartz avatar Sep 24 '24 23:09 ixartz

@ixartz got it. I have not tried the node-fs. I am just using PGllite in the browser for now, so I am afraid I can't help. But I am curious to use it for tests on the server at some point.

lirbank avatar Sep 24 '24 23:09 lirbank

@lirbank On my side, I didn't have the chance to try in the browser, I'm currently using for local/dev environment with PGLite. I also use for testing.

If you are interested, you can take a look at: https://github.com/ixartz/SaaS-Boilerplate

ixartz avatar Sep 25 '24 10:09 ixartz

https://github.com/electric-sql/pglite/issues/199#issuecomment-2290527070 fixed it for me as well, but I never got any error messages (in Safari), my .exec call simply hung forever.

electrovir avatar Oct 23 '24 23:10 electrovir