Skip to content

buildifier: add SARIF output (--format=sarif) - #1529

Merged
fmeum merged 6 commits into
bazel-contrib:mainfrom
LittleHuba:buildifier-sarif
Oct 28, 2026
Merged

fmeum merged 6 commits into
bazel-contrib:mainfrom
LittleHuba:buildifier-sarif

Conversation

@LittleHuba

Copy link
Copy Markdown
Contributor

Adds SARIF 2.1.0 as a third diagnostics format next to text and json. SARIF is understood by many CI, IDE and code-quality tools. Fixes #1528.

Usage

buildifier --mode=check --lint=warn --format=sarif -r . > buildifier.sarif

As with json, --format=sarif only works with --mode=check, and -v indents the output.

Mapping

  • Each lint warning becomes a result at level warning. Its ruleId is the warning category, helpUri is the WARNINGS.md link, and the region is the start and end line and column.
  • A file that needs reformatting becomes a file-level result (no region) with rule format, level warning.
  • A file that can't be parsed becomes a file-level result with rule parse-error, level error.
  • Rules are deduplicated per category.
  • With no findings, the output is still a valid log with empty rules and results.

Design notes

  • The serializer is a small in-tree struct set, so it adds no dependency.
  • Findings carry no severity today (only actionable, which warn.go always sets to true). So every lint finding is a warning.
  • autoFixable is not mapped. It would need SARIF fixes with replacement text, which can come in a follow-up.
  • A format finding has no region, because buildifier doesn't track where the formatting differs.
  • artifactLocation.uri is the path as passed on the command line, so run from the repo root with relative paths for repo-relative URIs.

Commits

  1. Serializer (utils/sarif.go), unit tests, and the Format hook-up.
  2. Accept --format=sarif in --mode=check and update the help text.
  3. README section.
  4. Integration tests with goldens, covering many files, a single file, stdin and an invalid file.

Additional testing

  • Sample outputs validate against the official OASIS SARIF 2.1.0 schema.

@LittleHuba
LittleHuba marked this pull request as ready for review October 27, 2026 10:59

@fmeum fmeum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is really useful!

Just a nit and I will merge and release this.

Comment thread buildifier/utils/sarif.go Resolved
LittleHuba and others added 4 commits October 27, 2026 13:11
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@fmeum fmeum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review: two small SARIF 2.1.0 conformance gaps and two test nits. The suggestions can be applied as one batch. With all of them applied, go test ./buildifier/utils passes and gofmt is clean. The integration goldens still need the columnKind line, as described in the first comment.

Comment thread buildifier/utils/sarif.go Resolved
Comment thread buildifier/utils/sarif.go Resolved
Comment thread buildifier/utils/sarif.go Resolved
Comment thread buildifier/utils/sarif.go Resolved
Comment thread buildifier/integration_test.sh Resolved
Comment thread buildifier/utils/sarif_test.go Resolved
@fmeum

fmeum commented Oct 27, 2026

Copy link
Copy Markdown
Member

I also asked Claude for a review and it found a few things worth fixing regarding sarif compat.

LittleHuba and others added 2 commits October 28, 2026 11:33
- Set run.columnKind, which is required when results are present
  (SARIF 3.14.27).
- Escape '{' and '}' in message text (3.11.5).
- Emit artifact locations as RFC 3986 URI references: percent-encode
  special characters and use file:// URIs for absolute paths (3.4.3).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Pipe an unformatted file in the stdin integration test so the stdin
  filename appears in the report.
- Add the license header to sarif_test.go.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@LittleHuba

Copy link
Copy Markdown
Contributor Author

Resolved all findings. Found in addition one more hole with escaping curly braces.

@fmeum
fmeum enabled auto-merge (squash) October 28, 2026 10:49
@fmeum
fmeum merged commit 82285db into bazel-contrib:main Oct 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[buildifier] Support for SARIF format

2 participants