Skip to content

build: add static type checking for runfiles with rules_pyrefly - #4009

Merged
rickeylev merged 25 commits into
bazel-contrib:mainfrom
rickeylev:use_pyrefly_runfiles_check
Aug 7, 2026
Merged

build: add static type checking for runfiles with rules_pyrefly#4009
rickeylev merged 25 commits into
bazel-contrib:mainfrom
rickeylev:use_pyrefly_runfiles_check

Conversation

@rickeylev

@rickeylev rickeylev commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Enable static type verification for the runfiles library using the
rules_pyrefly ruleset.

To ensure hermetic type safety across Bazel runfiles utilities, this
configures rules_pyrefly's Bzlmod dev toolchain, tags the runfiles
target with tags = ["pyrefly"], and enables global aspect evaluation
in .bazelrc via --aspects=//tests/support/pyrefly:pyrefly.bzl%pyrefly_aspect.

To support WORKSPACE mode builds without breaking dependency resolution,
a no-op stub repository for rules_pyrefly is defined under
tests/modules/rules_pyrefly_stub and registered in internal_dev_deps.bzl.
Under Bzlmod, the aspect performs static type checking on tagged targets,
while under WORKSPACE mode, the stub aspect evaluates cleanly as a no-op.

Integrates rules_pyrefly in MODULE.bazel, defines the Pyrefly aspect in tools/pyrefly.bzl with --config=pyrefly in .bazelrc, and addresses a type override suppression in python/runfiles/runfiles.py.
Relocates pyrefly.bzl into tools/private and updates the aspect target reference in .bazelrc.
Relocates pyrefly.bzl under tools/private/pyrefly and updates .bazelrc label reference.
Synchronizes BUILD.bazel with Gazelle generated rules.
Updates the aspect reference in .bazelrc to use the tools/private/pyrefly package path.
…ate BUILD files

Cleans up unused pyrefly bzl_library definitions now that pyrefly.bzl lives in tools/private/pyrefly.
Removes the unnecessary bzl_library target and BUILD.bazel from tools/private/pyrefly, updating .bazelrc to reference //tools/private:pyrefly/pyrefly.bzl%pyrefly_aspect.
…ype checking

Implements pyrefly_check in tools/private:pyrefly/pyrefly.bzl taking targets as a label list, and instantiates runfiles_check in tests/runfiles/BUILD.bazel targeting //python/runfiles:runfiles.
…d use default_visibility in tools/private/pyrefly/BUILD.bazel
Defines pyrefly_check_test with test = True so running bazel test //tests/runfiles/... automatically discovers and executes runfiles_check as a test target.
Restores pyrefly_check as a build rule in tools/private/pyrefly/pyrefly.bzl and tests it using standard build_test in tests/runfiles/BUILD.bazel.
Encapsulates pyrefly_check rule and build_test into a reusable pyrefly_check_test macro in tools/private/pyrefly/pyrefly.bzl.
Relocates the pyrefly test helper package from tools/private/pyrefly to tests/support/pyrefly and updates load references.
Replaces the custom pyrefly_check wrapper rule with rules_pyrefly's
native opt_in_tags feature. Applies pyrefly_aspect globally via
.bazelrc while tagging //python/runfiles:runfiles for opt-in static
type checking.
Prevents WORKSPACE-mode (non-Bzlmod) builds from failing due to
unresolved @rules_pyrefly repository references while preserving
opt-in tag-based static type checking under Bzlmod.
Top-level if statements are forbidden in Bazel BUILD files. Moving
the BZLMOD_ENABLED check inside the pyrefly_check_test macro
definition in pyrefly.bzl ensures clean BUILD file evaluation while
preserving Bzlmod-only target generation.
Adds rules_pyrefly repository definition to rules_python_internal_deps
in internal_dev_deps.bzl so package loading succeeds during WORKSPACE
mode builds. Removes non-existent bzl_library dependency target.
@rickeylev
rickeylev marked this pull request as ready for review August 6, 2026 11:09
@rickeylev
rickeylev requested a review from aignas as a code owner August 6, 2026 11:09
@rickeylev
rickeylev enabled auto-merge August 6, 2026 11:09
Clarifies in pyrefly.bzl that Pyrefly type checking does not support
WORKSPACE mode and exits early when Bzlmod is not enabled.
@rickeylev
rickeylev added this pull request to the merge queue Aug 7, 2026
@rickeylev
rickeylev removed this pull request from the merge queue due to a manual request Aug 7, 2026
@rickeylev
rickeylev force-pushed the use_pyrefly_runfiles_check branch 3 times, most recently from 7ee3132 to 000a93d Compare August 7, 2026 02:47
Registers rules_pyrefly_stub in internal_dev_deps.bzl for WORKSPACE
mode so load() statements resolve, and adds global aspect flag to
.bazelrc to apply Pyrefly static type checking across tagged targets.
@rickeylev
rickeylev force-pushed the use_pyrefly_runfiles_check branch from 000a93d to 43649e1 Compare August 7, 2026 02:49
@rickeylev
rickeylev enabled auto-merge August 7, 2026 02:50
@rickeylev
rickeylev disabled auto-merge August 7, 2026 02:50
Removes the macro target invocation now that Pyrefly type checking is applied globally via aspect on tagged targets.
@rickeylev
rickeylev enabled auto-merge August 7, 2026 02:51
@rickeylev
rickeylev disabled auto-merge August 7, 2026 02:52
Removes the unused macro and rule definitions, leaving only pyrefly_aspect in tests/support/pyrefly/pyrefly.bzl.
Adds an always-on workspace rule prohibiting the use of auth tokens in command line arguments.
@rickeylev
rickeylev enabled auto-merge August 7, 2026 02:56
@rickeylev
rickeylev added this pull request to the merge queue Aug 7, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 7, 2026
@rickeylev
rickeylev added this pull request to the merge queue Aug 7, 2026
Merged via the queue into bazel-contrib:main with commit 81b3260 Aug 7, 2026
6 checks passed
@rickeylev
rickeylev deleted the use_pyrefly_runfiles_check branch August 7, 2026 03:40
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.

2 participants