bake: add file-relative path opt-in#3935
Conversation
|
Needs docs follow-up: https://docs.docker.com/build/building/variables/#build-tool-configuration-variables |
fc54769 to
4ce75f7
Compare
tonistiigi
left a comment
There was a problem hiding this comment.
Looks like default . does not atm take the relative mode into account
t.Run("default-context-in-subdirectory", func(t *testing.T) {
bakefile := []byte(`
target "default" {
dockerfile-inline = <<EOT
FROM scratch
COPY marker /marker
EOT
}
`)
dir := tmpdir(
t,
fstest.CreateDir("definitions", 0700),
fstest.CreateFile("definitions/docker-bake.hcl", bakefile, 0600),
fstest.CreateFile("definitions/marker", []byte("marker"), 0600),
)
dirDest := t.TempDir()
out, err := bakeCmd(
sb,
withDir(dir),
withArgs("--file", "definitions/docker-bake.hcl", "--set", "*.output=type=local,dest="+dirDest),
withEnv("BUILDX_BAKE_FILE_RELATIVE_PATHS=1"),
)
require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "marker"))
})
4ce75f7 to
ad2e996
Compare
|
@tonistiigi Updated so file-relative mode is no longer based on the first Bake file. The parser now tracks the definition file that contributed each path. For HCL targets, it records the source block filename while decoding the target. For Compose files, it parses files separately in this mode so each generated target keeps the Compose file directory before normal Bake merging happens. The rebase now uses that metadata per field. An explicit
Also added coverage for the implicit default context case, for multi-file definitions using the defining file instead of the first file, and for the Compose subdirectory case. |
9e98fbe to
61f0bf5
Compare
|
I adjusted the Compose side in the last commit so it follows Compose project directory semantics instead of the HCL Bake definition-file model. For HCL Bake files, file-relative mode still resolves each path from the Bake file that defines that path. For Compose files, the base is now the first Compose file directory, which better matches what users expect when comparing This also keeps Compose override files from accidentally changing the base directory for paths they add. I added a regression test that covers a second Compose file adding |
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
e48541b to
4a3ed37
Compare
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
4a3ed37 to
90119d8
Compare
tonistiigi
left a comment
There was a problem hiding this comment.
failing tests for the inherits case
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)
// the context path is defined in subdir/docker-bake.hcl, so the
// inherited value must resolve relative to that file
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)
// the context path is defined in one/docker-bake.hcl, so the
// inherited value must resolve relative to that file, not to the
// file that defines the inheriting target
require.Equal(t, filepath.ToSlash(filepath.Clean("one/basectx")), *m["app"].Context)
})
}
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
fixes #1028
fixes #197
This adds an opt-in Bake mode that resolves local directory paths in
target.contextandtarget.contextsrelative to the Bake or Compose definition that owns those paths whenBUILDX_BAKE_FILE_RELATIVE_PATHS=1is set.Bake now accepts a parser option that records the definition file for local build context paths and rebases them after HCL or Compose files are parsed. The
buildx bakecommand enables this option fromBUILDX_BAKE_FILE_RELATIVE_PATHS, whilecwd://remains available for paths that should stay relative to the invocation directory.The opt-in also applies to targets that omit
context, so the implicit default.is resolved from the target definition file instead of the invocation directory.Compose files follow Compose project semantics for this opt-in, so local build contexts from Compose input are resolved from the directory of the first local Compose file rather than from override file locations.