Skip to content

Add .env file support#8

Merged
jbsmith7741 merged 4 commits into
mainfrom
env_file
Jun 12, 2026
Merged

Add .env file support#8
jbsmith7741 merged 4 commits into
mainfrom
env_file

Conversation

@jbsmith7741

@jbsmith7741 jbsmith7741 commented May 18, 2026

Copy link
Copy Markdown
Member

Quality check

  • Documentation included
  • Test coverage

Summary

Adds support for loading a working-directory .env file when OptEnv is enabled, alongside existing environment-variable mapping.

  • Introduces a dotenv parser (internal/encode/env/dotenv.go) with quoted-value handling and inline-comment trimming; a follow-up commit tightens quote edge cases.
  • Load / LoadEnv now apply .env values per struct field (non-empty .env entries override os.Getenv for that key) before file and flag sources.
  • Extends env encode/decode and file decode wiring; updates config.go load-order comments.
  • Adds dotenv_test.go and related env tests; refreshes fixture configs under test/.

Note

Medium Risk
Changes how environment-style config is decoded (flat GetVal vs prior nested env mapping) and introduces new parsing rules that affect load order and field population when .env or OptEnv is used.

Overview
Adds a dotenv parser and wires .env files into config loading: file.Load and -c paths with a .env extension use LoadEnvFile, which parses keys/values (quotes, escapes, inline comments) and maps them through the env Decoder via an injectable GetVal hook.

The env decoder now resolves values through GetVal (default os.Getenv) instead of the old recursive populate helper; env encode quotes strings when needed. Public docs describe cwd .env under OptEnv/LoadEnv, and tests/fixtures cover dotenv parsing plus loading test/.env. CI bumps Go versions (module go 1.18, trial v0.8.0).

Reviewed by Cursor Bugbot for commit 19d395c. Bugbot is set up for automated code reviews on this repo. Configure here.

@codecov

codecov Bot commented May 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 26.35%. Comparing base (3bcfc37) to head (fe5c348).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main       #8      +/-   ##
==========================================
+ Coverage   23.87%   26.35%   +2.48%     
==========================================
  Files           1        1              
  Lines         155      148       -7     
==========================================
+ Hits           37       39       +2     
+ Misses        105       96       -9     
  Partials       13       13              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jbsmith7741

Copy link
Copy Markdown
Member Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 19d395c. Configure here.

Comment thread config.go
// set value to field.
if err := encode.SetField(field, envVal, vStruct.Type().Field(i)); err != nil {
return fmt.Errorf("'%s' from '%s' cannot be set to %s (%s)", envVal, name, vStruct.Type().Field(i).Name, field.Type())
return fmt.Errorf("'%s' from '%s' cannot be set to %s (%s) %v", envVal, name, vStruct.Type().Field(i).Name, field.Type(), err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested struct env mapping removed

High Severity

Decoder.Unmarshal no longer recurses into nested structs or applies env prefixes / omitprefix, after populate was removed. Nested fields (e.g. LEVEL2_*, embedded structs, documented omitprefix layouts) are not filled from the environment unless the parent struct implements encoding.TextUnmarshaler.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 19d395c. Configure here.

@jbsmith7741
jbsmith7741 merged commit d3d99e6 into main Jun 12, 2026
10 checks passed
@jbsmith7741
jbsmith7741 deleted the env_file branch June 12, 2026 20:06
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