From 15b99a680483a804c86dcfdc2f23e31f1fa65328 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Fri, 13 Oct 2023 15:45:25 -0700 Subject: [PATCH] feat: print nicer output from plugin --- BUILD.bazel | 1 + MODULE.bazel | 4 ++++ README.md | 12 ++++++------ go.mod | 2 +- plugin.go | 20 ++++++++++++++------ 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/BUILD.bazel b/BUILD.bazel index ad141395..b685cc14 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -30,6 +30,7 @@ go_library( "@build_aspect_cli//pkg/ioutils", "@build_aspect_cli//pkg/plugin/sdk/v1alpha4/config", "@build_aspect_cli//pkg/plugin/sdk/v1alpha4/plugin", + "@com_github_fatih_color//:color", "@com_github_hashicorp_go_plugin//:go-plugin", "@in_gopkg_yaml_v2//:yaml_v2", ], diff --git a/MODULE.bazel b/MODULE.bazel index 56e595cb..7a796cc6 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -7,6 +7,7 @@ module( ) bazel_dep(name = "aspect_bazel_lib", version = "1.36.0") + # Needed in the root because we use js_lib_helpers in our aspect impl bazel_dep(name = "aspect_rules_js", version = "1.32.6") bazel_dep(name = "bazel_skylib", version = "1.4.1") @@ -14,8 +15,10 @@ bazel_dep(name = "platforms", version = "0.0.7") # Needed in the root because we dereference ProtoInfo in our aspect impl bazel_dep(name = "rules_proto", version = "5.3.0-21.7") + # Needed in the root because we dereference the toolchain in our aspect impl bazel_dep(name = "rules_buf", version = "0.1.1") + # Needed due to rules_proto leaking the dependency bazel_dep(name = "protobuf", version = "21.7", repo_name = "com_google_protobuf") @@ -34,6 +37,7 @@ go_deps.gazelle_override( use_repo( go_deps, "build_aspect_cli", + "com_github_fatih_color", "com_github_hashicorp_go_plugin", "in_gopkg_yaml_v2", ) diff --git a/README.md b/README.md index 915c7798..29a1bb80 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,7 @@ Features: - **No changes needed to rulesets**. Works with the Bazel rules you already use. - **No changes needed to BUILD files**. You don't need to add lint wrapper macros, and lint doesn't appear in `bazel query` output. Instead, users can lint their existing `*_library` targets. -- Lint results can be **presented in various ways**, see below - -See it in action: - -[![asciicast](https://asciinema.org/a/prVnsvrEN3Vpvm5Wf7QVq7Ytt.svg)](https://asciinema.org/a/prVnsvrEN3Vpvm5Wf7QVq7Ytt?t=41) +- Lint results can be **presented in various ways**, see "Usage" below. This project is inspired by the design for [Tricorder]. This is how Googlers get their static analysis results in code review (Critique). @@ -27,7 +23,7 @@ We have a separate project for formatting, see 0 { + // We are a child process of the Aspect CLI + // but we should be using the plugin SDK to ask the CLI to print for us + color.NoColor = false + relpath, _ := filepath.Rel(bazelBin, f) + color.New(color.FgYellow).Fprintf(streams.Stdout, "From %s:\n", relpath) fmt.Fprintln(streams.Stdout, lineResult) + fmt.Fprintln(streams.Stdout, "") } } @@ -121,7 +129,7 @@ func (plugin *LintPlugin) CustomCommands() ([]*aspectplugin.Command, error) { }, nil } -func (plugin *LintPlugin) findLintResultFiles(streams ioutils.Streams, bazelStartupArgs []string) ([]string, error) { +func (plugin *LintPlugin) findLintResultFiles(streams ioutils.Streams, bazelStartupArgs []string) ([]string, string, error) { // TODO: use the Build Event Stream to learn of report files as actions write them var infoOutBuf bytes.Buffer @@ -134,7 +142,7 @@ func (plugin *LintPlugin) findLintResultFiles(streams ioutils.Streams, bazelStar infoCmd := bazelStartupArgs infoCmd = append(infoCmd, "info", "bazel-bin") if exitCode, err := bazel.WorkspaceFromWd.RunCommand(infoStreams, nil, infoCmd...); exitCode != 0 { - return nil, err + return nil, "", err } binDir := strings.TrimSpace(infoOutBuf.String()) @@ -148,13 +156,13 @@ func (plugin *LintPlugin) findLintResultFiles(streams ioutils.Streams, bazelStar findCmd.Stdin = streams.Stdin if err := findCmd.Run(); err != nil { - return nil, err + return nil, "", err } lintFiles := strings.TrimSpace(findOutBuf.String()) if len(lintFiles) == 0 { - return []string{}, nil + return []string{}, "", nil } - return strings.Split(lintFiles, "\n"), nil + return strings.Split(lintFiles, "\n"), binDir, nil }