From e16c7dacdb43a3b87afc949f54c3ace57688f9fb Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Wed, 8 Jul 2026 06:51:44 -0700 Subject: [PATCH] Add path-mapping support --- README.md | 38 +++++++++++++++++++++++++++ shellcheck/internal/BUILD.bazel | 8 +++--- shellcheck/internal/aspect_runner.bat | 26 +++++++++++++++--- shellcheck/internal/aspect_runner.sh | 13 +++++++-- shellcheck/internal/rules.bzl | 8 +++--- 5 files changed, 80 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5840c54..fbbe8aa 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,44 @@ shellcheck_test( ) ``` +## Configuring the shellcheck aspect + +`shellcheck_aspect` runs `shellcheck` against any target that provides `ShInfo` or `ShBinaryInfo` (e.g. `sh_binary` and `sh_library` from [rules_shell]) as part of a normal `bazel build`. Enable it by adding the following to your `.bazelrc`: + +``` +build --aspects=@rules_shellcheck//shellcheck:shellcheck_aspect.bzl%shellcheck_aspect +build --output_groups=+shellcheck_checks +``` + +With those flags in place, any build that includes shell targets will also run `shellcheck` on their sources; the lint results are exposed via the `shellcheck_checks` output group. + +### Output format and severity + +The aspect reads two `string_flag` build settings. Override them on the command line or via `.bazelrc`: + +``` +build --@rules_shellcheck//shellcheck/settings:format=gcc +build --@rules_shellcheck//shellcheck/settings:severity=warning +``` + +- `//shellcheck/settings:format` accepts `checkstyle`, `diff`, `gcc`, `json`, `json1`, `quiet`, or `tty` (default: `shellcheck`'s built-in default). +- `//shellcheck/settings:severity` accepts `error`, `warning`, `info`, or `style` (default: `shellcheck`'s built-in default). + +Because these are standard `string_flag`s, you can also flip them per-command with `--config` groups or with [`transitions`][transitions] if you want a specific target to lint under different settings. + +### Skipping targets + +Add any of the following tags to a target to opt it out of `shellcheck_aspect`: + +- `no_shellcheck` +- `noshellcheck` +- `no_lint` +- `nolint` + +Targets under external repositories (`workspace_root` starting with `external`) are always skipped. + Note: this is a simple project that allows me to learn about various bazel concepts. Feel free to create PRs contributing to the project or consider using [rules_lint]. [rules_lint]: https://github.com/aspect-build/rules_lint +[rules_shell]: https://github.com/bazelbuild/rules_shell +[transitions]: https://bazel.build/extending/config#user-defined-transitions diff --git a/shellcheck/internal/BUILD.bazel b/shellcheck/internal/BUILD.bazel index 7fa069f..faa0207 100644 --- a/shellcheck/internal/BUILD.bazel +++ b/shellcheck/internal/BUILD.bazel @@ -4,11 +4,11 @@ filegroup( visibility = ["//shellcheck:__pkg__"], ) -filegroup( +alias( name = "aspect_runner", - srcs = select({ - "@platforms//os:windows": ["aspect_runner.bat"], - "//conditions:default": ["aspect_runner.sh"], + actual = select({ + "@platforms//os:windows": "aspect_runner.bat", + "//conditions:default": "aspect_runner.sh", }), visibility = ["//visibility:public"], ) diff --git a/shellcheck/internal/aspect_runner.bat b/shellcheck/internal/aspect_runner.bat index fbc3752..91ba1a3 100755 --- a/shellcheck/internal/aspect_runner.bat +++ b/shellcheck/internal/aspect_runner.bat @@ -1,6 +1,24 @@ -#!/bin/sh +@echo off +setlocal enabledelayedexpansion -set -eu +set "output=%~1" +shift -echo "" > "${SHELLCHECK_ASPECT_OUTPUT}" -exec $@ +if not "%~1"=="--" ( + echo aspect_runner: expected '--' after output path, got: %~1 1>&2 + exit /b 1 +) +shift + +break > "%output%" + +set "cmd=" +:loop +if "%~1"=="" goto :run +set "cmd=!cmd! %1" +shift +goto :loop + +:run +%cmd% +exit /b %errorlevel% diff --git a/shellcheck/internal/aspect_runner.sh b/shellcheck/internal/aspect_runner.sh index fbc3752..9cc18d0 100755 --- a/shellcheck/internal/aspect_runner.sh +++ b/shellcheck/internal/aspect_runner.sh @@ -2,5 +2,14 @@ set -eu -echo "" > "${SHELLCHECK_ASPECT_OUTPUT}" -exec $@ +output="$1" +shift + +if [ "${1-}" != "--" ]; then + echo "aspect_runner: expected '--' after output path, got: ${1-}" >&2 + exit 1 +fi +shift + +echo "" > "${output}" +exec "$@" diff --git a/shellcheck/internal/rules.bzl b/shellcheck/internal/rules.bzl index 9864231..0296d92 100644 --- a/shellcheck/internal/rules.bzl +++ b/shellcheck/internal/rules.bzl @@ -191,6 +191,8 @@ def _shellcheck_aspect_impl(target, ctx): tools = depset([toolchain.shellcheck], transitive = [toolchain.all_files]) args = ctx.actions.args() + args.add(output) + args.add("--") args.add(toolchain.shellcheck) args.add(toolchain.shellcheckrc, format = "--rcfile=%s") args.add_all(src_info.source_paths, format_each = "--source-path=%s") @@ -209,11 +211,11 @@ def _shellcheck_aspect_impl(target, ctx): executable = ctx.file._runner, inputs = depset(inputs_direct, transitive = inputs_transitive), arguments = [args], - env = ctx.configuration.default_shell_env | { - "SHELLCHECK_ASPECT_OUTPUT": output.path, - }, + env = ctx.configuration.default_shell_env, tools = tools, outputs = [output], + execution_requirements = {"supports-path-mapping": ""}, + toolchain = TOOLCHAIN_TYPE, ) return [