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

fix: use hmr port if specified #7282

Merged
merged 3 commits into from
Mar 14, 2022
Merged

fix: use hmr port if specified #7282

merged 3 commits into from
Mar 14, 2022

Conversation

benmccann
Copy link
Collaborator

Description

Closes #6068

Additional context

I don't understand what the hmr code is trying to do and it doesn't match the behavior described in the docs, which say:

When using server.middlewareMode or server.https, assigning server.hmr.server to your HTTP(S) server will process HMR connection requests through your server.

But the "assigning server.hmr.server to your HTTP(S) server" part is being completely ignored and it's using the user's server all the time. What's described in the docs is kind of a funny concept because normally you create the config and then pass it to Vite to create the server, but how would you provide the server to server.hmr.server when it hasn't been created yet? So I'm not really clear how this is intended to behave and there's probably more that could be fixed here.

In this PR, I'm just trying to fix one specific thing that's clearly broken which is that the hmr.port setting is currently ignored. I'll let y'all decide if you want to do more cleanup beyond that, but it's probably beyond the scope of what I would do because I think the tests rely on the current behavior and it's not super clear what else should be changed


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@benmccann benmccann added bug feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority) labels Mar 11, 2022
bluwy
bluwy previously approved these changes Mar 13, 2022
@bluwy
Copy link
Member

bluwy commented Mar 13, 2022

When using server.middlewareMode or server.https, assigning server.hmr.server to your HTTP(S) server will process HMR connection requests through your server.

The documentation for that paragraph was added in #3590 and updated #5163. I'm not familiar with this, but looking at the code it looks like the server is used directly regardless of middleware mode or https?

What's described in the docs is kind of a funny concept because normally you create the config and then pass it to Vite to create the server, but how would you provide the server to server.hmr.server when it hasn't been created yet? So I'm not really clear how this is intended to behave and there's probably more that could be fixed here.

If I understand the docs correctly, you provide an external server (not the vite dev server) to server.hmr.server, so there isn't a chicken-egg problem.

Maybe the code has changed and the docs got stale, but I'm also a bit confused of how this is used now 🤔

@@ -28,7 +28,7 @@ export function createWebSocketServer(
let httpsServer: Server | undefined = undefined

const hmr = isObject(config.server.hmr) && config.server.hmr
const wsServer = (hmr && hmr.server) || server
const wsServer = (hmr && hmr.server) || (!(hmr && hmr.port) && server)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check for the case where hmr.port is the same as the main server port? We should re-use the server in that case, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@patak-dev
Copy link
Member

I think the docs are confusing, IIUC the original PR that added them wanted to show a case where using hmr.server was useful but it should be rephrased so it doesn't give the impression that it only works when using that config options.

Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know if the specified server.port is occupied when creating the hmr server, the port auto-increment logic didn't kick in yet. So with the last version it may happen that the hmr.port isn't respected.
So we have two options, supposing that hmr.port === server.port

  1. your first commit,
    a. port is free, then hmr uses the port, and the normal server auto-increments
    b. port is taken, then hmr fails to listen

  2. your last commit,
    a. port is free, then hmr and normal server uses the same port
    b. port is taken, then hmr and normal server uses the next port

I think that 2 is better here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

server.hmr.port config not respected on server side
3 participants