Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"maps"
"net/url"
"os"
"path"
"path/filepath"
Expand All @@ -29,6 +30,7 @@ import (
hcl "github.com/hashicorp/hcl/v2"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
"github.com/pkg/errors"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
Expand Down Expand Up @@ -1299,15 +1301,18 @@ func updateContext(t *build.Inputs, inp *Input) {
for k, v := range t.NamedContexts {
if v.Path == "." {
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
continue
}
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
continue
}
if urlutil.IsRemoteURL(v.Path) {
continue
}
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
t.NamedContexts[k] = build.NamedContext{State: &st, Path: inp.URL}
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/", &llb.CopyInfo{
CopyDirContentsOnly: true,
}), llb.WithCustomNamef("set context %s to %s", k, v.Path))
t.NamedContexts[k] = build.NamedContext{State: &st, Path: remoteURLWithSubdir(inp.URL, v.Path)}
}

if t.ContextPath == "." {
Expand All @@ -1327,7 +1332,44 @@ func updateContext(t *build.Inputs, inp *Input) {
llb.WithCustomNamef("set context to %s", t.ContextPath),
)
t.ContextState = &st
t.ContextPath = inp.URL
t.ContextPath = remoteURLWithSubdir(inp.URL, t.ContextPath)
}

func remoteURLWithSubdir(remoteURL, subdir string) string {
subdir = path.Clean(subdir)
if subdir == "." || remoteURL == "" {
return remoteURL
}

// only relevant for git urls
parsed, ok, err := dfgitutil.ParseGitRef(remoteURL)
if err != nil || !ok {
return remoteURL
}
if parsed.SubDir != "" {
subdir = path.Clean(path.Join(parsed.SubDir, subdir))
}

// keep query string and transport style untouched nad only append/adjust
// fragment subdir
if !strings.Contains(remoteURL, "#") && subdir != "" {
if u, err := url.Parse(remoteURL); err == nil {
q := u.Query()
if q.Has("subdir") {
q.Set("subdir", subdir)
u.RawQuery = q.Encode()
return u.String()
}
}
}

// otherwise, we adjust the fragment part to add/replace subdir
base, frag, _ := strings.Cut(remoteURL, "#")
ref, _, _ := strings.Cut(frag, ":")
if ref == "" {
return base + "#:" + subdir
}
return base + "#" + ref + ":" + subdir
}

func collectLocalPaths(t build.Inputs) []string {
Expand Down
46 changes: 46 additions & 0 deletions bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2415,6 +2415,52 @@ target "mtx" {
}
}

func TestRemoteURLWithSubdir(t *testing.T) {
tests := []struct {
name string
remote string
subdir string
want string
}{
{
name: "git no ref",
remote: "https://github.com/docker/buildx.git",
subdir: "components/interface",
want: "https://github.com/docker/buildx.git#:components/interface",
},
{
name: "git with ref",
remote: "https://github.com/docker/buildx.git#main",
subdir: "components/interface",
want: "https://github.com/docker/buildx.git#main:components/interface",
},
{
name: "git with existing subdir",
remote: "https://github.com/docker/buildx.git#main:base",
subdir: "components/interface",
want: "https://github.com/docker/buildx.git#main:base/components/interface",
},
{
name: "git query ref",
remote: "https://github.com/docker/buildx.git?branch=main",
subdir: "components/interface",
want: "https://github.com/docker/buildx.git?branch=main#:components/interface",
},
{
name: "non git",
remote: "https://example.com/context.tar.gz",
subdir: "components/interface",
want: "https://example.com/context.tar.gz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := remoteURLWithSubdir(tt.remote, tt.subdir)
assert.Equal(t, tt.want, got)
})
}
}

func stringify[V fmt.Stringer](values []V) []string {
s := make([]string, len(values))
for i, v := range values {
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ require (
github.com/docker/docker v28.5.2+incompatible
github.com/docker/go-units v0.5.0
github.com/gofrs/flock v0.13.0
github.com/golang/snappy v0.0.4
github.com/golang/snappy v1.0.0
github.com/google/go-dap v0.12.1-0.20250904181021-d7a2259b058b
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/google/uuid v1.6.0
github.com/hashicorp/go-cty-funcs v0.0.0-20250818135842-6aab67130928
github.com/hashicorp/hcl/v2 v2.24.0
github.com/in-toto/in-toto-golang v0.10.0
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/moby/buildkit v0.28.0-rc2
github.com/moby/buildkit v0.28.0
github.com/moby/go-archive v0.2.0
github.com/moby/moby/api v1.53.0
github.com/moby/moby/client v0.2.2
Expand Down Expand Up @@ -101,7 +101,7 @@ require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudflare/circl v1.6.1 // indirect
github.com/cloudflare/circl v1.6.3 // indirect
github.com/containerd/containerd/api v1.10.0 // indirect
github.com/containerd/errdefs/pkg v0.3.0 // indirect
github.com/containerd/ttrpc v1.2.7 // indirect
Expand Down Expand Up @@ -227,7 +227,7 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
go.opentelemetry.io/proto/otlp v1.7.1 // indirect
go.yaml.in/yaml/v2 v2.4.3 // indirect
golang.org/x/net v0.50.0 // indirect
golang.org/x/net v0.51.0 // indirect
golang.org/x/oauth2 v0.34.0 // indirect
golang.org/x/time v0.14.0 // indirect
golang.org/x/tools v0.41.0 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1x
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
github.com/compose-spec/compose-go/v2 v2.9.1 h1:8UwI+ujNU+9Ffkf/YgAm/qM9/eU7Jn8nHzWG721W4rs=
Expand Down Expand Up @@ -291,8 +291,8 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8J
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/certificate-transparency-go v1.3.2 h1:9ahSNZF2o7SYMaKaXhAumVEzXB2QaayzII9C8rv7v+A=
github.com/google/certificate-transparency-go v1.3.2/go.mod h1:H5FpMUaGa5Ab2+KCYsxg6sELw3Flkl7pGZzWdBoYLXs=
github.com/google/flatbuffers v25.2.10+incompatible h1:F3vclr7C3HpB1k9mxCGRMXq6FdUalZ6H/pNX4FP1v0Q=
Expand Down Expand Up @@ -422,8 +422,8 @@ github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/buildkit v0.28.0-rc2 h1:Or6AOgXhcrbw6sdUU0W3qzAqPrq+AXd5PS+TPTvBvjw=
github.com/moby/buildkit v0.28.0-rc2/go.mod h1:OvWR1wd21skG+T2wKP3J3dZO+C+oEbIXoyWQTl4dX2A=
github.com/moby/buildkit v0.28.0 h1:rKulfRRSduHJPNpLTk481fHElqN9tps0VUx8YV/5zsA=
github.com/moby/buildkit v0.28.0/go.mod h1:RCuOcj/bVsCriBG8NeFzRxjiCFQKnKP7KOVlNTS18t4=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
Expand Down Expand Up @@ -688,8 +688,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60=
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM=
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw=
golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down
4 changes: 2 additions & 2 deletions hack/Vagrantfile.openbsd
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

Vagrant.configure("2") do |config|
config.vm.box = "pygolo/openbsd7"
config.vm.box_version = "7.5"
config.vm.box_version = "7.7"
config.vm.boot_timeout = 900
config.vm.synced_folder ".", "/vagrant", type: "rsync"
config.ssh.keep_alive = true

config.vm.provision "init", type: "shell", run: "once" do |sh|
sh.inline = <<~SHELL
set -ex
export PKG_PATH=https://mirrors.ocf.berkeley.edu/pub/OpenBSD/7.5/packages/amd64/
export PKG_PATH=https://cdn.openbsd.org/pub/OpenBSD/$(uname -r)/packages/$(machine -a)/
pkg_add -x git

ftp https://go.dev/dl/go#{ENV['GO_VERSION']}.openbsd-amd64.tar.gz
Expand Down
80 changes: 78 additions & 2 deletions tests/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
testBakeLocalCwdOverride,
testBakeRemoteCmdContextOverride,
testBakeRemoteContextSubdir,
testBakeRemoteNamedContextSubdir,
testBakeRemoteNamedContextDot,
testBakeRemoteCmdContextEscapeRoot,
testBakeRemoteCmdContextEscapeRelative,
testBakeRemoteDockerfileCwd,
Expand Down Expand Up @@ -563,12 +565,12 @@ COPY super-cool.txt /
}{
{
name: "no ref",
expectedContext: addr,
expectedContext: addr + "#:bar",
},
{
name: "branch ref",
ref: "main",
expectedContext: addr + "#main",
expectedContext: addr + "#main:bar",
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -920,6 +922,80 @@ COPY super-cool.txt /
require.FileExists(t, filepath.Join(dirDest, "super-cool.txt"))
}

// https://github.com/docker/buildx/issues/3670
func testBakeRemoteNamedContextSubdir(t *testing.T, sb integration.Sandbox) {
bakefile := []byte(`
target default {
context = "./build"
dockerfile = "Dockerfile"
contexts = {
files = "./files-src/"
}
}
`)
dockerfile := []byte(`
FROM scratch
COPY --from=files file.txt /file.txt
`)

dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateDir("build", 0700),
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
fstest.CreateDir("files-src", 0700),
fstest.CreateFile("files-src/file.txt", []byte("hello"), 0600),
)
dirDest := t.TempDir()

git, err := gitutil.New(gitutil.WithWorkingDir(dir))
require.NoError(t, err)
gittestutil.GitInit(git, t)
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "files-src")
gittestutil.GitCommit(git, t, "initial commit")
addr := gittestutil.GitServeHTTP(git, t)

out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "file.txt"))
}

func testBakeRemoteNamedContextDot(t *testing.T, sb integration.Sandbox) {
bakefile := []byte(`
target default {
context = "./build"
dockerfile = "Dockerfile"
contexts = {
files = "."
}
}
`)
dockerfile := []byte(`
FROM scratch
COPY --from=files marker.txt /marker.txt
`)

dir := tmpdir(
t,
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
fstest.CreateDir("build", 0700),
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
fstest.CreateFile("marker.txt", []byte("hello"), 0600),
)
dirDest := t.TempDir()

git, err := gitutil.New(gitutil.WithWorkingDir(dir))
require.NoError(t, err)
gittestutil.GitInit(git, t)
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "marker.txt")
gittestutil.GitCommit(git, t, "initial commit")
addr := gittestutil.GitServeHTTP(git, t)

out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
require.NoError(t, err, out)
require.FileExists(t, filepath.Join(dirDest, "marker.txt"))
}

func testBakeRemoteCmdContextEscapeRoot(t *testing.T, sb integration.Sandbox) {
dirSrc := tmpdir(
t,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/cloudflare/circl/sign/sign.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion vendor/github.com/golang/snappy/README

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading