Repository navigation
fix: load .env before reading HOST/PORT/UVICORN_WORKERS in cmd_serve - #45
Merged
OkiriGabriel merged 5 commits intoAug 17, 2026
Merged
Conversation
…oured cmd_serve read HOST, PORT and UVICORN_WORKERS from the environment, but the only load_dotenv() on the serve path lived in api/server.py, which uvicorn imports from the string "api.server:app" after those reads. The three settings in .env were therefore ignored and the hardcoded defaults always won. Call load_dotenv() at the top of cmd_serve, before the os.getenv reads. Precedence is unchanged: exported environment variables still beat .env (load_dotenv does not override) and --host/--port/--workers beat both. Update the .env.example comment that documented the old caveat. Fixes open-devops-agent#41 Co-Authored-By: Kimi K3 <[email protected]>
Brings in upstream's .env.example entries for PAGERDUTY_URGENCY_*, fixing tests/test_env_example_coverage.py. Co-Authored-By: Kimi K3 <[email protected]>
CI's dummy-env step creates a .env before pytest, which tripped the fixture's not-exists assertion. Save any pre-existing file's bytes and restore them after each test; remove the file only when none existed, preserving isolation for the absent-file case. Co-Authored-By: Kimi K3 <[email protected]>
Simulates the installed package living outside the directory containing .env: cwd is changed to a fresh invocation directory and the module stays in its installed location. Bare load_dotenv() anchors its upward search at the calling module, so the cwd .env is ignored after installation. Fails against production unchanged: tests/test_cli_serve_env.py::test_serve_reads_dotenv_from_invocation_cwd AssertionError: assert '127.0.0.1' == '203.0.113.7' Co-Authored-By: Kimi K3 <[email protected]>
Bare load_dotenv() anchors its upward search at the calling module, so after a normal installation (site-packages) a user's .env in the process cwd is never found and uvicorn starts on HOST/PORT/UVICORN_WORKERS defaults. Load Path.cwd() / ".env" explicitly, keeping python-dotenv's default non-override semantics: already-set environment values still win. Co-Authored-By: Kimi K3 <[email protected]>
Collaborator
|
Reviewed — CI is green across all checks. Loading .env before HOST/PORT/UVICORN_WORKERS in cmd_serve is the right fix. Merging to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
cmd_servereadHOST,PORTandUVICORN_WORKERSfrom the environment but never calledload_dotenv()— the only load happened inapi/server.py, which uvicorn imports after the values were already read..envsettings are now loaded before the reads, so the documented variables actually reach the server.Related Issues and Pull Requests
Fixes #41
Changes
devops_agent/cli.py:load_dotenv()at the top ofcmd_serve(with the module-top import). Precedence unchanged and now consistent:override=Falsekeeps exported shell env above.env, and the existingargs.host or os.getenv(...)fallbacks keep--host/--port/--workersflags above both.env.example: the SERVER-section caveat written before this fix ("export them rather than relying on .env alone") is now false — rewritten to state the actual precedencetests/test_cli_serve_env.py(4 tests) driving the realcli.main()with onlyuvicorn.runstubbedTesting
Test-first: the
.envtest is red against the un-fixed code, green after. Full suite locally: only the pre-existingtest_mcp.pypin failure — which #38 fixed upstream since; on current main the suite is green except the PagerDuty/.env.example drift (see the separate issue). Adversarially verified, including the load-order claim and a no-regression check on the other subcommands.Footer
Generated by Kimi K3 (brief, implementation, review, verification)