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.

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

3 participants