Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gzm0/rules_lint

 
 

Repository files navigation

Run linters under Bazel (EXPERIMENTAL)

It is currently EXPERIMENTAL and pre-release. No support is promised. There may be breaking changes, or we may archive and abandon the repository.

This ruleset integrates linting as a first-class concept under Bazel.

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.
  • Lints can be presented in various ways:
    • a hard failure, like it would with a lint_test rule that fails the build
    • as a warning, using whatever reporting method you prefer
    • or even as bot code review comments (e.g. with reviewdog)

How developers use it:

  1. (preferred) This ruleset provides an Aspect CLI plugin, so it can register the missing 'lint' command and users just type bazel lint //path/to:targets.
  2. Run with vanilla Bazel, by placing a couple commands in a shell script, Makefile, or similar wrapper.

See it in action:

asciicast

This project is inspired by the design for Tricorder. This is how Googlers get their static analysis results in code review (Critique). https://github.com/google/shipshape is an old, abandoned attempt to open-source Tricorder.

Note: we believe that Formatting is NOT Linting. We have a separate project for formatting, see https://github.com/aspect-build/bazel-super-formatter#design

Available linters

Adding a linter

Please follow the steps in lint/README.md and then send us a PR. Thanks!!

Installation

rules_lint currently only works with bzlmod under Bazel 6+. This is because we accumulate dependencies which are difficult to express in a WORKSPACE file. We might add support for WORKSPACE in the future.

Follow instructions from the release you wish to use: https://github.com/aspect-build/rules_lint/releases

Next, you must declare your linters as Bazel aspects.

This is needed because Bazel only allows aspect attributes of type bool, int or string. We want to follow the documentation from https://bazel.build/extending/aspects#aspect_definition_2: Aspects are also allowed to have private attributes of types label or label_list. Private label attributes can be used to specify dependencies on tools or libraries that are needed for actions generated by aspects.

We suggest creating a lint.bzl file in whatever package contains most of your custom Bazel configuration. This lint.bzl should contain linter aspect declarations. See the docs/ folder for "aspect factory functions" that declare your linters.

Finally, reference those linters as //path/to:lint.bzl%mylinter in the lint runner.

If you use the Aspect CLI, then include a block like the following in .aspect/cli/config.yaml:

plugins:
  - name: lint-plugin
    properties:
      lint_aspects:
        - //:lint.bzl%eslint
        - //:lint.bzl%buf

If you don't use Aspect CLI, you can put these in some other wrapper like a shell script that runs the linter aspects over the requested targets. See the lint.sh script in the example/ folder.

About

EXPERIMENTAL! Run static analysis tools with Bazel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Starlark 75.7%
  • Go 17.8%
  • Shell 4.4%
  • Other 2.1%