diff --git a/bake/compose.go b/bake/compose.go index 84c1fd4fe1b9..8151cc46fd5e 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -263,10 +263,13 @@ func loadDotEnv(curenv map[string]string, workingDir string) (map[string]string, return nil, err } - if _, err = os.Stat(ef); os.IsNotExist(err) { - return curenv, nil - } else if err != nil { + if st, err := os.Stat(ef); err != nil { + if os.IsNotExist(err) { + return curenv, nil + } return nil, err + } else if st.IsDir() { + return curenv, nil } dt, err := os.ReadFile(ef) diff --git a/bake/compose_test.go b/bake/compose_test.go index 9a5e97de2532..6c04b7f5cbd4 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -846,6 +846,22 @@ services: require.Equal(t, map[string]string{"base": "target:base"}, c.Targets[1].Contexts) } +func TestDotEnvDir(t *testing.T) { + tmpdir := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(tmpdir, ".env"), 0755)) + + dt := []byte(` +services: + foo: + build: + context: . +`) + + chdir(t, tmpdir) + _, err := ParseComposeFiles([]File{{Name: "compose.yml", Data: dt}}) + require.NoError(t, err) +} + // chdir changes the current working directory to the named directory, // and then restore the original working directory at the end of the test. func chdir(t *testing.T, dir string) {