From a91db7ccc99399758e1cf95463cb9ab78595fdf6 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 8 Apr 2025 17:06:45 +0200 Subject: [PATCH] bake: keep escaped interpolation in print output Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- bake/bake.go | 36 ++++++++++++++++++++++++++++++ tests/bake.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/bake/bake.go b/bake/bake.go index df9c559e..3eb41158 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -3,6 +3,7 @@ package bake import ( "context" "encoding" + "encoding/json" "io" "maps" "os" @@ -733,6 +734,41 @@ type Target struct { linked bool } +func (t *Target) MarshalJSON() ([]byte, error) { + tgt := *t + esc := func(s string) string { + return strings.ReplaceAll(strings.ReplaceAll(s, "${", "$${"), "%{", "%%{") + } + + tgt.Annotations = slices.Clone(t.Annotations) + for i, v := range tgt.Annotations { + tgt.Annotations[i] = esc(v) + } + + if tgt.DockerfileInline != nil { + escaped := esc(*tgt.DockerfileInline) + tgt.DockerfileInline = &escaped + } + + tgt.Labels = maps.Clone(t.Labels) + for k, v := range t.Labels { + if v != nil { + escaped := esc(*v) + tgt.Labels[k] = &escaped + } + } + + tgt.Args = maps.Clone(t.Args) + for k, v := range t.Args { + if v != nil { + escaped := esc(*v) + tgt.Args[k] = &escaped + } + } + + return json.Marshal(tgt) +} + var ( _ hclparser.WithEvalContexts = &Target{} _ hclparser.WithGetName = &Target{} diff --git a/tests/bake.go b/tests/bake.go index a7ee8a73..7065726a 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -40,6 +40,7 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ testBakePrint, testBakePrintSensitive, testBakePrintOverrideEmpty, + testBakePrintKeepEscaped, testBakeLocal, testBakeLocalMulti, testBakeRemote, @@ -329,6 +330,66 @@ target "default" { }`, stdout.String()) } +func testBakePrintKeepEscaped(t *testing.T, sb integration.Sandbox) { + bakefile := []byte(` +target "default" { + dockerfile-inline = <