Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add shellcheck #32

Merged
merged 9 commits into from
Oct 26, 2023
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | |
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
105 changes: 105 additions & 0 deletions docs/shellcheck.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions example/.shellcheckrc
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -20,6 +21,7 @@ exports_files(
".flake8",
"pmd.xml",
".ruff.toml",
".shellcheckrc",
],
visibility = ["//visibility:public"],
)
Expand Down Expand Up @@ -59,3 +61,6 @@ native_binary(
out = "ruff",
visibility = ["//visibility:public"],
)

# bazel run :shellcheck -- --help
shellcheck_binary(name = "shellcheck")
4 changes: 4 additions & 0 deletions example/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,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()
4 changes: 4 additions & 0 deletions example/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ fetch_ktfmt()
fetch_swiftformat()

fetch_ruff()

load("@aspect_rules_lint//lint:shellcheck.bzl", "fetch_shellcheck")

fetch_shellcheck()
4 changes: 2 additions & 2 deletions example/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,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 $@
# 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 $@


# Show the results.
Expand Down
5 changes: 5 additions & 0 deletions example/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ java_library(
name = "foo",
srcs = ["Foo.java"],
)

sh_library(
name = "hello_shell",
srcs = ["hello.sh"],
)
3 changes: 3 additions & 0 deletions example/src/hello.sh
Original file line number Diff line number Diff line change
@@ -1,3 +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"
10 changes: 9 additions & 1 deletion example/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -37,3 +37,11 @@ eslint_test(
# Normally you'd fix the file instead of tagging this test.
tags = ["manual"],
)

shellcheck_test(
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"],
)
8 changes: 8 additions & 0 deletions example/tools/lint.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
28 changes: 28 additions & 0 deletions lint/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -67,3 +83,15 @@ 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_skylib//rules:native_binary",
"@bazel_tools//tools/build_defs/repo:http.bzl",
"@bazel_tools//tools/build_defs/repo:utils.bzl",
],
)
Loading