The default generated `config/dev` file should read PORT at compile time

I have a very simple suggestion:

the generated config/dev.exs file should read from PORT at compile time to set the endpoints port.

If we did this, people making a new Phoenix app could just run:

PORT=6000 mix phx.server to change the default port. As it is this doesn’t work cause the runtime file takes over. Unless someone has a better suggestion for how to acheive this?

3 Likes

OH man, yes! I was literally about the ask this yesterday then figured I must just be missing something so I’m going to try one more time to see what I’m doing wrong. But then I figured I would just stop my other running app so I could finish what I was doing first and once again completely forgot about it.

1 Like

I can easily make the PR btw but just wanted some kind of direction on if it would be accepted first

1 Like

Adding a -p option to mix phx.server would also be an option (and more convenient for those of us who override our caps lock key :grin:). I’m sure that must have come up some time in the past 10 years but I never read any of the discussion if it has. I’ve always thought it strange that there’s not a both obvious and super-convenient way to do this.

2 Likes

Yeah that would be great, I say make a PR and link it here. I’d be surprised if there are strong objections to this.

1 Like

Wouldn’t reading PORT at compile time do the opposite of what you’re saying? I’d have to recompile my app to run it on a new port.

5 Likes

For dev, it shouldn’t be an issue since you can just restart the server, I think.

Personally, I think the environment variable should be called PHX_PORT since PORT is too generic. (I know it’s already there in config/runtime.exs, I’m just saying. Could be a “forwards compatibility” thing while keeping the legacy value.)

I’m guessing that the current behaviour is a hold-over from before Elixir/Phoenix had good support for runtime-based config. (I wasn’t around for that, but I understand that runtime config used to be an unsolved problem.)

Usually I use runtime configuration exclusively with prod config:

if config_env() == :prod do
... define runtime config
end

I think this is flexible enough for this use-case and many more other.

Yeah I suspect the proper answer here is actually changing runtime.exs to pull the setting of the port out of the prod section, and then removing the port setup from dev.exs.

2 Likes