Skip to content

dev/sg: rework lint package to allow FixableLinter and -fix

Warren Gifford requested to merge fixable-linters into main

Created by: bobheadxi

Refactors the lint package to now export two main ways to build linters:

  • Linter, which implements a Check(...)
  • FixableLinter, which also implements a Fix(...)

Using this, sg lint now:

  • on normal sg lint, runs checks and suggests that users run sg lint -fix if any of the failed linters implement FixableLinter
  • on sg lint -fix, runs all checks, but if a linter implements FixableLinter, also runs that

To enable this:

  • repo.State now parses the full diff up-front, so that linters do not interfere with each other
  • the go generate check now resets the workspace in Check
  • fix an issue with sg lint [target] -h to allow us to surface hints that a target has fixable linters

For back-compat, we have:

  • lint.ScriptCheck, which is more or less the same as the old lint.Script
  • lint.FuncCheck, which can wrap the old lint.Runner interface

To demonstrate the new API and features, this PR:

  • Creates a new go generate linter implementation that implements FixableLinter
  • Migrates some linters that have configuration (logging library linter, go mod version linter) to implement the Linter interface directly, because the struct func approach gives a better home for configuration

Closes https://github.com/sourcegraph/sourcegraph/issues/35396

Test plan

go run ./dev/sg lint with a generate issue will suggest that it is fixable, and leave no diff in the workspace:

image

go run ./dv/sg lint go -h indicates this target has linters that are fixable:

image

With -fix:

image

Merge request reports

Loading