diff --git a/godotenv.go b/godotenv.go index a210f4e..91151fe 100644 --- a/godotenv.go +++ b/godotenv.go @@ -34,7 +34,7 @@ func loadFile(filename string) (err error) { if !isIgnoredLine(fullLine) { key, value, err := parseLine(fullLine) - if err == nil { + if err == nil && os.Getenv(key) == "" { os.Setenv(key, value) } } diff --git a/godotenv_test.go b/godotenv_test.go index 2f096e7..e6ec8d9 100644 --- a/godotenv_test.go +++ b/godotenv_test.go @@ -83,6 +83,16 @@ func TestLoadQuotedEnv(t *testing.T) { loadEnvAndCompareValues(t, envFileName, expectedValues) } +func TestActualEnvVarsAreLeftAlone(t *testing.T) { + os.Clearenv() + os.Setenv("OPTION_A", "actualenv") + _ = Load("fixtures/plain.env") + + if os.Getenv("OPTION_A") != "actualenv" { + t.Error("An ENV var set earlier was overwritten") + } +} + func TestParsing(t *testing.T) { // unquoted values parseAndCompare(t, "FOO=bar", "FOO", "bar")