Skip to content
Draft
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: 3 additions & 0 deletions bazel/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")

use_repo(pip, "pip3", "website_pip3")

reachability_test_ext = use_extension("//dependency/test:reachability_test_extension.bzl", "reachability_test_extension", dev_dependency = True)
use_repo(reachability_test_ext, "apparent_excluded_repo", "apparent_excluded_transitive_repo")

# Load llvm_source for compile targets
http_archive = use_repo_rule("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

Expand Down
46 changes: 35 additions & 11 deletions bazel/MODULE.bazel.lock

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

3 changes: 3 additions & 0 deletions bazel/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ load_website_packages()

load("@website_pip3//:requirements.bzl", website_install_deps = "install_deps")
website_install_deps()

load("//dependency/test:reachability_test_extension.bzl", "reachability_test_repos")
reachability_test_repos()
15 changes: 10 additions & 5 deletions bazel/dependency/reachability.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ def _repo_is_excluded(repo_name, patterns):
return True
return False

def _repo_or_apparent_is_excluded(repo_name, patterns):
return _repo_is_excluded(repo_name, patterns) or _repo_is_excluded(apparent_name(repo_name), patterns)

def _matches_package_pattern(package, pattern):
if pattern == "//...":
return True
Expand Down Expand Up @@ -395,7 +398,7 @@ def _reachability_aspect_impl(target, ctx):
continue
for dep in _attr_targets(ctx.rule.attr, attr_name):
dep_repo = dep.label.repo_name
if dep_repo and _repo_is_excluded(dep_repo, excluded_patterns):
if dep_repo and _repo_or_apparent_is_excluded(dep_repo, excluded_patterns):
continue
if dep_repo and dep_repo != consumer_repo:
edges.append(struct(
Expand Down Expand Up @@ -717,10 +720,12 @@ def _dependency_reachability_rule(flags = []):
"excluded_patterns": attr.string_list(
default = [],
doc = (
"Canonical repository-name patterns excluded from " +
"traversal. Supported forms are exact matches (`repo_name`) " +
"and a single trailing `*` wildcard (`prefix*`) for prefix " +
"matching."
"Repository-name patterns excluded from traversal. Patterns " +
"are matched against both the canonical repository name and " +
"the apparent/module name emitted in dependency metadata, " +
"so metadata-facing patterns work under both WORKSPACE and " +
"bzlmod. Supported forms are exact matches (`repo_name`) and " +
"a single trailing `*` wildcard (`prefix*`) for prefix matching."
),
),
"attribution_patterns": attr.string_list(
Expand Down
21 changes: 21 additions & 0 deletions bazel/dependency/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,24 @@ dependency_reachability(
excluded_patterns = ["bazel_*"],
)

filegroup(
name = "apparent_exclusion_root",
srcs = ["@apparent_excluded_repo//:direct"],
)

dependency_reachability(
name = "reachability_apparent_exclusion_baseline",
testonly = True,
roots = [":apparent_exclusion_root"],
)

dependency_reachability(
name = "reachability_apparent_exclusion",
testonly = True,
roots = [":apparent_exclusion_root"],
excluded_patterns = ["apparent_excluded_repo"],
)

filegroup(
name = "rules_pkg_root",
srcs = ["@rules_pkg//pkg:filter_directory"],
Expand Down Expand Up @@ -317,6 +335,8 @@ sh_test(
data = [
":reachability_excluded_edge",
":reachability_edge_and_pattern",
":reachability_apparent_exclusion_baseline",
":reachability_apparent_exclusion",
":reachability_excluded_pattern",
":reachability_rules_pkg_transitive",
":reachability_excluded_rules_pkg_transitive",
Expand All @@ -338,6 +358,7 @@ bzl_library(
"reachability_apparent_name_test.bzl",
"reachability_config_validation_test.bzl",
"reachability_package_pattern_test.bzl",
"reachability_test_extension.bzl",
"reachability_test_rules.bzl",
],
deps = [
Expand Down
22 changes: 22 additions & 0 deletions bazel/dependency/test/exclusions_reachability_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ JQ="${JQ_BIN:-jq}"
REACHABILITY_EXCLUDED_EDGE_JSON="${RUNFILES_DIR}/dependency/test/reachability_excluded_edge.json"
REACHABILITY_EDGE_AND_PATTERN_JSON="${RUNFILES_DIR}/dependency/test/reachability_edge_and_pattern.json"
REACHABILITY_EXCLUDED_PATTERN_JSON="${RUNFILES_DIR}/dependency/test/reachability_excluded_pattern.json"
REACHABILITY_APPARENT_EXCLUSION_BASELINE_JSON="${RUNFILES_DIR}/dependency/test/reachability_apparent_exclusion_baseline.json"
REACHABILITY_APPARENT_EXCLUSION_JSON="${RUNFILES_DIR}/dependency/test/reachability_apparent_exclusion.json"
REACHABILITY_RULES_PKG_TRANSITIVE_JSON="${RUNFILES_DIR}/dependency/test/reachability_rules_pkg_transitive.json"
REACHABILITY_EXCLUDED_RULES_PKG_TRANSITIVE_JSON="${RUNFILES_DIR}/dependency/test/reachability_excluded_rules_pkg_transitive.json"
REACHABILITY_EMPTY_EXCLUSIONS_JSON="${RUNFILES_DIR}/dependency/test/reachability_empty_exclusions.json"
Expand Down Expand Up @@ -85,6 +87,26 @@ check "combined excluded_edges and excluded_patterns apply together" \
"0" \
"${REACHABILITY_EDGE_AND_PATTERN_JSON}"

check "baseline records extension repo by apparent name" \
'[.dependencies | to_entries[] | select(.value.name == "apparent_excluded_repo")] | length' \
"1" \
"${REACHABILITY_APPARENT_EXCLUSION_BASELINE_JSON}"

check "baseline reaches through bzlmod extension repo before apparent-name exclusion" \
'[.dependencies[] | .name] | any(. == "apparent_excluded_transitive_repo")' \
"true" \
"${REACHABILITY_APPARENT_EXCLUSION_BASELINE_JSON}"

check "apparent-name excluded bzlmod extension repo is not recorded" \
'[.dependencies[] | .name] | any(. == "apparent_excluded_repo")' \
"false" \
"${REACHABILITY_APPARENT_EXCLUSION_JSON}"

check "apparent-name excluded bzlmod extension repo is pruned transitively" \
'[.dependencies[] | .name] | any(. == "apparent_excluded_transitive_repo")' \
"false" \
"${REACHABILITY_APPARENT_EXCLUSION_JSON}"

check "baseline attribution records the unexcluded repository" \
'.dependencies | to_entries[] | select(.value.name == "rules_pkg") | .value.attributed_packages[0].package' \
"//dependency/test/transitive/ext/excluded" \
Expand Down
43 changes: 43 additions & 0 deletions bazel/dependency/test/reachability_test_extension.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Test-only module extension for reachability tests."""

def _test_repo_impl(ctx):
ctx.file("BUILD.bazel", ctx.attr.build_file_content)

_test_repo = repository_rule(
implementation = _test_repo_impl,
attrs = {
"build_file_content": attr.string(mandatory = True),
},
)

def reachability_test_repos():
"""Declares the fixture repos shared by bzlmod and WORKSPACE reachability tests."""
_test_repo(
name = "apparent_excluded_transitive_repo",
build_file_content = """\
package(default_visibility = ["//visibility:public"])

filegroup(
name = "leaf",
srcs = [],
)
""",
)
_test_repo(
name = "apparent_excluded_repo",
build_file_content = """\
package(default_visibility = ["//visibility:public"])

filegroup(
name = "direct",
srcs = ["@apparent_excluded_transitive_repo//:leaf"],
)
""",
)

def _reachability_test_extension_impl(_module_ctx):
reachability_test_repos()

reachability_test_extension = module_extension(
implementation = _reachability_test_extension_impl,
)