Skip to content

fix: respect VITE_HMR_PORT when selecting the HMR port - #129

Merged
thetutlage merged 1 commit into
adonisjs:8.xfrom
Sonny93:fix/vite-hmr-port
Nov 23, 2026
Merged

thetutlage merged 1 commit into
adonisjs:8.xfrom
Sonny93:fix/vite-hmr-port

Conversation

@Sonny93

@Sonny93 Sonny93 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Closes #128

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

The problem

The Vite guide says to set VITE_HMR_PORT to move the HMR websocket "when running several applications at once", for example VITE_HMR_PORT=24680 node ace serve --hmr. The assembler ignores it: the dev server and the test runner choose the port with getRandomPort({ port: 24678 }) and pass it to the app process as VITE_HMR_PORT, overwriting the user's value.

This also leaves a race between two apps. get-port only 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:

WebSocket server error: Port 24678 is already in use

Reproduction

Repository: https://github.com/Sonny93/adonisjs-hmr-port-repro

  • npm run repro:env starts node ace serve --hmr with VITE_HMR_PORT=24680: the app process receives 24678.
  • npm run repro:race starts two copies of the app at the same instant with different VITE_HMR_PORT values: one of them logs Port 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 in src/utils.ts, built like getPort() is for PORT:

  • VITE_HMR_PORT is read from process.env first, then from the .env files in their order of priority;
  • 24678 is used when it is not defined;
  • get-port still 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 await in TestRunner.runAndWatch(): VITE_HMR_PORT was set to "[object Promise]" in node ace test --watch.

Apps that do not define VITE_HMR_PORT behave 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 with EADDRINUSE, 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

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly. (Not needed: the docs already describe this behaviour.)

@fleet-panda fleet-panda Bot added the status: incoming The status of the hub task label Nov 17, 2026
@thetutlage thetutlage added status: review The status of the hub task and removed status: incoming The status of the hub task labels Nov 23, 2026
@thetutlage
thetutlage merged commit 4b45c24 into adonisjs:8.x Nov 23, 2026
4 checks passed
@thetutlage

Copy link
Copy Markdown
Member

Thanks for the fix :)

@fleet-panda fleet-panda Bot added status: accepting The status of the hub task status: done The status of the hub task and removed status: review The status of the hub task status: accepting The status of the hub task labels Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: done The status of the hub task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VITE_HMR_PORT is overwritten by the dev server, so two apps can race for port 24678

2 participants