From e5712ff08b2170975aa7552d700ed3e8ca0ba49a Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Fri, 23 Jun 2023 13:46:42 -0400 Subject: [PATCH] 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 --- dotenv/parser.go | 4 ++-- dotenv/parser_test.go | 6 ++++++ types/project.go | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/dotenv/parser.go b/dotenv/parser.go index bbcd318f..6c03801d 100644 --- a/dotenv/parser.go +++ b/dotenv/parser.go @@ -123,8 +123,8 @@ loop: } return "", "", inherited, fmt.Errorf( - `line %d: unexpected character %q in variable name`, - p.line, string(rune)) + `line %d: unexpected character %q in variable name %q`, + p.line, string(rune), strings.Split(src, "\n")[0]) } } diff --git a/dotenv/parser_test.go b/dotenv/parser_test.go index ce580f09..340ea3f0 100644 --- a/dotenv/parser_test.go +++ b/dotenv/parser_test.go @@ -32,3 +32,9 @@ func TestParseBytes(t *testing.T) { assert.Equal(t, value, out[key]) } } + +func TestParseVariable(t *testing.T) { + err := newParser().parse("%!(EXTRA string)=foo", map[string]string{}, nil) + assert.Error(t, err, "line 1: unexpected character \"%\" in variable name \"%!(EXTRA string)=foo\"") + +} diff --git a/types/project.go b/types/project.go index 896bcd69..bf5b8a79 100644 --- a/types/project.go +++ b/types/project.go @@ -507,7 +507,7 @@ func (p Project) ResolveServicesEnvironment(discardEnvFiles bool) error { fileVars, err := dotenv.ParseWithLookup(bytes.NewBuffer(b), resolve) if err != nil { - return err + return errors.Wrapf(err, "failed to read %s", envFile) } environment.OverrideBy(Mapping(fileVars).ToMappingWithEquals()) }