From fb773fa805188c80cdf8e54ac4fae6961fc1e564 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 16 Jul 2024 10:39:21 +0200 Subject: [PATCH 1/3] bake: check printer before printing warnings Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> (cherry picked from commit 7823a2dc0112caeb1d82ba523a89b9aa96abedc7) --- commands/bake.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/commands/bake.go b/commands/bake.go index 2834494e646..a7d73ed9f9d 100644 --- a/commands/bake.go +++ b/commands/bake.go @@ -146,7 +146,9 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba printer, err = progress.NewPrinter(ctx2, os.Stderr, progressMode, progress.WithDesc(progressTextDesc, progressConsoleDesc), progress.WithOnClose(func() { - printWarnings(os.Stderr, printer.Warnings(), progressMode) + if p := printer; p != nil { + printWarnings(os.Stderr, p.Warnings(), progressMode) + } }), ) if err != nil { From 9d8cf0bed3d84bbd4210b43bea45be211a5f2baf Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:10:00 +0200 Subject: [PATCH 2/3] test: bake print Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> (cherry picked from commit bd0b425734408559b09a15e50a438f29a8ffd9b8) --- tests/bake.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ tests/integration.go | 10 ++++++++++ 2 files changed, 56 insertions(+) diff --git a/tests/bake.go b/tests/bake.go index 47f85eacb2b..364b697dd2e 100644 --- a/tests/bake.go +++ b/tests/bake.go @@ -1,6 +1,7 @@ package tests import ( + "bytes" "encoding/json" "fmt" "os" @@ -9,6 +10,7 @@ import ( "testing" "github.com/containerd/continuity/fs/fstest" + "github.com/docker/buildx/bake" "github.com/docker/buildx/util/gitutil" "github.com/moby/buildkit/client" "github.com/moby/buildkit/identity" @@ -28,6 +30,7 @@ func bakeCmd(sb integration.Sandbox, opts ...cmdOpt) (string, error) { } var bakeTests = []func(t *testing.T, sb integration.Sandbox){ + testBakePrint, testBakeLocal, testBakeLocalMulti, testBakeRemote, @@ -55,6 +58,49 @@ var bakeTests = []func(t *testing.T, sb integration.Sandbox){ testBakeCallCheckFlag, } +func testBakePrint(t *testing.T, sb integration.Sandbox) { + dockerfile := []byte(` +FROM busybox +ARG HELLO +RUN echo "Hello ${HELLO}" + `) + bakefile := []byte(` +target "build" { + args = { + HELLO = "foo" + } +} +`) + dir := tmpdir( + t, + fstest.CreateFile("docker-bake.hcl", bakefile, 0600), + fstest.CreateFile("Dockerfile", dockerfile, 0600), + ) + + cmd := buildxCmd(sb, withDir(dir), withArgs("bake", "--print", "build")) + stdout := bytes.Buffer{} + stderr := bytes.Buffer{} + cmd.Stdout = &stdout + cmd.Stderr = &stderr + require.NoError(t, cmd.Run(), stdout.String(), stderr.String()) + + var def struct { + Group map[string]*bake.Group `json:"group,omitempty"` + Target map[string]*bake.Target `json:"target"` + } + require.NoError(t, json.Unmarshal(stdout.Bytes(), &def)) + + require.Len(t, def.Group, 1) + require.Contains(t, def.Group, "default") + + require.Equal(t, []string{"build"}, def.Group["default"].Targets) + require.Len(t, def.Target, 1) + require.Contains(t, def.Target, "build") + require.Equal(t, ".", *def.Target["build"].Context) + require.Equal(t, "Dockerfile", *def.Target["build"].Dockerfile) + require.Equal(t, map[string]*string{"HELLO": ptrstr("foo")}, def.Target["build"].Args) +} + func testBakeLocal(t *testing.T, sb integration.Sandbox) { dockerfile := []byte(` FROM scratch diff --git a/tests/integration.go b/tests/integration.go index 920d1dce110..2a4e6a622c4 100644 --- a/tests/integration.go +++ b/tests/integration.go @@ -4,6 +4,7 @@ import ( "os" "os/exec" "path/filepath" + "reflect" "strconv" "strings" "sync" @@ -208,3 +209,12 @@ func skipNoCompatBuildKit(t *testing.T, sb integration.Sandbox, constraint strin t.Skipf("buildkit version %s does not match %s constraint (%s)", buildkitVersion(t, sb), constraint, msg) } } + +func ptrstr(s interface{}) *string { + var n *string + if reflect.ValueOf(s).Kind() == reflect.String { + ss := s.(string) + n = &ss + } + return n +} From dd3bb69a1ea8c627f3b586fea7ac68de1a618a97 Mon Sep 17 00:00:00 2001 From: Talon Bowler Date: Thu, 18 Jul 2024 08:36:34 -0700 Subject: [PATCH 3/3] clarify the appropriate place to use the debug flag when viewing warnings Signed-off-by: Talon Bowler (cherry picked from commit cedbc5d68d0a05ca5462c44ff035da1e94c1149d) --- commands/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/build.go b/commands/build.go index 12d45a08f7d..7359a19477e 100644 --- a/commands/build.go +++ b/commands/build.go @@ -835,7 +835,7 @@ func printWarnings(w io.Writer, warnings []client.VertexWarning, mode progressui fmt.Fprintf(sb, "%d warnings found", len(warnings)) } if logrus.GetLevel() < logrus.DebugLevel { - fmt.Fprintf(sb, " (use --debug to expand)") + fmt.Fprintf(sb, " (use docker --debug to expand)") } fmt.Fprintf(sb, ":\n") fmt.Fprint(w, aec.Apply(sb.String(), aec.YellowF))