Skip to content

bake: add file-relative path opt-in#3935

Open
crazy-max wants to merge 2 commits into
docker:masterfrom
crazy-max:bake-file-relative-paths
Open

bake: add file-relative path opt-in#3935
crazy-max wants to merge 2 commits into
docker:masterfrom
crazy-max:bake-file-relative-paths

Conversation

@crazy-max

@crazy-max crazy-max commented Jul 6, 2026

Copy link
Copy Markdown
Member

fixes #1028
fixes #197

This adds an opt-in Bake mode that resolves local directory paths in target.context and target.contexts relative to the Bake or Compose file that defines each path when BUILDX_BAKE_FILE_RELATIVE_PATHS=1 is 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 bake command enables this option from BUILDX_BAKE_FILE_RELATIVE_PATHS, while cwd:// 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.

@crazy-max

Copy link
Copy Markdown
Member Author

@crazy-max crazy-max force-pushed the bake-file-relative-paths branch from fc54769 to 4ce75f7 Compare July 6, 2026 12:16
@crazy-max crazy-max requested a review from tonistiigi July 6, 2026 12:24
@crazy-max crazy-max marked this pull request as ready for review July 6, 2026 12:33

@tonistiigi tonistiigi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"))
	})

(typically `;` on Windows and `:` elsewhere), but can be changed with `BUILDX_BAKE_PATH_SEPARATOR`.

By default, local directory build contexts in Bake files are resolved from the
current working directory. To opt in to resolving local directory build contexts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the "first" bake file? Rather than "current" bake file. Even if this is more complicated, I think that should be the difference as the benefit of this method is that there isn't one global target directory.

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max

Copy link
Copy Markdown
Member Author

@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 context is rebased from the file that defined that context. Each named context in contexts is rebased from the file that defined that entry. If context is omitted, it now treats the implicit default . as coming from the target definition file, which fixes the default-context case you pointed out.

cwd:// remains the escape hatch for paths that should stay relative to the invocation directory, and special context forms such as target:, docker-image://, oci-layout://, remote URLs, and absolute paths are still left untouched.

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.

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max crazy-max force-pushed the bake-file-relative-paths branch from ad2e996 to 9e98fbe Compare July 7, 2026 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

buildx bake: Specifying --file in another directory leads to failure Relative paths in compose file not resolved correctly?

2 participants