Skip to content

Attempt to use bazel rules directly to engage periphery#1127

Open
maxwellE wants to merge 4 commits into
peripheryapp:masterfrom
maxwellE:maxwelle/native-bazel-rules
Open

Attempt to use bazel rules directly to engage periphery#1127
maxwellE wants to merge 4 commits into
peripheryapp:masterfrom
maxwellE:maxwelle/native-bazel-rules

Conversation

@maxwellE
Copy link
Copy Markdown

Adds the ability to build periphery reports inside of bazel without using bazel run. This allows users to do things like scan_test which is a failing version of scan. We also now have scan_report which can be used as a data dep in other bazel rules

maxwellE and others added 4 commits May 15, 2026 12:36
Adds two new public rules in bazel/rules.bzl alongside the existing scan rule:

- scan_test: a test target that runs the scan with --strict so `bazel test`
  fails when unused code is found. Wires Periphery into CI without invoking
  the --bazel driver.
- scan_report: a build target that runs Periphery at build time and exposes
  the formatted report as a regular file output, so other rules can take it
  as a data dep / src.

Also fixes ScanCommand to always honor --write-results, instead of skipping
the file write when there are no findings. The previous behavior broke any
Bazel action that declared the report as an output.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds a new `bazel/tests/` package with two Swift fixtures (Clean.swift and
Unused.swift) that exercise all three rules end-to-end:

- clean_scan / clean_scan_test / clean_scan_report run against a fixture with
  no unused code, validating the rules wire up correctly and the test rule
  passes on clean inputs.
- unused_scan_report runs against a fixture with an intentionally unused
  declaration; an sh_test asserts the resulting JSON report contains the
  expected symbol name.

Also fixes both scan templates to omit `--config` when no config attribute is
set, instead of passing `--config ""` (which periphery rejects as a
nonexistent path). The existing rule worked in practice only because the
BazelProjectDriver always supplies an absolute config path.

Wires `bazel test //bazel/tests/...` into the existing Bazel CI job on both
macOS and Linux × Bazel 8 and 9, and adds rules_shell as a dev dep for the
sh_test rule (no longer built-in in Bazel 8+).
The previous force_indexstore transition only enabled
swift.index_while_building. Without whole-module optimization, each module
compiles file-by-file and emits a fragmented indexstore that doesn't capture
cross-file references the way periphery expects, leading to false positives.

Renames the transition to periphery_deps_transition and expands it to set:

- compilation_mode = opt, which is the prerequisite for WMO in rules_swift.
- swift.opt_uses_wmo, which actually triggers whole-module compilation.
- swift.index_while_building, unchanged.

Also clears swift.opt_uses_osize so size-driven optimizations don't elide
declarations periphery should observe. The transition is hermetic, so deps
used outside the scan/scan_test/scan_report path are unaffected.
The prior transition set compilation_mode=opt purely to make rules_swift's
swift.opt_uses_wmo feature kick in. That's a sledgehammer: opt also turns on
inlining, dead-code elimination, and size optimizations that we don't need
and don't want around an indexing build.

rules_swift's toolchain (swift/internal/wmo.bzl) already scans the
@rules_swift//swift:copt build setting for `-wmo`,
`-whole-module-optimization`, or `-force-single-frontend-invocation` and
auto-enables the private swift._wmo_in_swiftcopts feature. Setting `-wmo` via
that build setting in the transition triggers the same WMO codegen path
without changing compilation_mode, so scanned deps stay in fastbuild.

Verified via bazel aquery that the SwiftCompile action for scanned targets
has -wmo / -whole-module-optimization on its command line and its output
path is under darwin_arm64-fastbuild-..., not -opt-.
@maxwellE maxwellE marked this pull request as ready for review May 15, 2026 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant