vite icon indicating copy to clipboard operation
vite copied to clipboard

buildEnd not trigger with vite 5.0.10

Open MaxRenglet opened this issue 2 years ago • 12 comments

Describe the bug

Hi,

It seems that buildEnd hook is not trigger anymore in vite 5.0.10 with a custom plugin. I run npm run dev then ctl-c to exit the process and buildEnd isn't trigger.

I tried with vite 4.5.1 and it works.

For the reproduction, I created a small plugin to test it on stackblitz ! It should console.log buildStart then buildEnd when the process is exited.

Thanks for the great work btw !

Reproduction

https://stackblitz.com/edit/vitejs-vite-8cekfx?file=vite.config.js

Steps to reproduce

npm install && npm run dev

System Info

System:
    OS: macOS 14.2.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 120.44 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.10.0 - ~/.volta/tools/image/node/20.10.0/bin/node
    npm: 10.2.3 - ~/.volta/tools/image/node/20.10.0/bin/npm
  Browsers:
    Safari: 17.2.1
  npmPackages:
    vite: ^5.0.10 => 5.0.10

Used Package Manager

npm

Logs

No response

Validations

MaxRenglet avatar Dec 23 '23 21:12 MaxRenglet

why apply: 'serve'? It shouldn't be called, if Vite 4 calling buildEnd it should be a bug (no makes sense having buildEnd in a dev plugin).

userquin avatar Dec 23 '23 21:12 userquin

Hi @userquin, yeah that what i thought, buildEnd is trigger in build mode but shouldn't be trigger in dev mode.. but it does in Vite 4.5.1.

Do you know if there is a similar hook working in dev ?

MaxRenglet avatar Dec 24 '23 09:12 MaxRenglet

No, but you can try adding build hooks (https://rollupjs.org/plugin-development/ and https://vitejs.dev/guide/api-plugin#transformindexhtml)... (apply can be also a function)

userquin avatar Dec 24 '23 09:12 userquin

Thanks @userquin, I will figure this out.

Should I rename the issue about the buildEnd triggered in dev in Vite 4.5.1 ?

MaxRenglet avatar Dec 24 '23 11:12 MaxRenglet

buildEnd should be called in dev too, it's documented in https://vitejs.dev/guide/api-plugin.html#universal-hooks. Seems like a bug it's not being called now.

bluwy avatar Dec 26 '23 04:12 bluwy

I took a look, and it seemed to work in Vite 4 because we took control of Ctrl+C via setRawMode, which was part of the breaking change related to the CLI shortcuts. Any versions other than v4 didn't work, as if you're relying on this behaviour, it's similar to listening to the Node.js process-exit-hook, and it's known to not always be reliable.

We're currently using SIGTERM to listen for process exit, but that only works in controlled environments such as child processes. I tested, and only SIGINT seems to reliably get triggered, but it was removed in https://github.com/vitejs/vite/commit/a04db16f0329cfa7ef97d0f58f36189b391494a0.

SIGINT is generally very tricky to work with unfortunately. You need to call process.exit() manually if you add a handler. But if you have multiple handlers (maybe some framework adds theirs too), then calling process.exit() will halt the subsequent added handlers.

This SO answer has some more details. If we do something similar, it might also help lessen the impact of https://github.com/vitejs/vite/issues/9470

bluwy avatar Dec 26 '23 06:12 bluwy

@bluwy the plugin shouldn't be called in build, it has apply: 'serve', check Conditional Application

EDIT: I think I have misunderstood the issue

userquin avatar Dec 26 '23 10:12 userquin

I don't see the plugin running when running npm run build in the repro 🤔

bluwy avatar Dec 26 '23 13:12 bluwy

Hi, @bluwy thanks for the heads up and explaination !

I will wait to see if a fix is push to migrate to Vite 5 !

Thanks ;)

MaxRenglet avatar Dec 27 '23 11:12 MaxRenglet