Skip to content

fix: allow env var to override non-zero struct values (#364) - #365

Open
jyr94 wants to merge 1 commit into
caarlos0:mainfrom
jyr94:fix-env-var-override
Open

jyr94 wants to merge 1 commit into
caarlos0:mainfrom
jyr94:fix-env-var-override

Conversation

@jyr94

@jyr94 jyr94 commented Jul 15, 2025

Copy link
Copy Markdown

Fixes #364

What this does

  • Ensures that environment variables override struct values,
    even when SetDefaultsForZeroValuesOnly is true.
  • Adjusts get(...) to return a flag indicating the value source.
  • Modifies setField to prioritize env vars.
  • Adds a test that reproduces the bug.

Why

The current logic prevents environment values from being applied
when the struct field is non-zero, which is not the intended behavior.

@kukymbr

kukymbr commented Nov 7, 2025

Copy link
Copy Markdown

@caarlos0 hi!
No pressure, but we really need this 🥹

Comment thread env.go
fieldParams.HasDefaultValue,
opts.Environment,
)
if _, ok := opts.Environment[fieldParams.Key]; ok && val != "" {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think we can just directly check isDefault here.

@caarlos0 caarlos0 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Bot review by GitHub Copilot. Requesting changes for the regressions noted inline. Both target regressions were reproduced; base behavior was confirmed by source inspection, not execution. No local build was run.

Comment thread env.go
Comment on lines +605 to +606
if _, ok := opts.Environment[fieldParams.Key]; ok && val != "" {
fromEnv = true

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

[P2] Do not mark a fallback default as an environment value

With Username initialized to "root", tags env:"USERNAME" envDefault:"admin", Environment: map[string]string{"USERNAME": ""}, and SetDefaultsForZeroValuesOnly: true, this now replaces "root" with "admin". getOr selects the default and returns isDefault=true, but this check sees the existing key and the nonempty selected default, sets fromEnv=true, and bypasses default preservation. Derive provenance from the existing exists/isDefault results rather than key presence alone. Add a regression test for an explicitly empty environment variable asserting no error and preservation of the initialized value.

Comment thread env.go
}

if value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero()) {
if fromEnv || (value != "" && (!opts.SetDefaultsForZeroValuesOnly || refField.IsZero())) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

[P2] Preserve the final nonempty-value guard

For an initialized integer field tagged env:"PORT,expand" and Environment: map[string]string{"PORT": "${MISSING}"}, expansion resolves to an empty string, but fromEnv remains true and this calls the integer parser with "", producing strconv.ParseInt: parsing "": invalid syntax. Loading an empty readable file through an integer ,file field has the same regression. Both occur with SetDefaultsForZeroValuesOnly disabled as well as enabled; the base skips empty resolved values and preserves the initialized field. Keep value != "" outside the override condition: value != "" && (fromEnv || !opts.SetDefaultsForZeroValuesOnly || refField.IsZero()). Add focused empty-expansion and empty-file tests under both option settings, asserting no error and preservation of the initialized integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Values are not loaded from environment with SetDefaultsForZeroValuesOnly

4 participants