fix: warn on malformed .env lines instead of silently skipping them#276
Open
spartan124 wants to merge 1 commit into
Open
fix: warn on malformed .env lines instead of silently skipping them#276spartan124 wants to merge 1 commit into
spartan124 wants to merge 1 commit into
Conversation
spartan124
marked this pull request as ready for review
July 25, 2026 19:37
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.
Closes #172
📌 Description
Previously, internal/config/dotenv.go utilized godotenv.Load() and silently ignored any
parsing errors. If a .env file contained a malformed line (e.g., missing =,
unterminated quote), a typo would silently cause variables to remain unset rather than
failing fast, surfacing later as confusing downstream errors.
This PR implements a robust warn-and-skip parser strategy. Instead of entirely aborting
.env loading, malformed lines are now isolated, loudly logged to stderr with a
descriptive warning, and skipped, allowing the remainder of the valid .env
configuration to be safely loaded.
🧩 Changes made
• Warn-and-Skip Parser Strategy: Replaced the bulk godotenv.Load() call with a custom
bufio.Scanner line-by-line wrapper.
• Enhanced Warning Logs: Malformed lines (missing = or unterminated quotes) now
correctly trigger a detailed WARNING log indicating the exact line context and file
path.
• Robust Duplicate Key Handling: Enforces explicit last-wins behavior within the same .
env file prior to updating the OS environment (which still safely ignores already-
exported OS variables).
• Added Full Test Suite: Created internal/config/dotenv_test.go covering edge cases to
guarantee stability.
🧪 Testing and Acceptance
go test ./internal/config/... -v -cover shows comprehensive test coverage over 95%.
The test suite explicitly verifies:
• ✅ Valid .env files parse identically to before.
• ✅ Missing = signs log a warning and correctly skip the single broken line.
• ✅ Unterminated quotes log a warning and properly capture block-level failures
without panicking.
• ✅ Duplicate keys resolve explicitly to a last-wins outcome within the file.
• ✅ Empty files and comment-only lines load gracefully.
• ✅ Existing OS environment variables are not overwritten.
Test Output
🔒 Security Notes
Verified that no sensitive defaults are implicitly used as fallbacks if parsing fails.
This change only adds verbosity to local dev tooling; downstream validation layers
continue to gate execution on missing required variables.