Run SwiftLint in your Bazel build efficiently.
.bazelrc
file and
forget about it.rules_swiftlint
leverages Bazel's Validation
Actions to run
SwiftLint. This is special in that its outputs are always requested, regardless
of the value of the --output_groups
flag; and the validation is skipped when
the target is depended upon as an implicit dependency, or as a tool, or is
build in the exec
(or the legacy host
) configuration—in short, it only runs
on code that goes into your app. It is, however, possible to validate your
Swift tools by building or testing your tools directly.
Note that rules_swiftlint
only validates, but does not format your Swift
code, because source files are immutable to Bazel during a build or a test. If
your workflow requires you to auto-format your Swift code, you need to run
SwiftLint with bazel run
:
bazel run @SwiftLint//:swiftlint -- [<SwiftLint flags>]
a. If you want to use a prebuilt binary of SwiftLint:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "SwiftLint",
build_file_content = """exports_files(["swiftlint"])""",
# Update these two lines whenever you want to update SwiftLint.
sha256 = "47078845857fa7cf8497f5861967c7ce67f91915e073fb3d3114b8b2486a9270",
url = "https://github.com/realm/SwiftLint/releases/download/0.50.3/portable_swiftlint.zip",
)
http_archive(
name = "com_github_thii_rules_swiftlint",
sha256 = "<see https://github.com/thii/rules_swiftlint/releases>",
url = "<see https://github.com/thii/rules_swiftlint/releases>",
)
load(
"@com_github_thii_rules_swiftlint//swiftlint:repositories.bzl",
"swiftlint_rules_dependencies",
)
swiftlint_rules_dependencies()
load(
"@com_github_thii_rules_swiftlint//swiftlint:defs.bzl",
"swiftlint_register_toolchains",
)
swiftlint_register_toolchains()
b. If you want to build SwiftLint from source:
rules_apple
's dependencies.SwiftLint
's dependencies.http_archive(
name = "com_github_thii_rules_swiftlint",
sha256 = "<see https://github.com/thii/rules_swiftlint/releases>",
url = "<see https://github.com/thii/rules_swiftlint/releases>",
)
load(
"@com_github_thii_rules_swiftlint//swiftlint:repositories.bzl",
"swiftlint_rules_dependencies",
)
swiftlint_rules_dependencies()
load(
"@com_github_thii_rules_swiftlint//swiftlint:defs.bzl",
"swiftlint_register_toolchains",
)
swiftlint_register_toolchains()
Skip this step if you don't have your own SwiftLint configuration file.
Declare this in your BUILD file (e.g., in the top-level BUILD file). If you
have multiple configuration files, add them all to this filegroup
target.
filegroup(
name = "swiftlint_config",
srcs = [".swiftlint.yml"],
)
.bazelrc
filebuild --aspects=@com_github_thii_rules_swiftlint//swiftlint:defs.bzl%swiftlint_aspect
# This flag is only required if you have your own SwiftLint configuration file.
build --@com_github_thii_rules_swiftlint//swiftlint:config=//:swiftlint_config
Then build your target as usual. SwiftLint validation will run as part of the build and report violations as build warnings and errors.
bazel build //your:target
If you only want to run SwiftLint validation alone, add a new Bazel config for
that, and explicitly request only the _validation
output group:
build:swiftlint --aspects=@com_github_thii_rules_swiftlint//swiftlint:defs.bzl%swiftlint_aspect
build:swiftlint --@com_github_thii_rules_swiftlint//swiftlint:config=//:swiftlint_config
build:swiftlint --output_groups=_validation
Then you can run SwiftLint validation without building your target with:
bazel build --config=swiftlint //your:target
If you only want to build your target but skip the SwiftLint validation, you
can do so by setting the --norun_validations
(or
--run_validations={0,false,no}
) flag in your build.
bazel build --norun_validations //your:target
bazel build //examples:main
This repository uses the Bazel ruleset template from https://github.com/bazel-contrib/rules-template.