oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.16k stars 2.77k forks source link

Bun.spawn for binary does not return the correct PID #12501

Open schettn opened 4 months ago

schettn commented 4 months ago

I use the following:

 let currentProc: Subprocess | null = null

  let serve = async () => {
    if (currentProc) {
      currentProc.kill()

      await currentProc.exited
    }

    currentProc = Bun.spawn({
      cmd: [
        'bun',
        'run',
        // 'pylon-server',  // DOES NOT WORK
        '/Users/schettn/Documents/pylon/node_modules/.bin/pylon-server', // WORKS
        '--port',
        options.port
      ],
      stdout: 'inherit',
      onExit: () => {
        console.log('Process exited')
      }
    })
  }

  await serve()

  setInterval(async () => {
    console.log('Rebuilding')
    await serve()
    console.log('Rebuilt')
  }, 10000)

The problem is that when I want to spawn bun run pylon-server the PID is not from /Users/schettn/Documents/pylon/node_modules/.bin/pylon-server. Instead the PID of currentProc is from the startup process.

Therefore I cannot call currentProc.kill() and restart my server.

Originally posted by @schettn in https://github.com/oven-sh/bun/issues/11400#issuecomment-2222092637

schettn commented 4 months ago

My current workaround is to call:

const {stdout} = await $`npx -p which which pylon-server`

const binPath = stdout.toString().trim()

Strangely await $`npx -p which which pylon-server`.text() kills the process silently.