tauri: `beforeDevCommand` is not killed on app exit

Describe the bug

Closing the tauri dev app window doesn’t close the dev server started with beforeDevCommand (confirmed using the create-react-app template for create-tauri-app)

To Reproduce

Steps to reproduce the behavior:

  1. Create an app using create-react-app template
  2. Run tauri dev
  3. Close the app window using the x button or alt + f4
  4. Run tauri dev again.

Expected behavior

The dev server should be gracefully shut down when the app is closed.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

If anyone interested, you can kill the previous process when launching the app like so: "beforeDevCommand": "kill -9 $(lsof -i :3003 -t) &>/dev/null || true && yarn start"

There is another approach in bash, where you can grab the PID of the last command run after sending to the background and then using fg to bring it back - and then we could potentially try to sigkill that:

> npm start & export REACTPID=$! ; fg
# in another terminal
> kill 9 ${REACTPID}

Not sure this is really going to work though, I do see the need for us to dive back into this problem.

The issue should stay open then

As a workaround (and good practice in general) for those who use vite, here’s the config we will be recommending in the guides:

{
  // prevent vite from obscuring rust errors
  clearScreen: false,
  // tauri expects a fixed port, fail if that port is not available
  server: {
    port: 3000,
    strictPort: true,
  },
  // to make use of `TAURI_PLATFORM`, `TAURI_ARCH`, `TAURI_FAMILY`, `TAURI_PLATFORM_VERSION`, `TAURI_PLATFORM_TYPE` and `TAURI_DEBUG` env variables
  envPrefix: ['VITE_', 'TAURI_'],
  build: {
    // tauri supports es2021
    target: ['es2021', 'chrome97', 'safari13'],
    // don't minify for debug builds
    minify: !process.env.TAURI_DEBUG && 'esbuild',
    // produce sourcemaps for debug builds
    sourcemap: !!process.env.TAURI_DEBUG,
  },
}

The important settings for this issue are these:

  // Fixes the console override issue
  clearScreen: false,
  // makes vite abort if orphan processes keep port 3000 occupied
  server: {
    port: 3000,
    strictPort: true,
  },