wasm-tool / wasm-pack-plugin

webpack plugin for Rust
MIT License
315 stars 70 forks source link

Build error on rebuild after code update #129

Open Arcahub opened 2 years ago

Arcahub commented 2 years ago

Hello,

I'm using your plugin in a Next application to build a rust library in wasm. I'm using @nrwl/nx to manage my monorepo and both the Nest app and the rust lib are integrated in it.

But I encounter a problem when developing my application. Every time I modify the rust code the plugin attempt to rebuild the wasm library, that is really cool but it always ends with this error:

Error: invalid type: sequence, expected a string at line 4 column 11

The wasm lib is not updated and so I need to restart the application.

To be honest, I don't know if the problem laid in @nrwl/nx file structure or in Next or in your plugin but since the plugin in the main actor you may have some ideas about why it is not working.

I've a test repository with a branch named feat/webpack: https://github.com/Arcahub/rust-wasm-next-nx/tree/feat/webpack

You can clone the repository, checkout the branch feat/webpack and then run yarn nx serve my-next-app to start the development server. The application is located in apps/my-next-app and the rust library is in libs/my-rust-lib.

To reproduce the bug, after you have started the development server, you can edit the file libs/my-rust-lib/src/lib.rs, save it and then you should have an error like the one I send earlier.

Note: The compilation seems to run two times, I don't think it's wanted but that may be link to my problem.

Thanks for reading, if you have any question feel free to ask me.

mjvmroz commented 2 years ago

I just spent a while debugging this, because it's been driving me insane too, wth the two processes trampling over each other, corrupting the output, and crashing my Typescript watcher. I had all sorts of theories: editor code formatting, WSL filesystem race conditions, plugin bugs... but it turns out that Next.js just initializes webpack twice. Topically, this rant about SSR and function colors was shared today.

Fortunately, we can pull an isServer flag out of the second param to the webpack configurer:

module.exports = {
    webpack(config, { isServer }) {
        // ...
        if (!isServer) {
            config.plugins.push(
                new WasmPackPlugin({
                    crateDirectory: resolve("./rust"),
                    outDir: resolve("./rust/pkg"),
                    args: "--log-level warn",
                }),
            );
        }
        // ...
    }
}
Arcahub commented 2 years ago

Thanks you very much for your help. What you are saying totally make sense and match with what I've experimented :nerd_face: .

I will test it later today and close this issue if it works :rocket: .

Arcahub commented 2 years ago

I tested it and your suggestion fix the problem of double compilation. Thanks you very much.

But sadly, I still got some errors on the library rebuild but on my personal work not the example repository so I will work on this bug to try to debug/reproduce it so I can ask again. Until I provide more details with a reproducible repo, here is the current behavior of my personal project:

The rust crate use the web audio api. The next app actually doesn't old that much of logic but display the module available on the web audio api thought the wasm library. And I load / use the wasm library in a component that has SSR disable

It alternate between two errors:

The same error as before that seems to appear after some rebuild not the first one: Error: invalid type: sequence, expected a string at line 4 column 11

And this one:

error - ./pkg/index.js
Error: Failed to read source code from /home/user/monorepo/apps/studio/client/pkg/index.js

Caused by:
    No such file or directory (os error 2)

Again I will try to provide a reproducible repository as soon as possible.

Arcahub commented 2 years ago

Hey I m back, I have successfully reproduced the bug thanks to rustwasm game of life: https://github.com/rustwasm/wasm_game_of_life .

You can test it on the same repository and same branch: https://github.com/Arcahub/rust-wasm-next-nx/tree/feat/webpack if you encounter any problem or if you are not able to reproduce the bug, tell me.

It seems that it came from the "life time" of the wasm module that is still in use while the plugin rebuild it and make the error happens.

Is there a possibility to put on hold the process of the development server while wasm-pack rebuild the library and then restart the development server ?

If you have any other ideas I would happily test them.