tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
84.73k stars 2.55k forks source link

[bug] vite process leaks memory when using CXX with the svelte template #7429

Closed sirinsidiator closed 1 year ago

sirinsidiator commented 1 year ago

Describe the bug

When using the create-tauri-app utility, the vite dev command watches the whole project folder by default, which causes a weird interaction with cxx where it would continuously rescan the debug output folder inside the src-tauri folder. This causes a huge memory leak (5MB+/s) and makes the whole debug process slow down to a crawl. The rescanning can be easily observed with sysinternals procmon.

Ignoring the src-tauri folder in vite.config.ts fixes the problem entirely:

    server: {
        port: 1420,
        strictPort: true,
        watch: {
            ignored: ['**/src-tauri/**/*']
        }
    },

Reproduction

  1. Setup new project with npm create tauri-app@latest
    • Project name · test
    • Choose which language to use for your frontend · TypeScript / JavaScript - (pnpm, yarn, npm)
    • Choose your package manager · npm
    • Choose your UI template · Svelte - (https://svelte.dev/)
    • Choose your UI flavor · TypeScript
  2. Add cxx to the rust project (see tutorial for more details)
    • cargo add cxx
    • cargo add cxx-build --build
    • update build.rs to call cxx_build:
      fn main() {
      cxx_build::bridge("src/main.rs")
      .compile("cxx-test");
      tauri_build::build()
      }
    • update main.rs and add empty ffi section:
      #[cxx::bridge]
      mod ffi {
      }
  3. Run npm run tauri dev and observe the node.js process for vite go crazy.

Expected behavior

Vite should run normally and not leak memory.

Platform and versions

[✔] Environment
    - OS: Windows 10.0.22621 X64
    ✔ WebView2: 114.0.1823.82
    ✔ MSVC: Visual Studio Community 2019
    ✔ rustc: 1.70.0 (90c541806 2023-05-31)
    ✔ Cargo: 1.70.0 (ec8a8a0ca 2023-04-25)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 18.15.0
    - yarn: 1.22.19
    - npm: 9.6.2

[-] Packages
    - tauri [RUST]: 1.4.1
    - tauri-build [RUST]: 1.4.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.2
    - @tauri-apps/api [NPM]: 1.4.0
    - @tauri-apps/cli [NPM]: 1.4.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

No response

amrbashir commented 1 year ago

Following your repro instructions, I couldn't find anything unusual, create-tauri-app templates have been updated to use a newer version of vite, could you test again? if the issue persists, please upload the repro to GitHub and post the link here.

sirinsidiator commented 1 year ago

I just tried it again and it is still not working as expected. When I follow my steps and watch the process id for the vite node.js with Process Monitor, I get hundreds of thousands of events like this per second: image As soon as I set src-tauri to be ignored in vite, it no longer does that.

I know this isn't really a bug in tauri and more of a configuration problem for a somewhat special use-case, but I figured I'd take the time and report it, since it is not trivial to find and fix when one hits this issue and I didn't find anything at all on the internet about this particular problem when I looked into it.

In my opinion the js side doesn't have any business watching the src-tauri folder anyway, so it wouldn't hurt to add that to templates that use vite regardless.