Skip to content

Commit

Permalink
feat: print nicer output from plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Oct 13, 2023
1 parent 22d6d12 commit 15b99a6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ 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")
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")

Expand All @@ -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",
)
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -27,7 +23,7 @@ We have a separate project for formatting, see <https://github.com/aspect-build/
[tricorder]: https://static.googleusercontent.com/media/research.google.com/en//pubs/archive/43322.pdf
[reviewdog]: https://github.com/reviewdog/reviewdog

## Ways to present results
## Usage

### 1. Warnings in the terminal with `bazel lint`

Expand All @@ -37,6 +33,8 @@ Users just type `bazel lint //path/to:targets`.

Reports are then written to the terminal.

[![asciicast](https://asciinema.org/a/xQWU1Wc1JINOubeguDDQbBqcq.svg)](https://asciinema.org/a/xQWU1Wc1JINOubeguDDQbBqcq)

### 2. Warnings in the terminal with a wrapper

You can use vanilla Bazel rather than Aspect CLI.
Expand All @@ -45,6 +43,8 @@ Placing a couple commands in a shell script, Makefile, or similar wrapper.

See the `example/lint.sh` file as an example.

[![asciicast](https://asciinema.org/a/gUUuQTCGIu85YMl6zz2GJIgD8.svg)](https://asciinema.org/a/gUUuQTCGIu85YMl6zz2GJIgD8)

### 3. Errors during `bazel build`

By adding `--aspects_parameters=fail_on_violation=true` to the command-line, we pass a parameter
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
aspect.build/cli v1.507.2
github.com/fatih/color v1.15.0
github.com/hashicorp/go-plugin v1.4.10
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -12,7 +13,6 @@ require (
github.com/bazelbuild/bazelisk v1.17.0 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/chzyer/readline v1.5.1 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
Expand Down
20 changes: 14 additions & 6 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/fatih/color"
goplugin "github.com/hashicorp/go-plugin"
"gopkg.in/yaml.v2"

Expand Down Expand Up @@ -81,7 +83,7 @@ func (plugin *LintPlugin) CustomCommands() ([]*aspectplugin.Command, error) {
}

// Find the lint result files.
lintFiles, err := plugin.findLintResultFiles(streams, bazelStartupArgs)
lintFiles, bazelBin, err := plugin.findLintResultFiles(streams, bazelStartupArgs)
if err != nil {
Log.Printf("Error collecting lint results: %v\n", err)

Expand Down Expand Up @@ -111,7 +113,13 @@ func (plugin *LintPlugin) CustomCommands() ([]*aspectplugin.Command, error) {

lineResult := strings.TrimSpace(string(lintResultBuf))
if len(lineResult) > 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, "")
}
}

Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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
}

0 comments on commit 15b99a6

Please sign in to comment.