Skip to content

Commit 3a7a190

Browse files
authored
fix: if a line contains multiple # characters, there will be issues w… (#238)
* fix: if a line contains multiple # characters, there will be issues when traversing from back to front * fix: typo
1 parent a7f6c4c commit 3a7a190

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

fixtures/comments.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Full line comment
2+
qux=thud # fred # other
3+
thud=fred#qux # other
4+
fred=qux#baz # other # more
25
foo=bar # baz
36
bar=foo#baz
47
baz="foo"#bar

godotenv_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,12 @@ func TestErrorParsing(t *testing.T) {
477477
func TestComments(t *testing.T) {
478478
envFileName := "fixtures/comments.env"
479479
expectedValues := map[string]string{
480-
"foo": "bar",
481-
"bar": "foo#baz",
482-
"baz": "foo",
480+
"qux": "thud",
481+
"thud": "fred#qux",
482+
"fred": "qux#baz",
483+
"foo": "bar",
484+
"bar": "foo#baz",
485+
"baz": "foo",
483486
}
484487

485488
loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
@@ -588,42 +591,42 @@ func TestWhitespace(t *testing.T) {
588591
}{
589592
"Leading whitespace": {
590593
input: " A=a\n",
591-
key: "A",
594+
key: "A",
592595
value: "a",
593596
},
594597
"Leading tab": {
595598
input: "\tA=a\n",
596-
key: "A",
599+
key: "A",
597600
value: "a",
598601
},
599602
"Leading mixed whitespace": {
600603
input: " \t \t\n\t \t A=a\n",
601-
key: "A",
604+
key: "A",
602605
value: "a",
603606
},
604607
"Leading whitespace before export": {
605608
input: " \t\t export A=a\n",
606-
key: "A",
609+
key: "A",
607610
value: "a",
608611
},
609612
"Trailing whitespace": {
610613
input: "A=a \t \t\n",
611-
key: "A",
614+
key: "A",
612615
value: "a",
613616
},
614617
"Trailing whitespace with export": {
615618
input: "export A=a\t \t \n",
616-
key: "A",
619+
key: "A",
617620
value: "a",
618621
},
619622
"No EOL": {
620623
input: "A=a",
621-
key: "A",
624+
key: "A",
622625
value: "a",
623626
},
624627
"Trailing whitespace with no EOL": {
625628
input: "A=a ",
626-
key: "A",
629+
key: "A",
627630
value: "a",
628631
},
629632
}

parser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
143143
}
144144

145145
// Work backwards to check if the line ends in whitespace then
146-
// a comment (ie asdasd # some comment)
147-
for i := endOfVar - 1; i >= 0; i-- {
148-
if line[i] == charComment && i > 0 {
146+
// a comment, ie: foo=bar # baz # other
147+
for i := 0; i < endOfVar; i++ {
148+
if line[i] == charComment && i < endOfVar {
149149
if isSpace(line[i-1]) {
150150
endOfVar = i
151151
break

0 commit comments

Comments
 (0)