From 927fb6731c2c53da85627a5c9acd93d5439fdb5e Mon Sep 17 00:00:00 2001 From: Talon Bowler Date: Wed, 12 Jun 2024 10:31:22 -0700 Subject: [PATCH 1/2] update the lint subrequest call to error when a build error was encountered during linting Signed-off-by: Talon Bowler --- commands/build.go | 23 ++++++++++++++++++++++- tests/build.go | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/commands/build.go b/commands/build.go index 9cf34404..09f500f7 100644 --- a/commands/build.go +++ b/commands/build.go @@ -866,7 +866,28 @@ func printResult(f *controllerapi.PrintFunc, res map[string]string) error { case "subrequests.describe": return printValue(subrequests.PrintDescribe, subrequests.SubrequestsDescribeDefinition.Version, f.Format, res) case "lint": - return printValue(lint.PrintLintViolations, lint.SubrequestLintDefinition.Version, f.Format, res) + err := printValue(lint.PrintLintViolations, lint.SubrequestLintDefinition.Version, f.Format, res) + if err != nil { + return err + } + + lintResults := lint.LintResults{} + if result, ok := res["result.json"]; ok { + if err := json.Unmarshal([]byte(result), &lintResults); err != nil { + return err + } + } + if lintResults.Error != nil { + fmt.Println() + lintBuf := bytes.NewBuffer([]byte(lintResults.Error.Message + "\n")) + sourceInfo := lintResults.Sources[lintResults.Error.Location.SourceIndex] + source := errdefs.Source{ + Info: sourceInfo, + Ranges: lintResults.Error.Location.Ranges, + } + source.Print(lintBuf) + return errors.New(lintBuf.String()) + } default: if dt, ok := res["result.json"]; ok && f.Format == "json" { fmt.Println(dt) diff --git a/tests/build.go b/tests/build.go index 3e1ccb0a..c79b2228 100644 --- a/tests/build.go +++ b/tests/build.go @@ -817,7 +817,7 @@ COPy --from=base \ stderr := bytes.Buffer{} cmd.Stdout = &stdout cmd.Stderr = &stderr - require.NoError(t, cmd.Run(), stdout.String(), stderr.String()) + require.Error(t, cmd.Run(), stdout.String(), stderr.String()) var res lint.LintResults require.NoError(t, json.Unmarshal(stdout.Bytes(), &res)) From 366328ba6ad1f930fbef1d2a52a274e0644960a4 Mon Sep 17 00:00:00 2001 From: Talon Bowler Date: Thu, 13 Jun 2024 16:11:35 -0700 Subject: [PATCH 2/2] Add comment to document the purpose behind the non-standard handling of the error Signed-off-by: Talon Bowler --- commands/build.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/commands/build.go b/commands/build.go index 09f500f7..9f85fa2f 100644 --- a/commands/build.go +++ b/commands/build.go @@ -878,6 +878,12 @@ func printResult(f *controllerapi.PrintFunc, res map[string]string) error { } } if lintResults.Error != nil { + // Print the error message and the source + // Normally, we would use `errdefs.WithSource` to attach the source to the + // error and let the error be printed by the handling that's already in place, + // but here we want to print the error in a way that's consistent with how + // the lint warnings are printed via the `lint.PrintLintViolations` function, + // which differs from the default error printing. fmt.Println() lintBuf := bytes.NewBuffer([]byte(lintResults.Error.Message + "\n")) sourceInfo := lintResults.Sources[lintResults.Error.Location.SourceIndex]