From 63c53104cfb0ba67cdae94c886d5913132a42a80 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Tue, 12 Jul 2022 22:41:41 +0200 Subject: [PATCH] Fix hash usage in environment values Signed-off-by: Ulysses Souza --- dotenv/godotenv.go | 6 ++---- dotenv/godotenv_test.go | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/dotenv/godotenv.go b/dotenv/godotenv.go index 98d66205..6685dff5 100644 --- a/dotenv/godotenv.go +++ b/dotenv/godotenv.go @@ -242,18 +242,16 @@ func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupF } // ditch the comments (but keep quoted hashes) - if strings.Contains(line, "#") { + if strings.HasPrefix(strings.TrimSpace(line), "#") || strings.Contains(line, " #") { segmentsBetweenHashes := strings.Split(line, "#") quotesAreOpen := false var segmentsToKeep []string for _, segment := range segmentsBetweenHashes { if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 { if quotesAreOpen { - quotesAreOpen = false segmentsToKeep = append(segmentsToKeep, segment) - } else { - quotesAreOpen = true } + quotesAreOpen = !quotesAreOpen } if len(segmentsToKeep) == 0 || quotesAreOpen { diff --git a/dotenv/godotenv_test.go b/dotenv/godotenv_test.go index 1bc726f2..e0dde43f 100644 --- a/dotenv/godotenv_test.go +++ b/dotenv/godotenv_test.go @@ -386,6 +386,7 @@ func TestParsing(t *testing.T) { // it 'ignores inline comments' do // expect(env("foo=bar # this is foo")).to eql('foo' => 'bar') parseAndCompare(t, "FOO=bar # this is foo", "FOO", "bar") + parseAndCompare(t, "FOO=123#not-an-inline-comment", "FOO", "123#not-an-inline-comment") // it 'allows # in quoted value' do // expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')