Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions shellcheck/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
26 changes: 22 additions & 4 deletions shellcheck/internal/aspect_runner.bat
Original file line number Diff line number Diff line change
@@ -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%
13 changes: 11 additions & 2 deletions shellcheck/internal/aspect_runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$@"
8 changes: 5 additions & 3 deletions shellcheck/internal/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 [
Expand Down
Loading