From 431732f5d1f029f14adbf5828ae9798c0b9b3936 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 14 Jun 2023 12:17:32 +0100 Subject: [PATCH] bake: fix incorrect dockerfile resolution against cwd:// context We need to resolve the strip the cwd:// prefix before attempting to resolve the dockerfile. Otherwise, we'll get the cwd:// prefix in the dockerfile name, which isn't stripped out later. Signed-off-by: Justin Chadwell --- bake/bake.go | 6 +++--- bake/bake_test.go | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 39cd927e..0fac4e3a 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -1048,12 +1048,12 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { bi.DockerfileInline = *t.DockerfileInline } updateContext(&bi, inp) - if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) { - bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath) - } if strings.HasPrefix(bi.ContextPath, "cwd://") { bi.ContextPath = path.Clean(strings.TrimPrefix(bi.ContextPath, "cwd://")) } + if !build.IsRemoteURL(bi.ContextPath) && bi.ContextState == nil && !path.IsAbs(bi.DockerfilePath) { + bi.DockerfilePath = path.Join(bi.ContextPath, bi.DockerfilePath) + } for k, v := range bi.NamedContexts { if strings.HasPrefix(v.Path, "cwd://") { bi.NamedContexts[k] = build.NamedContext{Path: path.Clean(strings.TrimPrefix(v.Path, "cwd://"))} diff --git a/bake/bake_test.go b/bake/bake_test.go index efebea20..75306cb2 100644 --- a/bake/bake_test.go +++ b/bake/bake_test.go @@ -386,18 +386,19 @@ func TestHCLCwdPrefix(t *testing.T) { m, g, err := ReadTargets(ctx, []File{fp}, []string{"app"}, nil, nil) require.NoError(t, err) - require.Equal(t, 1, len(m)) - _, ok := m["app"] - require.True(t, ok) - - _, err = TargetsToBuildOpt(m, &Input{}) + bo, err := TargetsToBuildOpt(m, &Input{}) require.NoError(t, err) + require.Equal(t, 1, len(g)) + require.Equal(t, []string{"app"}, g["default"].Targets) + + require.Equal(t, 1, len(m)) + require.Contains(t, m, "app") require.Equal(t, "test", *m["app"].Dockerfile) require.Equal(t, "foo", *m["app"].Context) - require.Equal(t, 1, len(g)) - require.Equal(t, []string{"app"}, g["default"].Targets) + require.Equal(t, "foo/test", bo["app"].Inputs.DockerfilePath) + require.Equal(t, "foo", bo["app"].Inputs.ContextPath) } func TestOverrideMerge(t *testing.T) {