Skip to content

Commit a953c9b

Browse files
authored
Merge pull request #3689 from tonistiigi/v0.32-cherry-picks
[v0.32] cherry picks
2 parents 22074cd + 0dceb4b commit a953c9b

13 files changed

Lines changed: 208 additions & 33 deletions

File tree

bake/bake.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"maps"
10+
"net/url"
1011
"os"
1112
"path"
1213
"path/filepath"
@@ -29,6 +30,7 @@ import (
2930
hcl "github.com/hashicorp/hcl/v2"
3031
"github.com/moby/buildkit/client"
3132
"github.com/moby/buildkit/client/llb"
33+
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
3234
"github.com/pkg/errors"
3335
"github.com/zclconf/go-cty/cty"
3436
"github.com/zclconf/go-cty/cty/convert"
@@ -1299,15 +1301,18 @@ func updateContext(t *build.Inputs, inp *Input) {
12991301
for k, v := range t.NamedContexts {
13001302
if v.Path == "." {
13011303
t.NamedContexts[k] = build.NamedContext{Path: inp.URL}
1304+
continue
13021305
}
13031306
if strings.HasPrefix(v.Path, "cwd://") || strings.HasPrefix(v.Path, "target:") || strings.HasPrefix(v.Path, "docker-image:") {
13041307
continue
13051308
}
13061309
if urlutil.IsRemoteURL(v.Path) {
13071310
continue
13081311
}
1309-
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/"), llb.WithCustomNamef("set context %s to %s", k, v.Path))
1310-
t.NamedContexts[k] = build.NamedContext{State: &st, Path: inp.URL}
1312+
st := llb.Scratch().File(llb.Copy(*inp.State, v.Path, "/", &llb.CopyInfo{
1313+
CopyDirContentsOnly: true,
1314+
}), llb.WithCustomNamef("set context %s to %s", k, v.Path))
1315+
t.NamedContexts[k] = build.NamedContext{State: &st, Path: remoteURLWithSubdir(inp.URL, v.Path)}
13111316
}
13121317

13131318
if t.ContextPath == "." {
@@ -1327,7 +1332,44 @@ func updateContext(t *build.Inputs, inp *Input) {
13271332
llb.WithCustomNamef("set context to %s", t.ContextPath),
13281333
)
13291334
t.ContextState = &st
1330-
t.ContextPath = inp.URL
1335+
t.ContextPath = remoteURLWithSubdir(inp.URL, t.ContextPath)
1336+
}
1337+
1338+
func remoteURLWithSubdir(remoteURL, subdir string) string {
1339+
subdir = path.Clean(subdir)
1340+
if subdir == "." || remoteURL == "" {
1341+
return remoteURL
1342+
}
1343+
1344+
// only relevant for git urls
1345+
parsed, ok, err := dfgitutil.ParseGitRef(remoteURL)
1346+
if err != nil || !ok {
1347+
return remoteURL
1348+
}
1349+
if parsed.SubDir != "" {
1350+
subdir = path.Clean(path.Join(parsed.SubDir, subdir))
1351+
}
1352+
1353+
// keep query string and transport style untouched nad only append/adjust
1354+
// fragment subdir
1355+
if !strings.Contains(remoteURL, "#") && subdir != "" {
1356+
if u, err := url.Parse(remoteURL); err == nil {
1357+
q := u.Query()
1358+
if q.Has("subdir") {
1359+
q.Set("subdir", subdir)
1360+
u.RawQuery = q.Encode()
1361+
return u.String()
1362+
}
1363+
}
1364+
}
1365+
1366+
// otherwise, we adjust the fragment part to add/replace subdir
1367+
base, frag, _ := strings.Cut(remoteURL, "#")
1368+
ref, _, _ := strings.Cut(frag, ":")
1369+
if ref == "" {
1370+
return base + "#:" + subdir
1371+
}
1372+
return base + "#" + ref + ":" + subdir
13311373
}
13321374

13331375
func collectLocalPaths(t build.Inputs) []string {

bake/bake_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,52 @@ target "mtx" {
24152415
}
24162416
}
24172417

2418+
func TestRemoteURLWithSubdir(t *testing.T) {
2419+
tests := []struct {
2420+
name string
2421+
remote string
2422+
subdir string
2423+
want string
2424+
}{
2425+
{
2426+
name: "git no ref",
2427+
remote: "https://github.com/docker/buildx.git",
2428+
subdir: "components/interface",
2429+
want: "https://github.com/docker/buildx.git#:components/interface",
2430+
},
2431+
{
2432+
name: "git with ref",
2433+
remote: "https://github.com/docker/buildx.git#main",
2434+
subdir: "components/interface",
2435+
want: "https://github.com/docker/buildx.git#main:components/interface",
2436+
},
2437+
{
2438+
name: "git with existing subdir",
2439+
remote: "https://github.com/docker/buildx.git#main:base",
2440+
subdir: "components/interface",
2441+
want: "https://github.com/docker/buildx.git#main:base/components/interface",
2442+
},
2443+
{
2444+
name: "git query ref",
2445+
remote: "https://github.com/docker/buildx.git?branch=main",
2446+
subdir: "components/interface",
2447+
want: "https://github.com/docker/buildx.git?branch=main#:components/interface",
2448+
},
2449+
{
2450+
name: "non git",
2451+
remote: "https://example.com/context.tar.gz",
2452+
subdir: "components/interface",
2453+
want: "https://example.com/context.tar.gz",
2454+
},
2455+
}
2456+
for _, tt := range tests {
2457+
t.Run(tt.name, func(t *testing.T) {
2458+
got := remoteURLWithSubdir(tt.remote, tt.subdir)
2459+
assert.Equal(t, tt.want, got)
2460+
})
2461+
}
2462+
}
2463+
24182464
func stringify[V fmt.Stringer](values []V) []string {
24192465
s := make([]string, len(values))
24202466
for i, v := range values {

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ require (
2121
github.com/docker/docker v28.5.2+incompatible
2222
github.com/docker/go-units v0.5.0
2323
github.com/gofrs/flock v0.13.0
24-
github.com/golang/snappy v0.0.4
24+
github.com/golang/snappy v1.0.0
2525
github.com/google/go-dap v0.12.1-0.20250904181021-d7a2259b058b
2626
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
2727
github.com/google/uuid v1.6.0
2828
github.com/hashicorp/go-cty-funcs v0.0.0-20250818135842-6aab67130928
2929
github.com/hashicorp/hcl/v2 v2.24.0
3030
github.com/in-toto/in-toto-golang v0.10.0
3131
github.com/mitchellh/hashstructure/v2 v2.0.2
32-
github.com/moby/buildkit v0.28.0-rc2
32+
github.com/moby/buildkit v0.28.0
3333
github.com/moby/go-archive v0.2.0
3434
github.com/moby/moby/api v1.53.0
3535
github.com/moby/moby/client v0.2.2
@@ -101,7 +101,7 @@ require (
101101
github.com/blang/semver v3.5.1+incompatible // indirect
102102
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
103103
github.com/cespare/xxhash/v2 v2.3.0 // indirect
104-
github.com/cloudflare/circl v1.6.1 // indirect
104+
github.com/cloudflare/circl v1.6.3 // indirect
105105
github.com/containerd/containerd/api v1.10.0 // indirect
106106
github.com/containerd/errdefs/pkg v0.3.0 // indirect
107107
github.com/containerd/ttrpc v1.2.7 // indirect
@@ -227,7 +227,7 @@ require (
227227
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
228228
go.opentelemetry.io/proto/otlp v1.7.1 // indirect
229229
go.yaml.in/yaml/v2 v2.4.3 // indirect
230-
golang.org/x/net v0.50.0 // indirect
230+
golang.org/x/net v0.51.0 // indirect
231231
golang.org/x/oauth2 v0.34.0 // indirect
232232
golang.org/x/time v0.14.0 // indirect
233233
golang.org/x/tools v0.41.0 // indirect

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1x
106106
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
107107
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
108108
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
109-
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
110-
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
109+
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
110+
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
111111
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
112112
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
113113
github.com/compose-spec/compose-go/v2 v2.9.1 h1:8UwI+ujNU+9Ffkf/YgAm/qM9/eU7Jn8nHzWG721W4rs=
@@ -291,8 +291,8 @@ github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8J
291291
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
292292
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
293293
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
294-
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
295-
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
294+
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
295+
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
296296
github.com/google/certificate-transparency-go v1.3.2 h1:9ahSNZF2o7SYMaKaXhAumVEzXB2QaayzII9C8rv7v+A=
297297
github.com/google/certificate-transparency-go v1.3.2/go.mod h1:H5FpMUaGa5Ab2+KCYsxg6sELw3Flkl7pGZzWdBoYLXs=
298298
github.com/google/flatbuffers v25.2.10+incompatible h1:F3vclr7C3HpB1k9mxCGRMXq6FdUalZ6H/pNX4FP1v0Q=
@@ -422,8 +422,8 @@ github.com/mitchellh/hashstructure/v2 v2.0.2 h1:vGKWl0YJqUNxE8d+h8f6NJLcCJrgbhC4
422422
github.com/mitchellh/hashstructure/v2 v2.0.2/go.mod h1:MG3aRVU/N29oo/V/IhBX8GR/zz4kQkprJgF2EVszyDE=
423423
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
424424
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
425-
github.com/moby/buildkit v0.28.0-rc2 h1:Or6AOgXhcrbw6sdUU0W3qzAqPrq+AXd5PS+TPTvBvjw=
426-
github.com/moby/buildkit v0.28.0-rc2/go.mod h1:OvWR1wd21skG+T2wKP3J3dZO+C+oEbIXoyWQTl4dX2A=
425+
github.com/moby/buildkit v0.28.0 h1:rKulfRRSduHJPNpLTk481fHElqN9tps0VUx8YV/5zsA=
426+
github.com/moby/buildkit v0.28.0/go.mod h1:RCuOcj/bVsCriBG8NeFzRxjiCFQKnKP7KOVlNTS18t4=
427427
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
428428
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
429429
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
@@ -688,8 +688,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
688688
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
689689
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
690690
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
691-
golang.org/x/net v0.50.0 h1:ucWh9eiCGyDR3vtzso0WMQinm2Dnt8cFMuQa9K33J60=
692-
golang.org/x/net v0.50.0/go.mod h1:UgoSli3F/pBgdJBHCTc+tp3gmrU4XswgGRgtnwWTfyM=
691+
golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo=
692+
golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y=
693693
golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw=
694694
golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
695695
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

hack/Vagrantfile.openbsd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
Vagrant.configure("2") do |config|
55
config.vm.box = "pygolo/openbsd7"
6-
config.vm.box_version = "7.5"
6+
config.vm.box_version = "7.7"
77
config.vm.boot_timeout = 900
88
config.vm.synced_folder ".", "/vagrant", type: "rsync"
99
config.ssh.keep_alive = true
1010

1111
config.vm.provision "init", type: "shell", run: "once" do |sh|
1212
sh.inline = <<~SHELL
1313
set -ex
14-
export PKG_PATH=https://mirrors.ocf.berkeley.edu/pub/OpenBSD/7.5/packages/amd64/
14+
export PKG_PATH=https://cdn.openbsd.org/pub/OpenBSD/$(uname -r)/packages/$(machine -a)/
1515
pkg_add -x git
1616
1717
ftp https://go.dev/dl/go#{ENV['GO_VERSION']}.openbsd-amd64.tar.gz

tests/bake.go

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){
5151
testBakeLocalCwdOverride,
5252
testBakeRemoteCmdContextOverride,
5353
testBakeRemoteContextSubdir,
54+
testBakeRemoteNamedContextSubdir,
55+
testBakeRemoteNamedContextDot,
5456
testBakeRemoteCmdContextEscapeRoot,
5557
testBakeRemoteCmdContextEscapeRelative,
5658
testBakeRemoteDockerfileCwd,
@@ -563,12 +565,12 @@ COPY super-cool.txt /
563565
}{
564566
{
565567
name: "no ref",
566-
expectedContext: addr,
568+
expectedContext: addr + "#:bar",
567569
},
568570
{
569571
name: "branch ref",
570572
ref: "main",
571-
expectedContext: addr + "#main",
573+
expectedContext: addr + "#main:bar",
572574
},
573575
}
574576
for _, tt := range tests {
@@ -920,6 +922,80 @@ COPY super-cool.txt /
920922
require.FileExists(t, filepath.Join(dirDest, "super-cool.txt"))
921923
}
922924

925+
// https://github.com/docker/buildx/issues/3670
926+
func testBakeRemoteNamedContextSubdir(t *testing.T, sb integration.Sandbox) {
927+
bakefile := []byte(`
928+
target default {
929+
context = "./build"
930+
dockerfile = "Dockerfile"
931+
contexts = {
932+
files = "./files-src/"
933+
}
934+
}
935+
`)
936+
dockerfile := []byte(`
937+
FROM scratch
938+
COPY --from=files file.txt /file.txt
939+
`)
940+
941+
dir := tmpdir(
942+
t,
943+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
944+
fstest.CreateDir("build", 0700),
945+
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
946+
fstest.CreateDir("files-src", 0700),
947+
fstest.CreateFile("files-src/file.txt", []byte("hello"), 0600),
948+
)
949+
dirDest := t.TempDir()
950+
951+
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
952+
require.NoError(t, err)
953+
gittestutil.GitInit(git, t)
954+
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "files-src")
955+
gittestutil.GitCommit(git, t, "initial commit")
956+
addr := gittestutil.GitServeHTTP(git, t)
957+
958+
out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
959+
require.NoError(t, err, out)
960+
require.FileExists(t, filepath.Join(dirDest, "file.txt"))
961+
}
962+
963+
func testBakeRemoteNamedContextDot(t *testing.T, sb integration.Sandbox) {
964+
bakefile := []byte(`
965+
target default {
966+
context = "./build"
967+
dockerfile = "Dockerfile"
968+
contexts = {
969+
files = "."
970+
}
971+
}
972+
`)
973+
dockerfile := []byte(`
974+
FROM scratch
975+
COPY --from=files marker.txt /marker.txt
976+
`)
977+
978+
dir := tmpdir(
979+
t,
980+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
981+
fstest.CreateDir("build", 0700),
982+
fstest.CreateFile("build/Dockerfile", dockerfile, 0600),
983+
fstest.CreateFile("marker.txt", []byte("hello"), 0600),
984+
)
985+
dirDest := t.TempDir()
986+
987+
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
988+
require.NoError(t, err)
989+
gittestutil.GitInit(git, t)
990+
gittestutil.GitAdd(git, t, "docker-bake.hcl", "build", "marker.txt")
991+
gittestutil.GitCommit(git, t, "initial commit")
992+
addr := gittestutil.GitServeHTTP(git, t)
993+
994+
out, err := bakeCmd(sb, withDir("/tmp"), withArgs(addr, "--set", "*.output=type=local,dest="+dirDest))
995+
require.NoError(t, err, out)
996+
require.FileExists(t, filepath.Join(dirDest, "marker.txt"))
997+
}
998+
923999
func testBakeRemoteCmdContextEscapeRoot(t *testing.T, sb integration.Sandbox) {
9241000
dirSrc := tmpdir(
9251001
t,

vendor/github.com/cloudflare/circl/internal/sha3/xor_unaligned.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/cloudflare/circl/sign/sign.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/golang/snappy/README

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)