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

Enable picking truly random port in serve mode #3692

Closed
ptrdom opened this issue Mar 11, 2024 · 9 comments
Closed

Enable picking truly random port in serve mode #3692

ptrdom opened this issue Mar 11, 2024 · 9 comments
Labels

Comments

@ptrdom
Copy link

ptrdom commented Mar 11, 2024

Currently there is no way to tell esbuild to pick a truly random port in serve mode - if port is unset or set to 0 then port in 8000-8009 range gets picked up. Ideally if port is set to 0 then this would just be passed to Go listen implementation and it would pick a free random port.

@ptrdom ptrdom changed the title Enable picking truly random port f Enable picking truly random port in serve mode Mar 11, 2024
@ghost
Copy link

ghost commented Mar 12, 2024

So I just have to modify this right:
if serveOptions.Port == 0 { // Default to picking a "800X" port if result, err := net.Listen(network, net.JoinHostPort(host, ":0")); err == nil { listener = result break } }
This will allow us to use random port can anybody check if my understanding is true

@ptrdom
Copy link
Author

ptrdom commented Mar 12, 2024

It is somewhat tricky - I think we want to preserve default behavior of keeping the port in familiar range of 8000-8009, but also allow passing 0. Problem here comes mainly from the fact that Go does not have nullable variables, so 0 expressed both user setting port to 0 and user not setting the port. The code likely needs a separate variable to track whether user has set the port or not.

@ghost
Copy link

ghost commented Mar 12, 2024

Thanks for the feedback, should i pr my changes or leave it
PS: am a beginner

@ptrdom
Copy link
Author

ptrdom commented Mar 12, 2024

Not sure, @evanw could you comment on this issue? Need your confirmation before exploring solutions.

@ghost
Copy link

ghost commented Mar 12, 2024

Hey yo i did this pr #3693 and i also tested locally which was confusing to setup and test but it has the random functionality

@evanw
Copy link
Owner

evanw commented Mar 14, 2024

I can do this but making the port nullable in the Go API is a breaking change, so it'll have to wait until a breaking change release to do it. Adding the breaking label for now to track this.

@evanw evanw added the breaking label Mar 14, 2024
@ghost
Copy link

ghost commented Mar 16, 2024

K

@evanw
Copy link
Owner

evanw commented Jun 30, 2024

Ideally if port is set to 0 then this would just be passed to Go listen implementation and it would pick a free random port.

I'm not sure that this is necessary. What is the reason to have a random port from the full range instead of a random port from a narrow range? I'm not sure why it would be ideal for esbuild to do that. What happens now because esbuild doesn't do that?

@ptrdom
Copy link
Author

ptrdom commented Jul 1, 2024

Ideally if port is set to 0 then this would just be passed to Go listen implementation and it would pick a free random port.

I'm not sure that this is necessary. What is the reason to have a random port from the full range instead of a random port from a narrow range? I'm not sure why it would be ideal for esbuild to do that. What happens now because esbuild doesn't do that?

There is only a slight semantic benefit - I have a library (https://github.com/ptrdom/scalajs-esbuild) where I am running esbuild behind a proxy, basically the setup of https://esbuild.github.io/api/#serve-proxy, so I would like to put esbuild on a truly random port, which I observed ends up in ephemeral port range, to signal that it should not be directly used. Maybe a portRange would be a more meaningful argument to have?

This is not something that other build tools provide - vite behaves similar to esbuild for example, and I can bet you have more urgent outstanding issues, but I do not think such feature is totally not beneficial.

What do you think about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants