Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildStart behavior change in Vite 6 can cause vite optimize to hang #19316

Closed
7 tasks done
ef4 opened this issue Jan 29, 2025 · 7 comments · Fixed by #19347 or #19356
Closed
7 tasks done

buildStart behavior change in Vite 6 can cause vite optimize to hang #19316

ef4 opened this issue Jan 29, 2025 · 7 comments · Fixed by #19347 or #19356
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)

Comments

@ef4
Copy link

ef4 commented Jan 29, 2025

Describe the bug

Under Vite 5, a plugin's buildStart doesn't run during vite optimize.

Under Vite 6 it does, as a side-effect of environment.init() being introduced in optimizeDeps().

And since vite optimize rather confusingly uses command === 'serve' here, plugins now have no way of knowing whether their buildStart is running in vite dev vs vite optimize.

As a result, plugins that want to manage a dev-time background task will launch that task during vite optimize and can cause node to refuse to exit, since they think they are supposed to keep serving content.

Reproduction

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

Steps to reproduce

The linked stackbliz has a minimal reproduction. Run vite optimize and observe that the process refuses to exit.

You can repeat the same vite.config.js on Vite 5.x and observe that vite optimize no longer hangs.

System Info

N/A

Used Package Manager

pnpm

Logs

No response

Validations

@ef4
Copy link
Author

ef4 commented Feb 4, 2025

This is not fixed. Please run the original stackblitz reproduction against Vite main or 6.1.0-beta.2. You'll see it still hangs.

@ef4
Copy link
Author

ef4 commented Feb 4, 2025

IMO, an alternative solution that would be less disruptive would be to change the command passed during vite optimize to "optimize" so that plugins can actually distinguish it from vite dev. It seems not helpful to pass command==="serve" when the command is literally not serve. From the git history it looks like that was original done to cause the mode to default to "development", but that could be achieved more directly by passing the mode explicitly.

@patak-dev patak-dev reopened this Feb 4, 2025
@patak-dev
Copy link
Member

@ef4 would you give more context on why you are using vite optimize. We'd like to deprecate it in Vite 6.1, as the command is leaking. Deps optimization should be an implementation detail of the vite dev server.

@patak-dev
Copy link
Member

patak-dev commented Feb 4, 2025

The reproduction passes with #19356:
https://stackblitz.com/edit/vitejs-vite-jmndxpw2?file=package.json&terminal=optimize

you can check using the preview package for the PR: https://pkg.pr.new/vite@19356

@ef4
Copy link
Author

ef4 commented Feb 4, 2025

I would be happy to drop vite optimize. The thing we need is a way to prove (in CI tests) that we haven't introduced any build errors during dep optimization.

vite optimize conveniently exits with a non-zero exit code if there are errors during dep optimization. Whereas as far as I know, vite dev will ignore errors during dep optimization.

Is there a way to either (1) force the entire build to fail if there are build errors during dep optimization or (2) register a handler for the errors themselves so we can detect them?

@patak-dev
Copy link
Member

would you provide a reproduction where vite dev doesn't fail when there is an error during dep optimization? About (1) I assume you mean during dev, as there isn't any optimization during build. About (2) dev should fail, this shouldn't be needed.

@ef4
Copy link
Author

ef4 commented Feb 5, 2025

Here is an example: https://stackblitz.com/edit/vitejs-vite-tb94xq8d?file=vite.config.js

The exception is logged to the console, but vite continues on and serves the app despite the unhandled exception during dep optimization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-downstream-blocker Blocking the downstream ecosystem to work properly (priority)
Projects
None yet
3 participants