Skip to content

Commit 9c6323c

Browse files
authored
fix: handle missing .env (#112)
1 parent 7344f9a commit 9c6323c

3 files changed

Lines changed: 10 additions & 16 deletions

File tree

cmd/tsk/tsk.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ func main() {
111111
os.Exit(1)
112112
}
113113

114-
exec.RunTasks(exec.Config, &opts.tasks)
114+
if err := exec.RunTasks(exec.Config, &opts.tasks); err != nil {
115+
fmt.Println(err)
116+
os.Exit(1)
117+
}
115118
}
116119

117120
func parseArgs(args []string, dashIndex int) (tasks []string, cliArgs string) {

internal/task/task_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ func TestConfig_CompileEnv(t *testing.T) {
9999
}
100100

101101
_, err := config.CompileEnv()
102-
if err == nil {
103-
t.Fatalf("expected error, got nil")
102+
// we expect this to be non-fatal
103+
if err != nil {
104+
t.Fatalf("expected nil, got err: %v", err)
104105
}
105106
})
106107
}
@@ -204,18 +205,6 @@ func TestTask_CompileEnv(t *testing.T) {
204205
}
205206
}
206207
})
207-
208-
t.Run("error loading dotenv", func(t *testing.T) {
209-
task := Task{
210-
DotEnv: "nonexistent.env",
211-
Dir: "/some/nonexistent/path",
212-
}
213-
214-
_, err := task.CompileEnv([]string{})
215-
if err == nil {
216-
t.Fatalf("expected error, got nil")
217-
}
218-
})
219208
}
220209

221210
func TestRunCmd(t *testing.T) {

internal/task/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ func readDotEnv(filename string) (map[string]string, error) {
4242
func appendDotEnvToEnv(env []string, dotenv string) ([]string, error) {
4343
additionalEnv, err := readDotEnv(dotenv)
4444
if err != nil {
45-
return nil, err
45+
// the dotenv file missing is non-fatal. log a warning and continue
46+
fmt.Printf("Warning: Could not load dotenv file %s: %v\n", dotenv, err)
47+
return env, nil
4648
}
4749
env = append(env, ConvertEnvToStringSlice(additionalEnv)...)
4850
return env, nil

0 commit comments

Comments
 (0)