buildEnd not trigger with vite 5.0.10
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
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [X] The provided reproduction is a minimal reproducible example of the bug.
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).
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 ?
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)
Thanks @userquin, I will figure this out.
Should I rename the issue about the buildEnd triggered in dev in Vite 4.5.1 ?
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.
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 the plugin shouldn't be called in build, it has apply: 'serve', check Conditional Application
EDIT: I think I have misunderstood the issue
I don't see the plugin running when running npm run build in the repro 🤔
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 ;)