Skip to main content

A lint running tool and framework.

Project description

Lintrunner

Overview

lintrunner is a tool that runs linters. It is responsible for:

  • Deciding which files need to be linted.
  • Invoking linters according to a common protocol.
  • Gathering results and presenting them to users.

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.

Installation

Currently, do:

cargo install lintrunner --force

In the future we’ll probably do binary releases through GitHub, pip, or conda.

Usage

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!

How lintrunner decides what paths to lint

When run with no arguments, lintrunner will check:

  • The files changed in the HEAD commit.
  • The files changed in the user’s working tree.

It does not check:

  • Any files not tracked by git; git add them to lint them.

There are multiple ways to customize how paths are checked:

Pass paths as positional arguments

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:

  • All paths changed from <tree-ish> to HEAD
  • All paths changed in the user's working tree.

Linter configuration

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.

Linter protocol

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/ .

Invocation

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.

Output

Any lint messages a linter would like to communicate the user must be represented as a LintMessage. The linter, must print LintMessages 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.

Exiting

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 lintrunnerand 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lintrunner-0.5.5.tar.gz (35.0 kB view details)

Uploaded Source

Built Distributions

lintrunner-0.5.5-py3-none-win_amd64.whl (1.1 MB view details)

Uploaded Python 3 Windows x86-64

lintrunner-0.5.5-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (2.1 MB view details)

Uploaded Python 3 manylinux: glibc 2.12+ x86-64

lintrunner-0.5.5-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (2.4 MB view details)

Uploaded Python 3 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

File details

Details for the file lintrunner-0.5.5.tar.gz.

File metadata

  • Download URL: lintrunner-0.5.5.tar.gz
  • Upload date:
  • Size: 35.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/0.12.12

File hashes

Hashes for lintrunner-0.5.5.tar.gz
Algorithm Hash digest
SHA256 cd48f056de2c86d94348963a20f4a3c9a22162a45510cf772e9c9ef69caefbc8
MD5 9aff16f8863072b4d47859ce8b06465a
BLAKE2b-256 36f84419bc34e40091c9d490600668db63c4c651f9d5f196a50b426dace33a08

See more details on using hashes here.

Provenance

File details

Details for the file lintrunner-0.5.5-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for lintrunner-0.5.5-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 db2095293dc5e99ff7c0cafc3e4496cf2ecd245cdad0efd26af8b77550cdf406
MD5 6b6d48cabd4389b32f2af6de4fc7e5b8
BLAKE2b-256 e86ce6ad49c515996b8a285948b9e4aafe24227fdf39b50aae94d0b6b45cfcd1

See more details on using hashes here.

Provenance

File details

Details for the file lintrunner-0.5.5-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for lintrunner-0.5.5-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 34c045e4562b0df1e6e6347e3eae9f0eb0519e805dfe1eb5b253c1c57e5d961c
MD5 a39de92fee9a5f746083796a846a6eef
BLAKE2b-256 7ead4d7289f29f16788b56f75f94b92c6c703f5a9a39df099f24dee99db6d6e5

See more details on using hashes here.

Provenance

File details

Details for the file lintrunner-0.5.5-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for lintrunner-0.5.5-py3-none-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 f4a014c4c82709ab2b73a32f7da395d6613e992fb3db1291e5b0630045411a59
MD5 96b89730ecfa671c985031b5a4b0d468
BLAKE2b-256 cc8f9ab342f98d65031f4cfbaf9131889e09342e1e27153b9f5b0b3730cfbf12

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page