Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 325433c

Browse files
authored
Merge pull request joho#29 from joho/respect_empty_external_env_vars
Respect preset empty external env vars
2 parents eaf676f + 034acc2 commit 325433c

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

godotenv.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,15 @@ func loadFile(filename string, overload bool) error {
119119
return err
120120
}
121121

122+
currentEnv := map[string]bool{}
123+
rawEnv := os.Environ()
124+
for _, rawEnvLine := range rawEnv {
125+
key := strings.Split(rawEnvLine, "=")[0]
126+
currentEnv[key] = true
127+
}
128+
122129
for key, value := range envMap {
123-
if os.Getenv(key) == "" || overload {
130+
if !currentEnv[key] || overload {
124131
os.Setenv(key, value)
125132
}
126133
}

godotenv_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,12 @@ func TestLoadDoesNotOverride(t *testing.T) {
100100
// ensure NO overload
101101
presets := map[string]string{
102102
"OPTION_A": "do_not_override",
103+
"OPTION_B": "",
103104
}
104105

105106
expectedValues := map[string]string{
106107
"OPTION_A": "do_not_override",
108+
"OPTION_B": "",
107109
}
108110
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets)
109111
}

0 commit comments

Comments
 (0)