Skip to main content
Version: 0.18.x

ruff

API for declaring a Ruff lint aspect that visits py_library rules.

Typical usage:

load("@aspect_rules_lint//lint:ruff.bzl", "ruff_aspect")

ruff = ruff_aspect(
binary = "@@//:ruff",
configs = "@@//:.ruff.toml",
)

Rules

ruff_workaround_20269

Workaround for https://github.com/bazelbuild/bazel/issues/20269

Example usage (generated):

load("@aspect_rules_lint//lint:ruff.bzl", "ruff_workaround_20269")

ruff_workaround_20269(
# A unique name for this repository.
name = "",
# A dictionary from local repository name to global repository name
repo_mapping = {},
)

name

Required name.

A unique name for this repository.

build_file_content

Optional string. Default: ""

repo_mapping

Required dictionary: String → String.

A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

sha256

Optional string. Default: ""

url

Optional string. Default: ""

Macros and Functions

fetch_ruff

A repository macro used from WORKSPACE to fetch ruff binaries

Example usage (generated):

load("@aspect_rules_lint//lint:ruff.bzl", "fetch_ruff")

fetch_ruff(
)

tag

Optional. Default: "v0.3.5"

a tag of ruff that we have mirrored, e.g. v0.1.0

lint_ruff_aspect

A factory function to create a linter aspect.

Attrs: binary: a ruff executable. Can be obtained like so:

    load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "ruff_bin_linux_amd64",
sha256 = "<-sha->",
urls = [
"https://github.com/charliermarsh/ruff/releases/download/v<-version->/ruff-x86_64-unknown-linux-gnu.tar.gz",
],
build_file_content = """exports_files(["ruff"])""",
)

configs: ruff config file(s) (`pyproject.toml`, `ruff.toml`, or `.ruff.toml`)

Example usage (generated):

load("@aspect_rules_lint//lint:ruff.bzl", "lint_ruff_aspect")

lint_ruff_aspect(
binary = None,
configs = None,
)

binary

Required.

configs

Required.

ruff_action

Run ruff as an action under Bazel.

Ruff will select the configuration file to use for each source file, as documented here: https://docs.astral.sh/ruff/configuration/#config-file-discovery

Note: all config files are passed to the action. This means that a change to any config file invalidates the action cache entries for ALL ruff actions.

However this is needed because:

  1. ruff has an extend field, so it may need to read more than one config file
  2. ruff's logic for selecting the appropriate config needs to read the file content to detect a [tool.ruff] section.

Example usage (generated):

load("@aspect_rules_lint//lint:ruff.bzl", "ruff_action")

ruff_action(
# Bazel Rule or Aspect evaluation context
ctx = None,
# label of the the ruff program
executable = None,
# python files to be linted
srcs = [],
# labels of ruff config files (pyproject.toml, ruff.toml, or .ruff.toml)
config = None,
# output file to generate
report = None,
)

ctx

Required.

Bazel Rule or Aspect evaluation context

executable

Required.

label of the the ruff program

srcs

Required.

python files to be linted

config

Required.

labels of ruff config files (pyproject.toml, ruff.toml, or .ruff.toml)

report

Required.

output file to generate

use_exit_code

Optional. Default: False

whether to fail the build when a lint violation is reported

ruff_fix

Create a Bazel Action that spawns ruff with --fix.

Example usage (generated):

load("@aspect_rules_lint//lint:ruff.bzl", "ruff_fix")

ruff_fix(
# an action context OR aspect context
ctx = None,
# struct with _ruff and _patcher field
executable = None,
# list of file objects to lint
srcs = [],
# labels of ruff config files (pyproject.toml, ruff.toml, or .ruff.toml)
config = None,
# output file containing the applied fixes that can be applied with the patch(1) command.
patch = None,
)

ctx

Required.

an action context OR aspect context

executable

Required.

struct with _ruff and _patcher field

srcs

Required.

list of file objects to lint

config

Required.

labels of ruff config files (pyproject.toml, ruff.toml, or .ruff.toml)

patch

Required.

output file containing the applied fixes that can be applied with the patch(1) command.