Skip to content

Commit

Permalink
refactor: named utility for cases where we must have a meaningless ou…
Browse files Browse the repository at this point in the history
…tput for exit code
  • Loading branch information
alexeagle committed Jul 16, 2024
1 parent b295838 commit d10646a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
6 changes: 6 additions & 0 deletions example/test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# If bats isn't working on your local machine, you can run it in this container.
# rules_lint% docker build example/test -t bazel.build/bats
# rules_lint% docker run -it -v "$PWD:/code" bazel.build/bats /code/example/test
FROM bats/bats
RUN echo https://dl-cdn.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories
RUN apk add bazel
7 changes: 3 additions & 4 deletions lint/clang_tidy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ clang_tidy = lint_clang_tidy_aspect(

load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "dummy_successful_lint_action", "patch_and_report_files", "report_files")
load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "discard_exit_code", "dummy_successful_lint_action", "patch_and_report_files", "report_files")

_MNEMONIC = "AspectRulesLintClangTidy"

Expand Down Expand Up @@ -238,8 +238,7 @@ def clang_tidy_action(ctx, compilation_context, executable, srcs, stdout, exit_c
outputs = [stdout]
env = {}
env["CLANG_TIDY__STDOUT_STDERR_OUTPUT_FILE"] = stdout.path
if exit_code == "discard":
exit_code = ctx.actions.declare_file("_exit_code_discard", sibling = stdout)

if exit_code:
env["CLANG_TIDY__EXIT_CODE_OUTPUT_FILE"] = exit_code.path
outputs.append(exit_code)
Expand Down Expand Up @@ -325,7 +324,7 @@ def _clang_tidy_aspect_impl(target, ctx):
clang_tidy_action(ctx, compilation_context, ctx.executable, files_to_lint, stdout, exit_code)

# Run again for machine-readable output, only if rules_lint_report output_group is requested
clang_tidy_action(ctx, compilation_context, ctx.executable, files_to_lint, report, exit_code = "discard")
clang_tidy_action(ctx, compilation_context, ctx.executable, files_to_lint, report, exit_code = discard_exit_code(_MNEMONIC, target, ctx))
return [info]

def lint_clang_tidy_aspect(binary, configs = [], global_config = [], header_filter = "", lint_target_headers = False, angle_includes_are_system = True, verbose = False):
Expand Down
7 changes: 2 additions & 5 deletions lint/eslint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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", "LintOptionsInfo", "dummy_successful_lint_action", "filter_srcs", "patch_and_report_files", "report_files", "should_visit")
load("//lint/private:lint_aspect.bzl", "LintOptionsInfo", "discard_exit_code", "dummy_successful_lint_action", "filter_srcs", "patch_and_report_files", "report_files", "should_visit")

_MNEMONIC = "AspectRulesLintESLint"

Expand Down Expand Up @@ -113,9 +113,6 @@ def eslint_action(ctx, executable, srcs, stdout, exit_code = None, format = "sty
file_inputs.append(format)
args.add_all([s.short_path for s in srcs])

if exit_code == "discard":
exit_code = ctx.actions.declare_file("_discard_eslint_exit_code", sibling = stdout)

if not exit_code:
ctx.actions.run_shell(
inputs = _gather_inputs(ctx, srcs, file_inputs),
Expand Down Expand Up @@ -212,7 +209,7 @@ def _eslint_aspect_impl(target, ctx):
eslint_action(ctx, ctx.executable, files_to_lint, stdout, exit_code)

# Run again for machine-readable output, only if rules_lint_report output_group is requested
eslint_action(ctx, ctx.executable, files_to_lint, report, exit_code = "discard", format = ctx.attr._formatter)
eslint_action(ctx, ctx.executable, files_to_lint, report, exit_code = discard_exit_code(_MNEMONIC, target, ctx), format = ctx.attr._formatter)

return [info]

Expand Down
7 changes: 7 additions & 0 deletions lint/private/lint_aspect.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def should_visit(rule, allow_kinds, allow_filegroup_tags = []):

_OUTFILE_FORMAT = "{label}.{mnemonic}.{suffix}"

def discard_exit_code(mnemonic, target, ctx):
"""Utility to declare an output file which will never be read.
Needed as a workaround for js_binary which accepts an output file for the exit code,
allowing it to return 0 even if the spawned binary exits non-zero."""
return ctx.actions.declare_file(_OUTFILE_FORMAT.format(label = target.label.name, mnemonic = mnemonic, suffix = "exit_code.discard"))

def report_files(mnemonic, target, ctx):
"""Declare linter output files.
Expand Down

0 comments on commit d10646a

Please sign in to comment.