bake: fix inherited file-relative contexts

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2026-07-21 11:25:56 +02:00
parent bc32533b6e
commit b1f1ac27bd
2 changed files with 55 additions and 2 deletions
+6 -2
View File
@@ -449,8 +449,7 @@ func (t *Target) rebaseContextPaths() {
t.Context = &contextPath
}
} else if t.hasDefaultContextBase {
contextPath := rebaseContextPath(t.defaultContextBase, ".")
t.Context = &contextPath
t.useDefaultContextBase = true
}
for k, v := range t.Contexts {
if base, ok := t.contextsBase[k]; ok {
@@ -755,6 +754,9 @@ func (c Config) ResolveTarget(name string, overrides map[string]map[string]Overr
t.Inherits = nil
if t.Context == nil {
s := "."
if t.useDefaultContextBase {
s = rebaseContextPath(t.defaultContextBase, ".")
}
t.Context = &s
}
if t.Dockerfile == nil || (t.Dockerfile != nil && *t.Dockerfile == "") {
@@ -849,6 +851,7 @@ type Target struct {
defaultContextBase string
hasDefaultContextBase bool
useDefaultContextBase bool
contextBase string
hasContextBase bool
contextsBase map[string]string
@@ -973,6 +976,7 @@ func (t *Target) Merge(t2 *Target) {
if t2.hasDefaultContextBase {
t.defaultContextBase = t2.defaultContextBase
t.hasDefaultContextBase = true
t.useDefaultContextBase = t2.useDefaultContextBase
}
if t2.Context != nil {
t.Context = t2.Context
+49
View File
@@ -869,6 +869,55 @@ target "other" {
require.Equal(t, filepath.ToSlash(filepath.Clean("two")), *m["other"].Context)
}
func TestInheritedContextRebase(t *testing.T) {
t.Run("same file", func(t *testing.T) {
fp := File{
Name: filepath.Join("subdir", "docker-bake.hcl"),
Data: []byte(`
target "base" {
context = "basectx"
}
target "app" {
inherits = ["base"]
tags = ["app:latest"]
}`),
}
m, _, err := ReadTargets(context.TODO(), []File{fp}, []string{"app"}, nil, nil, nil, &EntitlementConf{}, ParseOpt{
FileRelativePaths: true,
})
require.NoError(t, err)
require.Equal(t, filepath.ToSlash(filepath.Clean("subdir/basectx")), *m["app"].Context)
})
t.Run("cross file", func(t *testing.T) {
fp1 := File{
Name: filepath.Join("one", "docker-bake.hcl"),
Data: []byte(`
target "base" {
context = "basectx"
}`),
}
fp2 := File{
Name: filepath.Join("two", "docker-bake.hcl"),
Data: []byte(`
target "app" {
inherits = ["base"]
tags = ["app:latest"]
}`),
}
m, _, err := ReadTargets(context.TODO(), []File{fp1, fp2}, []string{"app"}, nil, nil, nil, &EntitlementConf{}, ParseOpt{
FileRelativePaths: true,
})
require.NoError(t, err)
require.Equal(t, filepath.ToSlash(filepath.Clean("one/basectx")), *m["app"].Context)
})
}
func TestOverridesNotRebased(t *testing.T) {
fp := File{
Name: filepath.Join("subdir", "docker-bake.hcl"),