Skip to content

Commit 6019a2b

Browse files
jsternbergcrazy-max
authored andcommitted
buildflags: skip empty cache entries when parsing
Broken in 11c8497. The section to skip an empty input was accidentally removed when some code was refactored to fix a separate issue. This skips empty cache entries which allows disabling the `cache-from` and `cache-to` entries from the command line overrides. Signed-off-by: Jonathan A. Sternberg <[email protected]>
1 parent 6da88e1 commit 6019a2b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

tests/bake.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func bakeCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) {
3838
var bakeTests = []func(t *testing.T, sb integration.Sandbox){
3939
testBakePrint,
4040
testBakePrintSensitive,
41+
testBakePrintOverrideEmpty,
4142
testBakeLocal,
4243
testBakeLocalMulti,
4344
testBakeRemote,
@@ -286,6 +287,47 @@ RUN echo "Hello ${HELLO}"
286287
}
287288
}
288289

290+
func testBakePrintOverrideEmpty(t *testing.T, sb integration.Sandbox) {
291+
dockerfile := []byte(`
292+
FROM scratch
293+
COPY foo /foo
294+
`)
295+
bakefile := []byte(`
296+
target "default" {
297+
cache-to = ["type=gha,mode=min,scope=integration-tests"]
298+
}
299+
`)
300+
dir := tmpdir(
301+
t,
302+
fstest.CreateFile("docker-bake.hcl", bakefile, 0600),
303+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
304+
fstest.CreateFile("foo", []byte("foo"), 0600),
305+
)
306+
307+
cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--print", "--set", "*.cache-to="))
308+
stdout := bytes.Buffer{}
309+
stderr := bytes.Buffer{}
310+
cmd.Stdout = &stdout
311+
cmd.Stderr = &stderr
312+
require.NoError(t, cmd.Run(), stdout.String(), stderr.String())
313+
314+
require.JSONEq(t, `{
315+
"group": {
316+
"default": {
317+
"targets": [
318+
"default"
319+
]
320+
}
321+
},
322+
"target": {
323+
"default": {
324+
"context": ".",
325+
"dockerfile": "Dockerfile"
326+
}
327+
}
328+
}`, stdout.String())
329+
}
330+
289331
func testBakeLocal(t *testing.T, sb integration.Sandbox) {
290332
dockerfile := []byte(`
291333
FROM scratch
@@ -871,6 +913,7 @@ target "default" {
871913
})
872914
}
873915
}
916+
874917
func testBakeSetNonExistingOutsideNoParallel(t *testing.T, sb integration.Sandbox) {
875918
for _, ent := range []bool{true, false} {
876919
t.Run(fmt.Sprintf("ent=%v", ent), func(t *testing.T) {

util/buildflags/cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ func ParseCacheEntry(in []string) (CacheOptions, error) {
175175

176176
opts := make(CacheOptions, 0, len(in))
177177
for _, in := range in {
178+
if in == "" {
179+
continue
180+
}
181+
178182
if !strings.Contains(in, "=") {
179183
// This is ref only format. Each field in the CSV is its own entry.
180184
fields, err := csvvalue.Fields(in, nil)

0 commit comments

Comments
 (0)