From b54f144c3bb4535a0c1b947a50968c1452e97ff2 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 11:31:44 +0200 Subject: [PATCH 1/9] feat: add shellcheck --- README.md | 3 +- docs/BUILD.bazel | 5 ++ docs/shellcheck.md | 83 ++++++++++++++++++++++++++++ example/.shellcheckrc | 8 +++ example/BUILD.bazel | 43 +++++++++++++-- example/WORKSPACE.bazel | 8 +++ example/lint.sh | 3 +- example/src/BUILD.bazel | 5 ++ example/src/hello.sh | 1 + example/test/BUILD.bazel | 6 ++- example/tools/lint.bzl | 8 +++ lint/BUILD.bazel | 11 ++++ lint/shellcheck.bzl | 113 +++++++++++++++++++++++++++++++++++++++ 13 files changed, 290 insertions(+), 7 deletions(-) create mode 100644 docs/shellcheck.md create mode 100644 example/.shellcheckrc create mode 100644 lint/shellcheck.bzl diff --git a/README.md b/README.md index dbcd4ebc..60f7288f 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ This leads to some minor differences in how they are used in rules_lint. | CSS/HTML | [Prettier] | | | JSON | [Prettier] | | | Markdown | [Prettier] | | -| Bash | [prettier-plugin-sh] | | +| Bash | [prettier-plugin-sh] | [shellcheck] | | SQL | [prettier-plugin-sql] | | | Starlark (Bazel) | [Buildifier] | | | Swift | [SwiftFormat] (1) | | @@ -79,6 +79,7 @@ This leads to some minor differences in how they are used in rules_lint. [jsonnetfmt]: https://github.com/google/go-jsonnet [scalafmt]: https://scalameta.org/scalafmt [ruff]: https://docs.astral.sh/ruff/ +[shellcheck]: https://www.shellcheck.net/ 1. Non-hermetic: requires that a swift toolchain is installed on the machine. See https://github.com/bazelbuild/rules_swift#1-install-swift diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 83c98588..f2db725c 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -37,4 +37,9 @@ stardoc_with_diff_test( bzl_library_target = "//lint:ruff", ) +stardoc_with_diff_test( + name = "shellcheck", + bzl_library_target = "//lint:shellcheck", +) + update_docs(name = "update") diff --git a/docs/shellcheck.md b/docs/shellcheck.md new file mode 100644 index 00000000..3c6682f2 --- /dev/null +++ b/docs/shellcheck.md @@ -0,0 +1,83 @@ + + +API for declaring a shellcheck lint aspect that visits sh_library rules. + +Typical usage: + +``` +load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") + +shellcheck = shellcheck_aspect( + binary = "@@//:shellcheck", + config = "@@//:.shellcheckrc", +) +``` + + + + +## fetch_shellcheck + +
+fetch_shellcheck(version)
+
+ +A repository macro used from WORKSPACE to fetch binaries + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| version | a version of shellcheck that we have mirrored, e.g. v0.9.0 | "v0.9.0" | + + + + +## shellcheck_action + +
+shellcheck_action(ctx, executable, srcs, config, report, use_exit_code)
+
+ +Run shellcheck as an action under Bazel. + +Based on https://github.com/koalaman/shellcheck/blob/master/shellcheck.1.md + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| ctx | Bazel Rule or Aspect evaluation context | none | +| executable | label of the the shellcheck program | none | +| srcs | bash files to be linted | none | +| config | label of the .shellcheckrc file | none | +| report | output file to generate | none | +| use_exit_code | whether to fail the build when a lint violation is reported | False | + + + + +## shellcheck_aspect + +
+shellcheck_aspect(binary, config)
+
+ +A factory function to create a linter aspect. + +Attrs: + binary: a shellcheck executable. + config: the .shellcheckrc file + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| binary |

-

| none | +| config |

-

| none | + + diff --git a/example/.shellcheckrc b/example/.shellcheckrc new file mode 100644 index 00000000..ceb269aa --- /dev/null +++ b/example/.shellcheckrc @@ -0,0 +1,8 @@ +# Turn on warnings for unquoted variables with safe values +enable=quote-safe-variables + +# Turn on warnings for unassigned uppercase variables +enable=check-unassigned-uppercase + +# Allow [ ! -z foo ] instead of suggesting -n +disable=SC2236 \ No newline at end of file diff --git a/example/BUILD.bazel b/example/BUILD.bazel index 8622edcb..7bd4b9bd 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -20,6 +20,7 @@ exports_files( ".flake8", "pmd.xml", ".ruff.toml", + ".shellcheckrc", ], visibility = ["//visibility:public"], ) @@ -48,14 +49,48 @@ java_binary( runtime_deps = ["@net_sourceforge_pmd"], ) +config_setting( + name = "linux_x86", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], +) + +config_setting( + name = "linux_aarch64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], +) + # We can test that it works with: # bazel run :ruff -- help native_binary( name = "ruff", - src = select({ - "@platforms//os:osx": "@ruff_aarch64-apple-darwin//:ruff", - "//conditions:default": "@ruff_x86_64-unknown-linux-gnu//:ruff", - }), + src = select( + { + "@platforms//os:osx": "@ruff_aarch64-apple-darwin//:ruff", + ":linux_x86": "@ruff_x86_64-unknown-linux-gnu//:ruff", + }, + no_match_error = "Ruff has not been fetched for your platform", + ), out = "ruff", visibility = ["//visibility:public"], ) + +# bazel run :shellcheck -- --help +native_binary( + name = "shellcheck", + src = select( + { + "@platforms//os:osx": "@shellcheck_darwin.x86_64//:shellcheck", + ":linux_x86": "@shellcheck_linux.x86_64//:shellcheck", + ":linux_aarch64": "@shellcheck_linux.aarch64//:shellcheck", + }, + no_match_error = "Shellcheck hasn't been fetched for your platform", + ), + out = "shellcheck", + visibility = ["//visibility:public"], +) diff --git a/example/WORKSPACE.bazel b/example/WORKSPACE.bazel index c7f0ca76..e70db901 100644 --- a/example/WORKSPACE.bazel +++ b/example/WORKSPACE.bazel @@ -47,6 +47,10 @@ load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies") rules_js_dependencies() +load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies") + +aspect_bazel_lib_dependencies(override_local_config_platform = True) + load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains") nodejs_register_toolchains( @@ -170,3 +174,7 @@ fetch_swiftformat() load("@aspect_rules_lint//lint:ruff.bzl", "fetch_ruff") fetch_ruff() + +load("@aspect_rules_lint//lint:shellcheck.bzl", "fetch_shellcheck") + +fetch_shellcheck() diff --git a/example/lint.sh b/example/lint.sh index 0e236cdc..6652bcbc 100755 --- a/example/lint.sh +++ b/example/lint.sh @@ -15,7 +15,8 @@ fi # Produce report files # You can add --aspects_parameters=fail_on_violation=true to make this command fail instead. -bazel build --aspects //tools:lint.bzl%eslint,//tools:lint.bzl%buf,//tools:lint.bzl%flake8,//tools:lint.bzl%pmd,//tools:lint.bzl%ruff --output_groups=rules_lint_report $@ +# //tools:lint.bzl%eslint,//tools:lint.bzl%buf,//tools:lint.bzl%flake8,//tools:lint.bzl%pmd,//tools:lint.bzl%ruff +bazel build --aspects //tools:lint.bzl%shellcheck --output_groups=rules_lint_report $@ # Show the results. diff --git a/example/src/BUILD.bazel b/example/src/BUILD.bazel index 24c0031b..c63cdf03 100644 --- a/example/src/BUILD.bazel +++ b/example/src/BUILD.bazel @@ -27,3 +27,8 @@ java_library( name = "foo", srcs = ["Foo.java"], ) + +sh_library( + name = "hello_shell", + srcs = ["hello.sh"], +) diff --git a/example/src/hello.sh b/example/src/hello.sh index a51622d3..3f0d0f03 100644 --- a/example/src/hello.sh +++ b/example/src/hello.sh @@ -1,3 +1,4 @@ #!/bin/bash [ -z $THING ] && echo "hello world" +[ ! -z "$foo" ] && echo "foo" diff --git a/example/test/BUILD.bazel b/example/test/BUILD.bazel index 7f92b96e..6c26fab4 100644 --- a/example/test/BUILD.bazel +++ b/example/test/BUILD.bazel @@ -1,7 +1,7 @@ "Demonstrates how to enforce zero-lint-tolerance policy with tests" load("@aspect_rules_ts//ts:defs.bzl", "ts_project") -load("//tools:lint.bzl", "eslint_test", "flake8_test", "pmd_test") +load("//tools:lint.bzl", "eslint_test", "flake8_test", "pmd_test", "shellcheck_test") ts_project( name = "no_violations", @@ -37,3 +37,7 @@ eslint_test( # Normally you'd fix the file instead of tagging this test. tags = ["manual"], ) + +shellcheck_test( + srcs = ["//src:hello_sh"], +) diff --git a/example/tools/lint.bzl b/example/tools/lint.bzl index df496cd4..e4f6382f 100644 --- a/example/tools/lint.bzl +++ b/example/tools/lint.bzl @@ -6,6 +6,7 @@ load("@aspect_rules_lint//lint:flake8.bzl", "flake8_aspect") load("@aspect_rules_lint//lint:lint_test.bzl", "make_lint_test") load("@aspect_rules_lint//lint:pmd.bzl", "pmd_aspect") load("@aspect_rules_lint//lint:ruff.bzl", "ruff_aspect") +load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") buf = buf_lint_aspect( config = "@@//:buf.yaml", @@ -36,3 +37,10 @@ ruff = ruff_aspect( binary = "@@//:ruff", config = "@@//:.ruff.toml", ) + +shellcheck = shellcheck_aspect( + binary = "@@//:shellcheck", + config = "@@//:.shellcheckrc", +) + +shellcheck_test = make_lint_test(aspect = shellcheck) diff --git a/lint/BUILD.bazel b/lint/BUILD.bazel index cc0c025d..6728f8a5 100644 --- a/lint/BUILD.bazel +++ b/lint/BUILD.bazel @@ -67,3 +67,14 @@ bzl_library( "@bazel_tools//tools/build_defs/repo:utils.bzl", ], ) + +bzl_library( + name = "shellcheck", + srcs = ["shellcheck.bzl"], + visibility = ["//visibility:public"], + deps = [ + "//lint/private:lint_aspect", + "@bazel_tools//tools/build_defs/repo:http.bzl", + "@bazel_tools//tools/build_defs/repo:utils.bzl", + ], +) diff --git a/lint/shellcheck.bzl b/lint/shellcheck.bzl new file mode 100644 index 00000000..624b323d --- /dev/null +++ b/lint/shellcheck.bzl @@ -0,0 +1,113 @@ +"""API for declaring a shellcheck lint aspect that visits sh_library rules. + +Typical usage: + +``` +load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") + +shellcheck = shellcheck_aspect( + binary = "@@//:shellcheck", + config = "@@//:.shellcheckrc", +) +``` +""" + +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") + +def shellcheck_action(ctx, executable, srcs, config, report, use_exit_code = False): + """Run shellcheck as an action under Bazel. + + Based on https://github.com/koalaman/shellcheck/blob/master/shellcheck.1.md + + Args: + ctx: Bazel Rule or Aspect evaluation context + executable: label of the the shellcheck program + srcs: bash files to be linted + config: label of the .shellcheckrc file + report: output file to generate + use_exit_code: whether to fail the build when a lint violation is reported + """ + inputs = srcs + [config] + outputs = [report] + + # Wire command-line options, see + # https://github.com/koalaman/shellcheck/blob/master/shellcheck.1.md#options + args = ctx.actions.args() + args.add_all(srcs) + + ctx.actions.run_shell( + inputs = inputs + [executable], + outputs = outputs, + command = """\ + {shellcheck} $@ 2>{report} {exit_zero} + """.format( + shellcheck = executable.path, + report = report.path, + exit_zero = "" if use_exit_code else "|| true", + ), + arguments = [args], + mnemonic = "shellcheck", + ) + +# buildifier: disable=function-docstring +def _shellcheck_aspect_impl(target, ctx): + if ctx.rule.kind not in ["sh_library"]: + return [] + + report, info = report_file(target, ctx) + shellcheck_action(ctx, ctx.executable._shellcheck, ctx.rule.files.srcs, ctx.file._config_file, report, ctx.attr.fail_on_violation) + return [info] + +def shellcheck_aspect(binary, config): + """A factory function to create a linter aspect. + + Attrs: + binary: a shellcheck executable. + config: the .shellcheckrc file + """ + return aspect( + implementation = _shellcheck_aspect_impl, + attrs = { + "fail_on_violation": attr.bool(), + "_shellcheck": attr.label( + default = binary, + executable = True, + cfg = "exec", + ), + "_config_file": attr.label( + default = config, + allow_single_file = True, + ), + }, + ) + +# Data manually mirrored from https://github.com/koalaman/shellcheck/releases +# TODO: add a mirror_shellcheck.sh script to automate this +SHELLCHECK_VERSIONS = { + "v0.9.0": { + "darwin.x86_64": "7d3730694707605d6e60cec4efcb79a0632d61babc035aa16cda1b897536acf5", + "linux.x86_64": "700324c6dd0ebea0117591c6cc9d7350d9c7c5c287acbad7630fa17b1d4d9e2f", + "linux.aarch64": "179c579ef3481317d130adebede74a34dbbc2df961a70916dd4039ebf0735fae", + }, +} + +def fetch_shellcheck(version = SHELLCHECK_VERSIONS.keys()[0]): + """A repository macro used from WORKSPACE to fetch binaries + + Args: + version: a version of shellcheck that we have mirrored, e.g. `v0.9.0` + """ + for plat, sha256 in SHELLCHECK_VERSIONS[version].items(): + maybe( + http_archive, + name = "shellcheck_{}".format(plat), + url = "https://github.com/koalaman/shellcheck/releases/download/{version}/shellcheck-{version}.{plat}.tar.xz".format( + version = version, + plat = plat, + ), + strip_prefix = "shellcheck-{}".format(version), + sha256 = sha256, + build_file_content = """exports_files(["shellcheck"])""", + ) From 840e7cef94c1c4f3671f3bb058b4f5d8891af45a Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 11:41:49 +0200 Subject: [PATCH 2/9] fixed --- example/WORKSPACE.bazel | 4 ---- example/lint.sh | 5 ++--- lint/shellcheck.bzl | 2 +- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/example/WORKSPACE.bazel b/example/WORKSPACE.bazel index e70db901..74770262 100644 --- a/example/WORKSPACE.bazel +++ b/example/WORKSPACE.bazel @@ -47,10 +47,6 @@ load("@aspect_rules_js//js:repositories.bzl", "rules_js_dependencies") rules_js_dependencies() -load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies") - -aspect_bazel_lib_dependencies(override_local_config_platform = True) - load("@rules_nodejs//nodejs:repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains") nodejs_register_toolchains( diff --git a/example/lint.sh b/example/lint.sh index 6652bcbc..f4fa45a2 100755 --- a/example/lint.sh +++ b/example/lint.sh @@ -14,9 +14,8 @@ fi # Produce report files # You can add --aspects_parameters=fail_on_violation=true to make this command fail instead. - -# //tools:lint.bzl%eslint,//tools:lint.bzl%buf,//tools:lint.bzl%flake8,//tools:lint.bzl%pmd,//tools:lint.bzl%ruff -bazel build --aspects //tools:lint.bzl%shellcheck --output_groups=rules_lint_report $@ +# Note: can only run one of +bazel build --aspects //tools:lint.bzl%eslint,//tools:lint.bzl%buf,//tools:lint.bzl%flake8,//tools:lint.bzl%pmd,//tools:lint.bzl%shellcheck --output_groups=rules_lint_report $@ # Show the results. diff --git a/lint/shellcheck.bzl b/lint/shellcheck.bzl index 624b323d..669b5b34 100644 --- a/lint/shellcheck.bzl +++ b/lint/shellcheck.bzl @@ -41,7 +41,7 @@ def shellcheck_action(ctx, executable, srcs, config, report, use_exit_code = Fal inputs = inputs + [executable], outputs = outputs, command = """\ - {shellcheck} $@ 2>{report} {exit_zero} + {shellcheck} $@ >{report} {exit_zero} """.format( shellcheck = executable.path, report = report.path, From 3e0dad32b532c1ce56a257bfcb5fc7bc8507073d Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:00:43 +0200 Subject: [PATCH 3/9] code review improvements --- example/BUILD.bazel | 31 ++----------------------------- example/WORKSPACE.bzlmod | 4 ++++ example/lint.sh | 2 +- example/src/hello.sh | 2 ++ lint/BUILD.bazel | 16 ++++++++++++++++ lint/shellcheck.bzl | 16 ++++++++++++++++ 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/example/BUILD.bazel b/example/BUILD.bazel index 7bd4b9bd..2d454ee6 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -1,4 +1,5 @@ load("@aspect_rules_js//js:defs.bzl", "js_library") +load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_binary") load("@bazel_skylib//rules:native_binary.bzl", "native_binary") load("@npm//:defs.bzl", "npm_link_all_packages") load("@npm//:eslint/package_json.bzl", eslint_bin = "bin") @@ -49,22 +50,6 @@ java_binary( runtime_deps = ["@net_sourceforge_pmd"], ) -config_setting( - name = "linux_x86", - constraint_values = [ - "@platforms//os:linux", - "@platforms//cpu:x86_64", - ], -) - -config_setting( - name = "linux_aarch64", - constraint_values = [ - "@platforms//os:linux", - "@platforms//cpu:aarch64", - ], -) - # We can test that it works with: # bazel run :ruff -- help native_binary( @@ -81,16 +66,4 @@ native_binary( ) # bazel run :shellcheck -- --help -native_binary( - name = "shellcheck", - src = select( - { - "@platforms//os:osx": "@shellcheck_darwin.x86_64//:shellcheck", - ":linux_x86": "@shellcheck_linux.x86_64//:shellcheck", - ":linux_aarch64": "@shellcheck_linux.aarch64//:shellcheck", - }, - no_match_error = "Shellcheck hasn't been fetched for your platform", - ), - out = "shellcheck", - visibility = ["//visibility:public"], -) +shellcheck_binary(name = "shellcheck") diff --git a/example/WORKSPACE.bzlmod b/example/WORKSPACE.bzlmod index e25b48c1..9a172123 100644 --- a/example/WORKSPACE.bzlmod +++ b/example/WORKSPACE.bzlmod @@ -27,3 +27,7 @@ fetch_ktfmt() fetch_swiftformat() fetch_ruff() + +load("@aspect_rules_lint//lint:shellcheck.bzl", "fetch_shellcheck") + +fetch_shellcheck() diff --git a/example/lint.sh b/example/lint.sh index f4fa45a2..700b502f 100755 --- a/example/lint.sh +++ b/example/lint.sh @@ -14,7 +14,7 @@ fi # Produce report files # You can add --aspects_parameters=fail_on_violation=true to make this command fail instead. -# Note: can only run one of +# TODO: put back ruff after the output paths don't collide bazel build --aspects //tools:lint.bzl%eslint,//tools:lint.bzl%buf,//tools:lint.bzl%flake8,//tools:lint.bzl%pmd,//tools:lint.bzl%shellcheck --output_groups=rules_lint_report $@ diff --git a/example/src/hello.sh b/example/src/hello.sh index 3f0d0f03..51afcbc1 100644 --- a/example/src/hello.sh +++ b/example/src/hello.sh @@ -1,4 +1,6 @@ #!/bin/bash [ -z $THING ] && echo "hello world" + +# Note, we should not get a lint here because the .shellcheckrc excludes it [ ! -z "$foo" ] && echo "foo" diff --git a/lint/BUILD.bazel b/lint/BUILD.bazel index 6728f8a5..ea91066c 100644 --- a/lint/BUILD.bazel +++ b/lint/BUILD.bazel @@ -3,6 +3,22 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") exports_files(glob(["*.bzl"]) + ["lint_test.sh"]) +config_setting( + name = "linux_x86", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], +) + +config_setting( + name = "linux_aarch64", + constraint_values = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], +) + js_library( name = "eslint.workaround_17660", srcs = ["eslint.workaround_17660.js"], diff --git a/lint/shellcheck.bzl b/lint/shellcheck.bzl index 669b5b34..3ce49b71 100644 --- a/lint/shellcheck.bzl +++ b/lint/shellcheck.bzl @@ -12,10 +12,26 @@ 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") +def shellcheck_binary(name): + native_binary( + name = name, + src = select( + { + "@platforms//os:osx": "@shellcheck_darwin.x86_64//:shellcheck", + "@aspect_rules_lint//lint:linux_x86": "@shellcheck_linux.x86_64//:shellcheck", + "@aspect_rules_lint//lint:linux_aarch64": "@shellcheck_linux.aarch64//:shellcheck", + }, + no_match_error = "Shellcheck hasn't been fetched for your platform", + ), + out = "shellcheck", + visibility = ["//visibility:public"], + ) + def shellcheck_action(ctx, executable, srcs, config, report, use_exit_code = False): """Run shellcheck as an action under Bazel. From 253a3b9f47353789dcb48f15531a45f0a3023806 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:02:59 +0200 Subject: [PATCH 4/9] chore: docgen --- docs/shellcheck.md | 18 ++++++++++++++++++ lint/BUILD.bazel | 1 + 2 files changed, 19 insertions(+) diff --git a/docs/shellcheck.md b/docs/shellcheck.md index 3c6682f2..252ece0d 100644 --- a/docs/shellcheck.md +++ b/docs/shellcheck.md @@ -81,3 +81,21 @@ Attrs: | config |

-

| none | + + +## shellcheck_binary + +
+shellcheck_binary(name)
+
+ + + +**PARAMETERS** + + +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| name |

-

| none | + + diff --git a/lint/BUILD.bazel b/lint/BUILD.bazel index ea91066c..e5386170 100644 --- a/lint/BUILD.bazel +++ b/lint/BUILD.bazel @@ -90,6 +90,7 @@ bzl_library( visibility = ["//visibility:public"], deps = [ "//lint/private:lint_aspect", + "@bazel_skylib//rules:native_binary", "@bazel_tools//tools/build_defs/repo:http.bzl", "@bazel_tools//tools/build_defs/repo:utils.bzl", ], From 28e87d44fdf89ed689555fba991e44819d2c8a24 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:04:37 +0200 Subject: [PATCH 5/9] revert change to ruff --- example/BUILD.bazel | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/example/BUILD.bazel b/example/BUILD.bazel index 2d454ee6..a9395cd2 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -54,13 +54,10 @@ java_binary( # bazel run :ruff -- help native_binary( name = "ruff", - src = select( - { - "@platforms//os:osx": "@ruff_aarch64-apple-darwin//:ruff", - ":linux_x86": "@ruff_x86_64-unknown-linux-gnu//:ruff", - }, - no_match_error = "Ruff has not been fetched for your platform", - ), + src = select({ + "@platforms//os:osx": "@ruff_aarch64-apple-darwin//:ruff", + "//conditions:default": "@ruff_x86_64-unknown-linux-gnu//:ruff", + }), out = "ruff", visibility = ["//visibility:public"], ) From 59d259fd66e2fa633dd93da056013ae43c1004ec Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:06:27 +0200 Subject: [PATCH 6/9] Add missing doc --- docs/shellcheck.md | 2 +- lint/shellcheck.bzl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/shellcheck.md b/docs/shellcheck.md index 252ece0d..59bb6f26 100644 --- a/docs/shellcheck.md +++ b/docs/shellcheck.md @@ -89,7 +89,7 @@ Attrs: shellcheck_binary(name) - +Wrapper around native_binary to select the correct shellcheck executable for the execution platform. **PARAMETERS** diff --git a/lint/shellcheck.bzl b/lint/shellcheck.bzl index 3ce49b71..8a787776 100644 --- a/lint/shellcheck.bzl +++ b/lint/shellcheck.bzl @@ -18,6 +18,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//lint/private:lint_aspect.bzl", "report_file") def shellcheck_binary(name): + """Wrapper around native_binary to select the correct shellcheck executable for the execution platform.""" native_binary( name = name, src = select( From a36cf7763967237f1e531ac1155074d4ef7113f1 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:09:16 +0200 Subject: [PATCH 7/9] more improve docs --- docs/shellcheck.md | 6 +++++- lint/shellcheck.bzl | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/docs/shellcheck.md b/docs/shellcheck.md index 59bb6f26..6a6d423b 100644 --- a/docs/shellcheck.md +++ b/docs/shellcheck.md @@ -4,11 +4,15 @@ API for declaring a shellcheck lint aspect that visits sh_library rules. Typical usage: +1. Use [fetch_shellcheck] in WORKSPACE to call the `http_archive` calls to download binaries. +2. Use [shellcheck_binary] in `tools/BUILD.bazel` to declare the shellcheck target +3. Use [shellcheck_aspect] in `tools/lint.bzl` to declare the shellcheck linter aspect: + ``` load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") shellcheck = shellcheck_aspect( - binary = "@@//:shellcheck", + binary = "@@//tools:shellcheck", config = "@@//:.shellcheckrc", ) ``` diff --git a/lint/shellcheck.bzl b/lint/shellcheck.bzl index 8a787776..573a0cf2 100644 --- a/lint/shellcheck.bzl +++ b/lint/shellcheck.bzl @@ -2,11 +2,15 @@ Typical usage: +1. Use [fetch_shellcheck] in WORKSPACE to call the `http_archive` calls to download binaries. +2. Use [shellcheck_binary] in `tools/BUILD.bazel` to declare the shellcheck target +3. Use [shellcheck_aspect] in `tools/lint.bzl` to declare the shellcheck linter aspect: + ``` load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") shellcheck = shellcheck_aspect( - binary = "@@//:shellcheck", + binary = "@@//tools:shellcheck", config = "@@//:.shellcheckrc", ) ``` From 9712e7f4af78131df9aa7ef7b5dfb6bfc5c7f76b Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:10:04 +0200 Subject: [PATCH 8/9] more improve docs --- docs/shellcheck.md | 6 +++--- lint/shellcheck.bzl | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/shellcheck.md b/docs/shellcheck.md index 6a6d423b..fc79bf1c 100644 --- a/docs/shellcheck.md +++ b/docs/shellcheck.md @@ -4,9 +4,9 @@ API for declaring a shellcheck lint aspect that visits sh_library rules. Typical usage: -1. Use [fetch_shellcheck] in WORKSPACE to call the `http_archive` calls to download binaries. -2. Use [shellcheck_binary] in `tools/BUILD.bazel` to declare the shellcheck target -3. Use [shellcheck_aspect] in `tools/lint.bzl` to declare the shellcheck linter aspect: +1. Use [fetch_shellcheck](#fetch_shellcheck) in WORKSPACE to call the `http_archive` calls to download binaries. +2. Use [shellcheck_binary](#shellcheck_binary) in `tools/BUILD.bazel` to declare the shellcheck target +3. Use [shellcheck_aspect](#shellcheck_aspect) in `tools/lint.bzl` to declare the shellcheck linter aspect: ``` load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") diff --git a/lint/shellcheck.bzl b/lint/shellcheck.bzl index 573a0cf2..2cf2150f 100644 --- a/lint/shellcheck.bzl +++ b/lint/shellcheck.bzl @@ -2,9 +2,9 @@ Typical usage: -1. Use [fetch_shellcheck] in WORKSPACE to call the `http_archive` calls to download binaries. -2. Use [shellcheck_binary] in `tools/BUILD.bazel` to declare the shellcheck target -3. Use [shellcheck_aspect] in `tools/lint.bzl` to declare the shellcheck linter aspect: +1. Use [fetch_shellcheck](#fetch_shellcheck) in WORKSPACE to call the `http_archive` calls to download binaries. +2. Use [shellcheck_binary](#shellcheck_binary) in `tools/BUILD.bazel` to declare the shellcheck target +3. Use [shellcheck_aspect](#shellcheck_aspect) in `tools/lint.bzl` to declare the shellcheck linter aspect: ``` load("@aspect_rules_lint//lint:shellcheck.bzl", "shellcheck_aspect") From 760774c381d14c5dfec62580fd54da269ffc570a Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Thu, 26 Oct 2023 12:15:12 +0200 Subject: [PATCH 9/9] fix shellcheck test --- example/test/BUILD.bazel | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/example/test/BUILD.bazel b/example/test/BUILD.bazel index 6c26fab4..ab9490cf 100644 --- a/example/test/BUILD.bazel +++ b/example/test/BUILD.bazel @@ -39,5 +39,9 @@ eslint_test( ) shellcheck_test( - srcs = ["//src:hello_sh"], + name = "shellcheck", + srcs = ["//src:hello_shell"], + # Expected to fail based on current content of the file. + # Normally you'd fix the file instead of tagging this test. + tags = ["manual"], )