Skip to content

Commit

Permalink
python: make disallow native rules to work with external repos with e…
Browse files Browse the repository at this point in the history
…mpty package

This basically makes it usable with the downloaded runtimes rules_python
makes available. The issue was the code to construct the label was
leaving a trailing "/" when the the target being checked at the root
of the workspace. To fix, just omit the trailing slash when
the package name is empty to prevent the trailing slash.

Work towards bazelbuild#17773
  • Loading branch information
rickeylev committed Dec 12, 2023
1 parent 21ad24c commit e5240a6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/starlark/builtins_bzl/common/python/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,10 @@ def check_native_allowed(ctx):
# package_group doesn't allow @repo syntax, so we work around that
# by prefixing external repos with a fake package path. This also
# makes it easy to enable or disable all external repos.
check_label = Label("@//__EXTERNAL_REPOS__/{workspace}/{package}".format(
check_label = Label("@//__EXTERNAL_REPOS__/{workspace}{package}".format(
workspace = ctx.label.workspace_name,
package = ctx.label.package,
# Prevent a label with trailing slash, which is malformed.
package = "/" + ctx.label.package if ctx.label.package else "",
))
allowlist = ctx.attr._native_rules_allowlist
if allowlist:
Expand Down
60 changes: 60 additions & 0 deletions src/test/shell/bazel/python_version_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,64 @@ EOF
fi
}

function test_incompatible_python_disallow_native_rules_external_repos() {

mkdir external_repo
touch external_repo/WORKSPACE
touch external_repo/WORKSPACE.bzlmod
cat > external_repo/MODULE.bazel <<EOF
module(name="external_repo")
EOF

# There's special logic to handle targets at the root.
cat > external_repo/BUILD <<EOF
py_library(
name = "root",
visibility = ["//visibility:public"],
)
EOF
mkdir external_repo/pkg
cat > external_repo/pkg/BUILD <<EOF
py_library(
name = "pkg",
visibility = ["//visibility:public"],
)
EOF

touch bin.py
cat > BUILD <<EOF
load("@rules_python//python:py_binary.bzl", "py_binary")
py_binary(
name = "bin",
srcs = ["bin.py"],
deps = [
"@external_repo//:root",
"@external_repo//pkg:pkg",
],
)
package_group(
name = "allowed",
packages = [
"//__EXTERNAL_REPOS__/external_repo/...",
"//__EXTERNAL_REPOS__/external_repo~override/...",
"//__EXTERNAL_REPOS__/bazel_tools/...",
],
)
EOF
cat > MODULE.bazel <<EOF
module(name="python_version_test")
bazel_dep(name = "external_repo", version="0.0.0")
local_path_override(
module_name = "external_repo",
path = "$PWD/external_repo",
)
EOF

bazel build \
--incompatible_python_disallow_native_rules \
--python_native_rules_allowlist=//:allowed \
//:bin &> $TEST_log || fail "build failed"

}

run_suite "Tests for how the Python rules handle Python 2 vs Python 3"

0 comments on commit e5240a6

Please sign in to comment.