Skip to content

Commit 63c5310

Browse files
committed
Fix hash usage in environment values
Signed-off-by: Ulysses Souza <[email protected]>
1 parent 045c678 commit 63c5310

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

dotenv/godotenv.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,16 @@ func parseLineWithLookup(line string, envMap map[string]string, lookupFn LookupF
242242
}
243243

244244
// ditch the comments (but keep quoted hashes)
245-
if strings.Contains(line, "#") {
245+
if strings.HasPrefix(strings.TrimSpace(line), "#") || strings.Contains(line, " #") {
246246
segmentsBetweenHashes := strings.Split(line, "#")
247247
quotesAreOpen := false
248248
var segmentsToKeep []string
249249
for _, segment := range segmentsBetweenHashes {
250250
if strings.Count(segment, "\"") == 1 || strings.Count(segment, "'") == 1 {
251251
if quotesAreOpen {
252-
quotesAreOpen = false
253252
segmentsToKeep = append(segmentsToKeep, segment)
254-
} else {
255-
quotesAreOpen = true
256253
}
254+
quotesAreOpen = !quotesAreOpen
257255
}
258256

259257
if len(segmentsToKeep) == 0 || quotesAreOpen {

dotenv/godotenv_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ func TestParsing(t *testing.T) {
386386
// it 'ignores inline comments' do
387387
// expect(env("foo=bar # this is foo")).to eql('foo' => 'bar')
388388
parseAndCompare(t, "FOO=bar # this is foo", "FOO", "bar")
389+
parseAndCompare(t, "FOO=123#not-an-inline-comment", "FOO", "123#not-an-inline-comment")
389390

390391
// it 'allows # in quoted value' do
391392
// expect(env('foo="bar#baz" # comment')).to eql('foo' => 'bar#baz')

0 commit comments

Comments
 (0)