fix(env): warn instead of failing when env_from file is missing - #2901
Conversation
A missing file referenced by `env_from` caused Devbox to exit early with "failed parsing <file> file. Error: failed to open file: ...", preventing the environment from being enabled. The referenced file is often untracked and intended to be created by a command in `init_hook` (e.g. `cp -n .env.example .env`), so failing before the hook runs is a papercut. ParseEnvsFromDotEnv now wraps the underlying open error so callers can detect a missing file via errors.Is(err, os.ErrNotExist). configEnvs uses this to warn and continue when the env_from file does not exist, while still returning a hard error for genuine parse failures. Fixes #2504 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UiVJfTdwpkKJ4UDn9opMMX
There was a problem hiding this comment.
Pull request overview
This PR fixes a first-time setup papercut where env_from points to an untracked .env file that may not exist yet (often created later by init_hook). Instead of failing early, Devbox now warns and continues enabling the environment when the referenced .env file is missing, while still failing on real parse errors.
Changes:
- Wrap
os.Openerrors inParseEnvsFromDotEnvso callers can detect missing-file cases viaerrors.Is(err, os.ErrNotExist). - Update
Devbox.configEnvsto warn-and-continue when theenv_fromfile is missing, preserving hard failures for genuine parse errors. - Add unit tests covering successful parsing and the missing-file regression case.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/devconfig/configfile/env.go | Wraps .env open errors with %w so missing files can be detected reliably. |
| internal/devconfig/configfile/env_test.go | Adds coverage for parsing an existing .env and for ensuring missing files return an os.ErrNotExist-detectable error. |
| internal/devbox/devbox.go | Converts missing env_from .env files into a warning instead of a fatal error during environment enablement. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@claude please review this PR for correctness, and flag anything that would block merging. |
|
Claude finished @mikeland73's task in 2m 30s —— View job Reviewing PR #2901
ReviewI reviewed the diff ( Correctness
Minor, non-blocking observations
VerdictNo correctness issues found that would block merging. The two notes above are optional polish, not blockers. |
Summary
Fixes #2504.
When
env_frompoints to a.envfile that does not exist yet, Devbox exited early with:This is a papercut on first-time setup: the referenced file is typically untracked and intended to be created by a command in
init_hook(e.g.cp -n .env.example .env). Because parsingenv_fromhappens beforeinit_hookruns, the environment could never be enabled to bootstrap the file in the first place.Fix
internal/devconfig/configfile/env.go:ParseEnvsFromDotEnvnow wraps the underlyingos.Openerror with%w, so callers can distinguish a missing file (errors.Is(err, os.ErrNotExist)) from a genuine parse error.internal/devbox/devbox.go:configEnvstreats a missingenv_fromfile as a warning and continues enabling the environment, instead of returning a hard error. Real parse errors (file exists but is malformed) still fail as before.New behavior when the file is missing:
Changes
internal/devconfig/configfile/env.go: wrap the open error so missing files are detectable.internal/devbox/devbox.go: warn-and-continue on a missingenv_fromfile.internal/devconfig/configfile/env_test.go: newTestParseEnvsFromDotEnvcovering a valid.envfile and the missing-file (os.ErrNotExist) regression.How was it tested?
go test ./internal/devconfig/configfile/ -run TestParseEnvsFromDotEnv -v— passes.go build ./internal/...andgo vet ./internal/devconfig/configfile/ ./internal/devbox/— clean.Community Contribution License
All community contributions in this pull request are licensed to the project
maintainers under the terms of the
Apache 2 License.
By creating this pull request, I represent that I have the right to license the
contributions to the project maintainers under the Apache 2 License as stated in
the
Community Contribution License.
cc @t-monaghan (issue reporter) — thanks for the clear write-up and reproduction.
🤖 Generated with Claude Code
https://claude.ai/code/session_01UiVJfTdwpkKJ4UDn9opMMX
Generated by Claude Code