Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Print dotenv file path when there is an error reading file #1842

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,18 @@ func TestDotenvHasEnvVarInPath(t *testing.T) {
tt.Run(t)
}

func TestTaskDotenvParseErrorMessage(t *testing.T) {
e := task.Executor{
Dir: "testdata/dotenv/parse_error",
}

path, _ := filepath.Abs(filepath.Join(e.Dir, ".env-with-error"))
expected := fmt.Sprintf("error reading env file %s:", path)

err := e.Setup()
require.ErrorContains(t, err, expected)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used ErrorContains() and only included the part of the error message that we add, so that the tests wouldn't depend on the exact wording coming from the dotenv library.

}

func TestTaskDotenv(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/dotenv_task/default",
Expand Down
3 changes: 2 additions & 1 deletion taskfile/dotenv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package taskfile

import (
"fmt"
"os"

"github.com/joho/godotenv"
Expand Down Expand Up @@ -37,7 +38,7 @@ func Dotenv(c *compiler.Compiler, tf *ast.Taskfile, dir string) (*ast.Vars, erro

envs, err := godotenv.Read(dotEnvPath)
if err != nil {
return nil, err
return nil, fmt.Errorf("error reading env file %s: %w", dotEnvPath, err)
}
for key, value := range envs {
if ok := env.Exists(key); !ok {
Expand Down
2 changes: 2 additions & 0 deletions testdata/dotenv/parse_error/.env-with-error
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#intentional parse error
SOME_VAR
8 changes: 8 additions & 0 deletions testdata/dotenv/parse_error/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'

dotenv: ['.env-with-error']

tasks:
default:
cmd: "true"

Loading