Crates.io | lintrunner |
lib.rs | lintrunner |
version | 0.8.0 |
source | src |
created_at | 2021-10-22 20:20:08.00034 |
updated_at | 2022-05-06 19:33:13.143681 |
description | A lint running tool and framework. |
homepage | |
repository | |
max_upload_size | |
id | 469531 |
size | 154,060 |
lintrunner
is a tool that runs linters. It is responsible for:
The intention is to provide a universal way to configure and invoke linters, which is useful on large polyglot projects.
The design of lintrunner
is heavily inspired by linttool
, a project that exists internally at Meta.
pip install lintrunner
First, you need to add a configuration file to your repo. See the Linter configuration section for more info.
Then, simply run lintrunner
to lint your changes!
lintrunner
When run with no arguments, lintrunner
will check:
HEAD
commit.It does not check:
git
; git add
them to lint them.There are multiple ways to customize how paths are checked:
For example:
lintrunner foo.py bar.cpp
This naturally composes with xargs
, for example the canonical way to check
every path in the repo is:
git grep -Il . | xargs lintrunner
--paths-cmd
Some ways to invoke xargs
will cause multiple lintrunner
processes to be
run, increasing lint time (especially on huge path sets). As an alternative that
gives lintrunner
control of parallelization, you can use --paths-cmd
. If
--paths-cmd
is specified lintrunner
will execute that command and consider
each line of its stdout
to be a file to lint.
For example, the same command above would be:
lintrunner --paths-cmd='git grep -Il .'
--paths-file
If this is specified, lintrunner
will read paths from the given file, one per
line, and check those. This can be useful if you have some really complex logic
to determine which paths to check.
--revision
This value can be any <tree-ish>
accepted by git diff-tree
, like a commit
hash or revspec. If this is specified, lintrunner
will check:
<tree-ish>
to HEAD
--merge-base-with
Like --revision
, except the revision is determined by computing the merge-base
of HEAD
and the provided <tree-ish>
. This is useful for linting all commits
in a specific pull request. For example, for a pull request targeting master,
you can run:
lintrunner -m master
lintrunner
knows which linters to run and how by looking at a configuration
file, conventionally named .lintrunner.toml
.
Here is an example linter configuration:
[[linter]]
name = 'FLAKE8'
include_patterns = [
'src/**/*.py', # unix-style globs supported
'test/**/*.py',
]
exclude_patterns = ['src/my_bad_file.py']
command = [
'python3',
'flake8_linter.py',
'—-',
# {{PATHSFILE}} gets rewritten to a tmpfile containing all paths to lint
'@{{PATHSFILE}}',
]
A complete description of the configuration schema can be found here.
Most linters have their own output format and arguments. In order to impose
consistency on linter invocation and outputs, lintrunner
implements a protocol
that it expects linters to fulfill. In most cases, a small script (called a
linter adapter) is required to implement the protocol for a given external
linter. You can see some example adapters in examples/
.
Linters will be invoked according to the command
specified by their
configuration. They will be called once per lint run.
If a linter needs to know which paths to run on, it should take a
{{PATHSFILE}}
argument. During invocation, the string {{PATHSFILE}}
will be
replaced with the name of a temporary file containing which paths the linter
should run on, one path per line.
A common way to implement this in a linter adapter is to use argparse
’s
fromfile_prefix_chars
feature. In the Flake8 example above, we use @
as the fromfile_prefix_chars
argument, so argparse
will automatically read the {{PATHSFILE}}
and supply
its contents as a list of arguments.
Any lint messages a linter would like to communicate the user must be
represented as a LintMessage
. The linter, must print LintMessage
s as JSON
Lines to stdout
, one message per line. Output to
stderr
will be ignored.
A complete description of the LintMessage schema can be found here.
Linters should always exit with code 0. This is true even if lint errors are
reported; lintrunner
itself will determine how to exit based on what linters
report.
To signal a general linter failure (which should ideally never happen!), linters
can return a LintMessage
with path = None
.
In the event a linter exits non-zero, it will be caught by lintrunner
and
presented as a “general linter failure” with stdout/stderr shown to the user.
This should be considered a bug in the linter’s implementation of this protocol.