Skip to content

Commit

Permalink
fix: escape hatch from is_source filtering (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Jan 22, 2024
1 parent a1ade5b commit e8b525e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 12 deletions.
6 changes: 6 additions & 0 deletions docs/linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ Add a [make_lint_test](./lint_test.md) call to the `lint.bzl` file, then use the

See the `example/test/BUILD.bazel` file in this repo for some examples.

## Linting generated files

By default, we filter out generated files from linting.

To bypass this filter, add `tags=["lint-genfiles"]` to a target to force all the `srcs` to be linted.

## Debugging

Some linters honor the debug flag in this repo. To enable it, add a Bazel flag:
Expand Down
22 changes: 22 additions & 0 deletions example/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ write_file(
content = ["public class generated { protected void finalize(int a) {} }"],
)

write_file(
name = "shell_code_generator",
out = "generated.sh",
content = [
"#!/usr/bin/env bash",
"[ -z $THING ] && echo 'hello world'",
],
)

ts_project(
name = "no_violations",
srcs = [
Expand All @@ -46,6 +55,12 @@ java_library(
srcs = ["generated.java"],
)

sh_library(
name = "generated_sh",
srcs = ["generated.sh"],
tags = ["lint-genfiles"],
)

flake8_test(
name = "flake8_should_ignore_generated",
srcs = [":generated_py"],
Expand Down Expand Up @@ -92,3 +107,10 @@ shellcheck_test(
# Normally you'd fix the file instead of tagging this test.
tags = ["manual"],
)

shellcheck_test(
name = "shellcheck-generated",
srcs = [":generated_sh"],
# Expected to fail because the sh_library opted-in to having generated code checked as well
tags = ["manual"],
)
4 changes: 2 additions & 2 deletions lint/eslint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ See the [react example](https://github.com/bazelbuild/examples/blob/b498bb106b20

load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "COPY_FILE_TO_BIN_TOOLCHAINS", "copy_files_to_bin_actions")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("//lint/private:lint_aspect.bzl", "patch_and_report_files")
load("//lint/private:lint_aspect.bzl", "filter_srcs", "patch_and_report_files")

_MNEMONIC = "ESLint"

Expand Down Expand Up @@ -146,7 +146,7 @@ def _eslint_aspect_impl(target, ctx):
return []

patch, report, info = patch_and_report_files(_MNEMONIC, target, ctx)
files_to_lint = [s for s in ctx.rule.files.srcs if s.is_source]
files_to_lint = filter_srcs(ctx.rule)
eslint_action(ctx, ctx.executable, files_to_lint, report, ctx.attr.fail_on_violation)
eslint_fix(ctx, ctx.executable, files_to_lint, patch)
return [info]
Expand Down
4 changes: 2 additions & 2 deletions lint/flake8.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ flake8 = flake8_aspect(
```
"""

load("//lint/private:lint_aspect.bzl", "report_file")
load("//lint/private:lint_aspect.bzl", "filter_srcs", "report_file")

_MNEMONIC = "flake8"

Expand Down Expand Up @@ -64,7 +64,7 @@ def _flake8_aspect_impl(target, ctx):
return []

report, info = report_file(_MNEMONIC, target, ctx)
flake8_action(ctx, ctx.executable._flake8, [s for s in ctx.rule.files.srcs if s.is_source], ctx.file._config_file, report, ctx.attr.fail_on_violation)
flake8_action(ctx, ctx.executable._flake8, filter_srcs(ctx.rule), ctx.file._config_file, report, ctx.attr.fail_on_violation)
return [info]

def flake8_aspect(binary, config):
Expand Down
4 changes: 2 additions & 2 deletions lint/golangci-lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ golangci_lint = golangci_lint_aspect(

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@io_bazel_rules_go//go:def.bzl", "go_context")
load("//lint/private:lint_aspect.bzl", "report_file")
load("//lint/private:lint_aspect.bzl", "filter_srcs", "report_file")

_MNEMONIC = "golangcilint"

Expand Down Expand Up @@ -69,7 +69,7 @@ def _golangci_lint_aspect_impl(target, ctx):
return []

report, info = report_file(_MNEMONIC, target, ctx)
golangci_lint_action(ctx, ctx.executable._golangci_lint, ctx.rule.files.srcs, ctx.file._config_file, report, ctx.attr.fail_on_violation)
golangci_lint_action(ctx, ctx.executable._golangci_lint, filter_srcs(ctx.rule), ctx.file._config_file, report, ctx.attr.fail_on_violation)
return [info]

def golangci_lint_aspect(binary, config):
Expand Down
4 changes: 2 additions & 2 deletions lint/pmd.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pmd = pmd_aspect(
```
"""

load("//lint/private:lint_aspect.bzl", "report_file")
load("//lint/private:lint_aspect.bzl", "filter_srcs", "report_file")

_MNEMONIC = "PMD"

Expand Down Expand Up @@ -69,7 +69,7 @@ def _pmd_aspect_impl(target, ctx):
return []

report, info = report_file(_MNEMONIC, target, ctx)
pmd_action(ctx, ctx.executable._pmd, [s for s in ctx.rule.files.srcs if s.is_source], ctx.files._rulesets, report, ctx.attr.fail_on_violation)
pmd_action(ctx, ctx.executable._pmd, filter_srcs(ctx.rule), ctx.files._rulesets, report, ctx.attr.fail_on_violation)
return [info]

def pmd_aspect(binary, rulesets):
Expand Down
6 changes: 6 additions & 0 deletions lint/private/lint_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ def patch_and_report_files(*args):
rules_lint_report = depset([report]),
rules_lint_patch = depset([patch]),
)

def filter_srcs(rule):
if "lint-genfiles" in rule.attr.tags:
return rule.files.srcs
else:
return [s for s in rule.files.srcs if s.is_source]
4 changes: 2 additions & 2 deletions lint/ruff.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ruff = ruff_aspect(
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//lint/private:lint_aspect.bzl", "patch_and_report_files")
load("//lint/private:lint_aspect.bzl", "filter_srcs", "patch_and_report_files")
load(":ruff_versions.bzl", "RUFF_VERSIONS")

_MNEMONIC = "ruff"
Expand Down Expand Up @@ -112,7 +112,7 @@ def _ruff_aspect_impl(target, ctx):
return []

patch, report, info = patch_and_report_files(_MNEMONIC, target, ctx)
files_to_lint = [s for s in ctx.rule.files.srcs if s.is_source]
files_to_lint = filter_srcs(ctx.rule)
ruff_action(ctx, ctx.executable._ruff, files_to_lint, ctx.files._config_files, report, ctx.attr.fail_on_violation)
ruff_fix(ctx, ctx.executable, files_to_lint, ctx.files._config_files, patch)
return [info]
Expand Down
4 changes: 2 additions & 2 deletions lint/shellcheck.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ shellcheck = shellcheck_aspect(
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//lint/private:lint_aspect.bzl", "report_file")
load("//lint/private:lint_aspect.bzl", "filter_srcs", "report_file")

_MNEMONIC = "shellcheck"

Expand Down Expand Up @@ -82,7 +82,7 @@ def _shellcheck_aspect_impl(target, ctx):
return []

report, info = report_file(_MNEMONIC, target, ctx)
shellcheck_action(ctx, ctx.executable._shellcheck, [s for s in ctx.rule.files.srcs if s.is_source], ctx.file._config_file, report, ctx.attr.fail_on_violation)
shellcheck_action(ctx, ctx.executable._shellcheck, filter_srcs(ctx.rule), ctx.file._config_file, report, ctx.attr.fail_on_violation)
return [info]

def shellcheck_aspect(binary, config):
Expand Down

0 comments on commit e8b525e

Please sign in to comment.