Repository navigation
fix: respect VITE_HMR_PORT when selecting the HMR port - #129
Merged
Merged
Conversation
Member
|
Thanks for the fix :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 Linked issue
Closes #128
❓ Type of change
📚 Description
The problem
The Vite guide says to set
VITE_HMR_PORTto move the HMR websocket "when running several applications at once", for exampleVITE_HMR_PORT=24680 node ace serve --hmr. The assembler ignores it: the dev server and the test runner choose the port withgetRandomPort({ port: 24678 })and pass it to the app process asVITE_HMR_PORT, overwriting the user's value.This also leaves a race between two apps.
get-portonly checks that 24678 is free at that moment. Vite opens the port much later, in the child process, once the app boots. When two apps start at the same time, both checks see 24678 as free, both children are told to use it, and the second Vite fails:Reproduction
Repository: https://github.com/Sonny93/adonisjs-hmr-port-repro
npm run repro:envstartsnode ace serve --hmrwithVITE_HMR_PORT=24680: the app process receives 24678.npm run repro:racestarts two copies of the app at the same instant with differentVITE_HMR_PORTvalues: one of them logsPort 24678 is already in use, and the Vite clients of both apps connect to 24678, so the app that lost the race has no HMR. The race depends on timing: with 10 seconds between the two starts, the second check finds 24678 busy and picks another port.The fix
A new
getHmrPort()helper insrc/utils.ts, built likegetPort()is forPORT:VITE_HMR_PORTis read fromprocess.envfirst, then from the.envfiles in their order of priority;get-portstill falls back to a free port when the preferred one is busy.The dev server and both test runner modes use it. This also fixes a missing
awaitinTestRunner.runAndWatch():VITE_HMR_PORTwas set to"[object Promise]"innode ace test --watch.Apps that do not define
VITE_HMR_PORTbehave as before. Apps that define one get their port, as the docs describe.Not covered: the race itself
The check-then-open race still exists when two apps prefer the same port (for example two apps without
VITE_HMR_PORT). A complete fix would live in@adonisjs/vite: when the websocket server fails withEADDRINUSE, retry on another free port and tell the client which port to use. I kept it out of this pull request and can propose it as a follow-up if you want it.AI disclosure
This pull request was prepared with Claude Code (Claude Opus 5.5): investigation, code, tests, reproduction repository and this description. I reviewed the change and ran the reproduction before opening it.
📝 Checklist