Skip to content

Commit e5712ff

Browse files
committed
improve dotenv error messages
- print the variable name that failed to parse - print the name of the file where the syntax error appeared Signed-off-by: Nick Santos <[email protected]>
1 parent 736df56 commit e5712ff

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

dotenv/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ loop:
123123
}
124124

125125
return "", "", inherited, fmt.Errorf(
126-
`line %d: unexpected character %q in variable name`,
127-
p.line, string(rune))
126+
`line %d: unexpected character %q in variable name %q`,
127+
p.line, string(rune), strings.Split(src, "\n")[0])
128128
}
129129
}
130130

dotenv/parser_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,9 @@ func TestParseBytes(t *testing.T) {
3232
assert.Equal(t, value, out[key])
3333
}
3434
}
35+
36+
func TestParseVariable(t *testing.T) {
37+
err := newParser().parse("%!(EXTRA string)=foo", map[string]string{}, nil)
38+
assert.Error(t, err, "line 1: unexpected character \"%\" in variable name \"%!(EXTRA string)=foo\"")
39+
40+
}

types/project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func (p Project) ResolveServicesEnvironment(discardEnvFiles bool) error {
507507

508508
fileVars, err := dotenv.ParseWithLookup(bytes.NewBuffer(b), resolve)
509509
if err != nil {
510-
return err
510+
return errors.Wrapf(err, "failed to read %s", envFile)
511511
}
512512
environment.OverrideBy(Mapping(fileVars).ToMappingWithEquals())
513513
}

0 commit comments

Comments
 (0)