Skip to content

Commit

Permalink
fix(ruff): honor exclusions when linting (#483)
Browse files Browse the repository at this point in the history
* fix(ruff): honor exclusions when linting

Without this flag, it will lint files that are excluded by the config file since we pass explicit flags

* chore: repro problem with ruff excludes
  • Loading branch information
alexeagle authored Feb 14, 2025
1 parent 38c0c15 commit 8a8cc86
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions example/.ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude = ['test/excluded.py']
10 changes: 10 additions & 0 deletions example/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ py_library(
srcs = ["generated.py"],
)

py_library(
name = "excluded_py",
srcs = ["excluded.py"],
)

java_library(
name = "generated_java",
srcs = ["generated.java"],
Expand All @@ -73,6 +78,11 @@ ruff_test(
srcs = [":generated_py"],
)

ruff_test(
name = "ruff_should_ignore_excluded",
srcs = ["excluded_py"],
)

pmd_test(
name = "pmd_should_ignore_generated",
srcs = [":generated_java"],
Expand Down
1 change: 1 addition & 0 deletions example/test/excluded.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import os
4 changes: 3 additions & 1 deletion lint/ruff.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def ruff_action(ctx, executable, srcs, config, stdout, exit_code = None, env = {
# `ruff help check` to see available options
args = ctx.actions.args()
args.add("check")
# Honor exclusions in pyproject.toml even though we pass explicit list of files
args.add("--force-exclude")
args.add_all(srcs)

if exit_code:
Expand Down Expand Up @@ -130,7 +132,7 @@ def ruff_fix(ctx, executable, srcs, config, patch, stdout, exit_code, env = {}):
output = patch_cfg,
content = json.encode({
"linter": executable._ruff.path,
"args": ["check", "--fix"] + [s.path for s in srcs],
"args": ["check", "--fix", "--force-exclude"] + [s.path for s in srcs],
"files_to_diff": [s.path for s in srcs],
"output": patch.path,
}),
Expand Down

0 comments on commit 8a8cc86

Please sign in to comment.