bake: fix inherited file-relative contexts
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
+6
-2
@@ -449,8 +449,7 @@ func (t *Target) rebaseContextPaths() {
|
|||||||
t.Context = &contextPath
|
t.Context = &contextPath
|
||||||
}
|
}
|
||||||
} else if t.hasDefaultContextBase {
|
} else if t.hasDefaultContextBase {
|
||||||
contextPath := rebaseContextPath(t.defaultContextBase, ".")
|
t.useDefaultContextBase = true
|
||||||
t.Context = &contextPath
|
|
||||||
}
|
}
|
||||||
for k, v := range t.Contexts {
|
for k, v := range t.Contexts {
|
||||||
if base, ok := t.contextsBase[k]; ok {
|
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
|
t.Inherits = nil
|
||||||
if t.Context == nil {
|
if t.Context == nil {
|
||||||
s := "."
|
s := "."
|
||||||
|
if t.useDefaultContextBase {
|
||||||
|
s = rebaseContextPath(t.defaultContextBase, ".")
|
||||||
|
}
|
||||||
t.Context = &s
|
t.Context = &s
|
||||||
}
|
}
|
||||||
if t.Dockerfile == nil || (t.Dockerfile != nil && *t.Dockerfile == "") {
|
if t.Dockerfile == nil || (t.Dockerfile != nil && *t.Dockerfile == "") {
|
||||||
@@ -849,6 +851,7 @@ type Target struct {
|
|||||||
|
|
||||||
defaultContextBase string
|
defaultContextBase string
|
||||||
hasDefaultContextBase bool
|
hasDefaultContextBase bool
|
||||||
|
useDefaultContextBase bool
|
||||||
contextBase string
|
contextBase string
|
||||||
hasContextBase bool
|
hasContextBase bool
|
||||||
contextsBase map[string]string
|
contextsBase map[string]string
|
||||||
@@ -973,6 +976,7 @@ func (t *Target) Merge(t2 *Target) {
|
|||||||
if t2.hasDefaultContextBase {
|
if t2.hasDefaultContextBase {
|
||||||
t.defaultContextBase = t2.defaultContextBase
|
t.defaultContextBase = t2.defaultContextBase
|
||||||
t.hasDefaultContextBase = true
|
t.hasDefaultContextBase = true
|
||||||
|
t.useDefaultContextBase = t2.useDefaultContextBase
|
||||||
}
|
}
|
||||||
if t2.Context != nil {
|
if t2.Context != nil {
|
||||||
t.Context = t2.Context
|
t.Context = t2.Context
|
||||||
|
|||||||
@@ -869,6 +869,55 @@ target "other" {
|
|||||||
require.Equal(t, filepath.ToSlash(filepath.Clean("two")), *m["other"].Context)
|
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) {
|
func TestOverridesNotRebased(t *testing.T) {
|
||||||
fp := File{
|
fp := File{
|
||||||
Name: filepath.Join("subdir", "docker-bake.hcl"),
|
Name: filepath.Join("subdir", "docker-bake.hcl"),
|
||||||
|
|||||||
Reference in New Issue
Block a user