Skip to content

Commit

Permalink
fix: if a line contains multiple # characters, there will be issues w… (
Browse files Browse the repository at this point in the history
#238)

* fix: if a line contains multiple # characters, there will be issues when traversing from back to front

* fix: typo
  • Loading branch information
astak16 authored Dec 16, 2024
1 parent a7f6c4c commit 3a7a190
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
3 changes: 3 additions & 0 deletions fixtures/comments.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Full line comment
qux=thud # fred # other
thud=fred#qux # other
fred=qux#baz # other # more
foo=bar # baz
bar=foo#baz
baz="foo"#bar
25 changes: 14 additions & 11 deletions godotenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,12 @@ func TestErrorParsing(t *testing.T) {
func TestComments(t *testing.T) {
envFileName := "fixtures/comments.env"
expectedValues := map[string]string{
"foo": "bar",
"bar": "foo#baz",
"baz": "foo",
"qux": "thud",
"thud": "fred#qux",
"fred": "qux#baz",
"foo": "bar",
"bar": "foo#baz",
"baz": "foo",
}

loadEnvAndCompareValues(t, Load, envFileName, expectedValues, noopPresets)
Expand Down Expand Up @@ -588,42 +591,42 @@ func TestWhitespace(t *testing.T) {
}{
"Leading whitespace": {
input: " A=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading tab": {
input: "\tA=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading mixed whitespace": {
input: " \t \t\n\t \t A=a\n",
key: "A",
key: "A",
value: "a",
},
"Leading whitespace before export": {
input: " \t\t export A=a\n",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace": {
input: "A=a \t \t\n",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace with export": {
input: "export A=a\t \t \n",
key: "A",
key: "A",
value: "a",
},
"No EOL": {
input: "A=a",
key: "A",
key: "A",
value: "a",
},
"Trailing whitespace with no EOL": {
input: "A=a ",
key: "A",
key: "A",
value: "a",
},
}
Expand Down
6 changes: 3 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func extractVarValue(src []byte, vars map[string]string) (value string, rest []b
}

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

0 comments on commit 3a7a190

Please sign in to comment.