Skip to content

Commit 6111e72

Browse files
committed
add sourceInfoMap callback when printing warnings for with command.
Signed-off-by: Talon Bowler <[email protected]>
1 parent c01c148 commit 6111e72

File tree

2 files changed

+77
-37
lines changed

2 files changed

+77
-37
lines changed

commands/bake.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,17 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
332332
res = sp.ExporterResponse
333333
}
334334

335+
printArgs := &printResultArgs{
336+
callFunc: pf,
337+
res: res,
338+
contextPath: req.Inputs.ContextPath,
339+
dockerfileName: req.Inputs.DockerfilePath,
340+
}
335341
if callFormatJSON {
336342
jsonResults[name] = map[string]any{}
337343
buf := &bytes.Buffer{}
338-
if code, err := printResult(buf, pf, res); err != nil {
344+
printArgs.writer = buf
345+
if code, err := printResult(printArgs); err != nil {
339346
jsonResults[name]["error"] = err.Error()
340347
exitCode = 1
341348
} else if code != 0 && exitCode == 0 {
@@ -355,13 +362,15 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
355362
} else {
356363
sep = true
357364
}
365+
358366
fmt.Fprintf(dockerCli.Out(), "%s\n", name)
359367
if descr := tgts[name].Description; descr != "" {
360368
fmt.Fprintf(dockerCli.Out(), "%s\n", descr)
361369
}
362370

363371
fmt.Fprintln(dockerCli.Out())
364-
if code, err := printResult(dockerCli.Out(), pf, res); err != nil {
372+
printArgs.writer = dockerCli.Out()
373+
if code, err := printResult(printArgs); err != nil {
365374
fmt.Fprintf(dockerCli.Out(), "error: %v\n", err)
366375
exitCode = 1
367376
} else if code != 0 && exitCode == 0 {

commands/build.go

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import (
4949
"github.com/moby/buildkit/frontend/subrequests/outline"
5050
"github.com/moby/buildkit/frontend/subrequests/targets"
5151
"github.com/moby/buildkit/solver/errdefs"
52+
solverpb "github.com/moby/buildkit/solver/pb"
5253
"github.com/moby/buildkit/util/grpcerrors"
5354
"github.com/moby/buildkit/util/progress/progressui"
5455
"github.com/morikuni/aec"
@@ -387,7 +388,14 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
387388
}
388389
}
389390
if opts.CallFunc != nil {
390-
if exitcode, err := printResult(dockerCli.Out(), opts.CallFunc, resp.ExporterResponse); err != nil {
391+
printArgs := &printResultArgs{
392+
writer: os.Stdout,
393+
callFunc: opts.CallFunc,
394+
res: resp.ExporterResponse,
395+
contextPath: options.contextPath,
396+
dockerfileName: options.dockerfileName,
397+
}
398+
if exitcode, err := printResult(printArgs); err != nil {
391399
return err
392400
} else if exitcode != 0 {
393401
os.Exit(exitcode)
@@ -882,38 +890,66 @@ func printWarnings(w io.Writer, warnings []client.VertexWarning, mode progressui
882890
}
883891
}
884892

885-
func printResult(w io.Writer, f *controllerapi.CallFunc, res map[string]string) (int, error) {
886-
switch f.Name {
893+
type printResultArgs struct {
894+
writer io.Writer
895+
callFunc *controllerapi.CallFunc
896+
res map[string]string
897+
contextPath string
898+
dockerfileName string
899+
}
900+
901+
func printResult(printArgs *printResultArgs) (int, error) {
902+
switch printArgs.callFunc.Name {
887903
case "outline":
888-
return 0, printValue(w, outline.PrintOutline, outline.SubrequestsOutlineDefinition.Version, f.Format, res)
904+
return 0, printValue(printArgs, outline.PrintOutline, outline.SubrequestsOutlineDefinition.Version)
889905
case "targets":
890-
return 0, printValue(w, targets.PrintTargets, targets.SubrequestsTargetsDefinition.Version, f.Format, res)
906+
return 0, printValue(printArgs, targets.PrintTargets, targets.SubrequestsTargetsDefinition.Version)
891907
case "subrequests.describe":
892-
return 0, printValue(w, subrequests.PrintDescribe, subrequests.SubrequestsDescribeDefinition.Version, f.Format, res)
908+
return 0, printValue(printArgs, subrequests.PrintDescribe, subrequests.SubrequestsDescribeDefinition.Version)
893909
case "lint":
894910
lintResults := lint.LintResults{}
895-
if result, ok := res["result.json"]; ok {
911+
if result, ok := printArgs.res["result.json"]; ok {
896912
if err := json.Unmarshal([]byte(result), &lintResults); err != nil {
897913
return 0, err
898914
}
899915
}
900916

901917
warningCount := len(lintResults.Warnings)
902-
if f.Format != "json" && warningCount > 0 {
918+
if printArgs.callFunc.Format != "json" && warningCount > 0 {
903919
var warningCountMsg string
904920
if warningCount == 1 {
905921
warningCountMsg = "1 warning has been found!"
906922
} else if warningCount > 1 {
907923
warningCountMsg = fmt.Sprintf("%d warnings have been found!", warningCount)
908924
}
909-
fmt.Fprintf(w, "Check complete, %s\n", warningCountMsg)
925+
fmt.Fprintf(printArgs.writer, "Check complete, %s\n", warningCountMsg)
926+
}
927+
928+
sourceInfoMap := func(sourceInfo *solverpb.SourceInfo) *solverpb.SourceInfo {
929+
newSourceInfo := &solverpb.SourceInfo{}
930+
*newSourceInfo = *sourceInfo
931+
932+
contextPath := "."
933+
if printArgs.contextPath != "" {
934+
contextPath = printArgs.contextPath
935+
}
936+
937+
dockerfileName := printArgs.dockerfileName
938+
if dockerfileName == "" {
939+
dockerfileName = filepath.Join(contextPath, "Dockerfile")
940+
}
941+
942+
if strings.HasSuffix(dockerfileName, sourceInfo.Filename) {
943+
newSourceInfo.Filename = dockerfileName
944+
}
945+
return newSourceInfo
910946
}
911947

912948
lintPrintFunc := func(b []byte, w io.Writer) error {
913-
return lintResults.PrintTo(w, nil)
949+
return lintResults.PrintTo(w, sourceInfoMap)
914950
}
915951

916-
err := printValue(w, lintPrintFunc, lint.SubrequestLintDefinition.Version, f.Format, res)
952+
err := printValue(printArgs, lintPrintFunc, lint.SubrequestLintDefinition.Version)
917953
if err != nil {
918954
return 0, err
919955
}
@@ -923,32 +959,27 @@ func printResult(w io.Writer, f *controllerapi.CallFunc, res map[string]string)
923959
// Normally, we would use `errdefs.WithSource` to attach the source to the
924960
// error and let the error be printed by the handling that's already in place,
925961
// but here we want to print the error in a way that's consistent with how
926-
// the lint warnings are printed via the `lint.PrintLintViolations` function,
962+
// the lint warnings are printed via the `lintResults.PrintTo` function,
927963
// which differs from the default error printing.
928-
if f.Format != "json" && len(lintResults.Warnings) > 0 {
929-
fmt.Fprintln(w)
930-
}
931-
lintBuf := bytes.NewBuffer([]byte(lintResults.Error.Message + "\n"))
932-
sourceInfo := lintResults.Sources[lintResults.Error.Location.SourceIndex]
933-
source := errdefs.Source{
934-
Info: sourceInfo,
935-
Ranges: lintResults.Error.Location.Ranges,
964+
if printArgs.callFunc.Format != "json" && len(lintResults.Warnings) > 0 {
965+
fmt.Fprintln(printArgs.writer)
936966
}
937-
source.Print(lintBuf)
967+
lintBuf := bytes.NewBuffer(nil)
968+
lintResults.PrintErrorTo(lintBuf)
938969
return 0, errors.New(lintBuf.String())
939-
} else if len(lintResults.Warnings) == 0 && f.Format != "json" {
940-
fmt.Fprintln(w, "Check complete, no warnings found.")
970+
} else if len(lintResults.Warnings) == 0 && printArgs.callFunc.Format != "json" {
971+
fmt.Fprintln(printArgs.writer, "Check complete, no warnings found.")
941972
}
942973
default:
943-
if dt, ok := res["result.json"]; ok && f.Format == "json" {
944-
fmt.Fprintln(w, dt)
945-
} else if dt, ok := res["result.txt"]; ok {
946-
fmt.Fprint(w, dt)
974+
if dt, ok := printArgs.res["result.json"]; ok && printArgs.callFunc.Format == "json" {
975+
fmt.Fprintln(printArgs.writer, dt)
976+
} else if dt, ok := printArgs.res["result.txt"]; ok {
977+
fmt.Fprint(printArgs.writer, dt)
947978
} else {
948-
fmt.Fprintf(w, "%s %+v\n", f, res)
979+
fmt.Fprintf(printArgs.writer, "%s %+v\n", printArgs.callFunc, printArgs.res)
949980
}
950981
}
951-
if v, ok := res["result.statuscode"]; !f.IgnoreStatus && ok {
982+
if v, ok := printArgs.res["result.statuscode"]; !printArgs.callFunc.IgnoreStatus && ok {
952983
if n, err := strconv.Atoi(v); err == nil && n != 0 {
953984
return n, nil
954985
}
@@ -958,18 +989,18 @@ func printResult(w io.Writer, f *controllerapi.CallFunc, res map[string]string)
958989

959990
type callFunc func([]byte, io.Writer) error
960991

961-
func printValue(w io.Writer, printer callFunc, version string, format string, res map[string]string) error {
962-
if format == "json" {
963-
fmt.Fprintln(w, res["result.json"])
992+
func printValue(printArgs *printResultArgs, printer callFunc, version string) error {
993+
if printArgs.callFunc.Format == "json" {
994+
fmt.Fprintln(printArgs.writer, printArgs.res["result.json"])
964995
return nil
965996
}
966997

967-
if res["version"] != "" && versions.LessThan(version, res["version"]) && res["result.txt"] != "" {
998+
if printArgs.res["version"] != "" && versions.LessThan(version, printArgs.res["version"]) && printArgs.res["result.txt"] != "" {
968999
// structure is too new and we don't know how to print it
969-
fmt.Fprint(w, res["result.txt"])
1000+
fmt.Fprint(printArgs.writer, printArgs.res["result.txt"])
9701001
return nil
9711002
}
972-
return printer([]byte(res["result.json"]), w)
1003+
return printer([]byte(printArgs.res["result.json"]), printArgs.writer)
9731004
}
9741005

9751006
type invokeConfig struct {

0 commit comments

Comments
 (0)