feat: auto-load .env alongside agctl.yaml (--env-file / AGCTL_ENV_FILE)#48
Merged
Conversation
agctl's \${VAR} interpolation read only os.environ, so using a .env file
required manually sourcing it first. This adds first-class .env support:
- Auto-load .env next to the resolved agctl.yaml as env defaults, with real
environment winning (.env fills blanks only — docker-compose/dotenv
convention; CI/prod inject real env over committed defaults).
- Explicit sources: --env-file <path> (global flag + per-command wherever
--config is declared) and AGCTL_ENV_FILE. Precedence: --env-file >
AGCTL_ENV_FILE > sibling .env. Missing sibling = no-op; missing explicit
source = ConfigError (exit 2).
- python-dotenv parses with its own \${VAR} expansion disabled, so agctl's
single interpolate() owns all \${...} resolution (chained/nested).
AGCTL_* lines in .env apply as overrides (real env still wins).
New module agctl/config/env_file.py; merge step in compose_config after
config discovery, before interpolation. Adds python-dotenv>=1.0 dep.
Reviewed via a 4-reviewer parallel team; all Critical/Important findings
fixed and guarded by regression tests (incl. mock-start daemon argv
forwarding and malformed-UTF8 error wrapping).
Tests: 24 in tests/unit/test_env_file.py + a mock-start daemon-argv
regression test; full suite green (1101 passed, 18 skipped).
Co-Authored-By: Claude <noreply@anthropic.com>
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
agctl's
${VAR}interpolation previously read onlyos.environ, so using a.envfile required manually sourcing it first (set -a; . ./.env; set +a). This adds first-class.envsupport — a.envnext to the resolvedagctl.yamlis loaded automatically as env defaults, with real environment winning, plus explicit--env-file/AGCTL_ENV_FILEsources.Behavior
.envsitting next to the resolvedagctl.yamlis loaded automatically — no shell sourcing needed..envprovides defaults only; a value already in the real environment overrides it (docker-compose / python-dotenv / rails / django convention — lets CI/prod inject real secrets over committed defaults).--env-file <path>(global flag, and per-command wherever--configis declared) andAGCTL_ENV_FILE.--env-file>AGCTL_ENV_FILE> sibling.env. Explicit replaces auto-load (like--configreplaces walk-up)..env→ silent no-op; missing explicit--env-file/AGCTL_ENV_FILE→ConfigError(exit 2), mirroring--config.${VAR}expansion disabled, so agctl's oneinterpolate()owns all${...}resolution (chained/nested).AGCTL_*lines in.envapply as overrides (real env still wins).Implementation
agctl/config/env_file.py:load_env_file(path, *, required)+resolve_dotenv_values(explicit, env, base_path).compose_configmerges.envdefaults (env = {**dotenv, **env}, never mutatesos.environ) after config discovery, before interpolation — so the sibling sits next to the resolved config andAGCTL_CONFIG/AGCTL_ENV_FILEset inside.envcan't steer their own resolution (no cycle).--env-fileincli.py; per-command--env-filemirroring--config; threaded through_core/_envelopehelpers and callbacks (parallel tooverlay_paths).AGCTL_ENV_FILEneeds no CLI plumbing (resolved incompose_config).python-dotenv>=1.0added to core deps.Review
Reviewed by a 4-reviewer parallel team (core pipeline, CLI threading, tests, docs). All findings resolved:
mock startnow forwards--env-fileto the spawned daemon (it re-loads config from scratch) — guarded by a regression test..envnow surfaces asConfigError(notInternalError); the headline invariant (dotenv interpolation off) and the circularity / sibling-only guards are now pinned by tests.Test plan
tests/unit/test_env_file.py— 24 tests (parsing, real-env-wins, precedence, circularity guard, sibling-only, raw-values pin, malformed-UTF8, CLI wiring)tests/unit/test_mock_commands.py— daemon-argv--env-fileforwarding regression testruff check: zero new errors; new filesruff format-cleandocs-watcher🤖 Generated with Claude Code