Skip to content

fix(cli): pyfly run resolves the port like the application does - #165

Merged
ancongui merged 1 commit into
mainfrom
fix/run-port-honours-env-and-profiles
Sep 13, 2026
Merged

ancongui merged 1 commit into
mainfrom
fix/run-port-honours-env-and-profiles

Conversation

@ancongui

Copy link
Copy Markdown
Contributor

What was wrong

pyfly run picked the bind port as --port → a raw read of pyfly.yaml8080. That bypassed the configuration system, so three documented ways of setting the port were silently inert for the CLI:

  • PYFLY_SERVER_PORT=8090 (the relaxed-binding override Config.get() honours for every key);
  • -D server.port=9000 — the very example in --define's help text, which _build_launch_env turns into that same variable;
  • pyfly-{profile}.yaml overlays (and config/pyfly.yaml).

The application process reads the same key through Config and believed the overridden value, so the banner/log said one port and the server bound another. Found while starting a service on a machine where 8080 was already taken: the server bound 8080 next to the other process and nothing listened where the operator asked (PYFLY_MANAGEMENT_SERVER_PORT worked, which made it look like a per-key bug).

Fix

_read_port_from_config() resolves with the same precedence as Config: PYFLY_SERVER_PORT first (a non-integer is a click.BadParameter, not a silent 8080), then Config.from_sources(cwd, active_profiles=…, load_defaults=False).get("pyfly.server.port"), and only then the raw file as a last resort. --port still wins over everything.

Tests

TestReadPortFromConfig (7 cases): no file, file, env over file, env without file, -D server.port=9000 end to end through _build_launch_env, profile overlay over base, bad env value. ruff, ruff format, mypy --strict, tests/cli (300) and the unit suite (4972 passed) are green locally.

`pyfly run` chose the port from the --port flag, then a raw read of pyfly.yaml, then
8080. That bypassed the configuration system entirely: the relaxed-binding override
PYFLY_SERVER_PORT, the -D server.port=9000 example advertised by --define's own help
text (which becomes exactly that variable), and the profile overlays were all inert for
the bind port, while the application read the same key through Config and believed the
overridden value. Found starting a service whose default port was taken: the server
bound 8080 next to the other process and nothing listened where the operator asked.

The CLI now resolves the port with the same precedence as Config: PYFLY_SERVER_PORT
first (a non-integer is a usage error, not a silent 8080), then the merged
configuration for the active profiles, and only then the raw file. Seven tests cover
the file, the env override with and without a file, the -D path end to end, the profile
overlay and the bad value.
@ancongui
ancongui merged commit b496e40 into main Sep 13, 2026
6 checks passed
@ancongui
ancongui deleted the fix/run-port-honours-env-and-profiles branch September 13, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant