diff --git a/godotenv.go b/godotenv.go index 016acc1..0d9066c 100644 --- a/godotenv.go +++ b/godotenv.go @@ -119,8 +119,15 @@ func loadFile(filename string, overload bool) error { return err } + currentEnv := map[string]bool{} + rawEnv := os.Environ() + for _, rawEnvLine := range rawEnv { + key := strings.Split(rawEnvLine, "=")[0] + currentEnv[key] = true + } + for key, value := range envMap { - if os.Getenv(key) == "" || overload { + if !currentEnv[key] || overload { os.Setenv(key, value) } } diff --git a/godotenv_test.go b/godotenv_test.go index 0a1aff6..a62bf87 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -100,10 +100,12 @@ func TestLoadDoesNotOverride(t *testing.T) { // ensure NO overload presets := map[string]string{ "OPTION_A": "do_not_override", + "OPTION_B": "", } expectedValues := map[string]string{ "OPTION_A": "do_not_override", + "OPTION_B": "", } loadEnvAndCompareValues(t, Load, envFileName, expectedValues, presets) }