Skip to content

Commit

Permalink
Merge pull request #283 from compose-spec/fix-inline-env-comments
Browse files Browse the repository at this point in the history
Fix hash usage in environment values
  • Loading branch information
ulyssessouza committed Jul 13, 2022
2 parents 045c678 + 63c5310 commit 2b59fba
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
6 changes: 2 additions & 4 deletions dotenv/godotenv.go
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions dotenv/godotenv_test.go
Expand Up @@ -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')
Expand Down

0 comments on commit 2b59fba

Please sign in to comment.