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

Open
ptrdom opened this issue Mar 11, 2024 · 7 comments
Open

Enable picking truly random port in serve mode #3692

ptrdom opened this issue Mar 11, 2024 · 7 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

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