Skip to content

Commit 1533cd1

Browse files
rickeylevcopybara-github
authored andcommitted
python: make incompatible_python_disallow_native_rules work for top-level external repo targets
This basically makes it usable with the downloaded runtimes rules_python makes available, which reference their runtimes as e.g. `@python_3_11//:python`. 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 #17773 Closes #20516 PiperOrigin-RevId: 591053422 Change-Id: I7790df2db10278844ae2b36cfe671de03164972f
1 parent b6030fc commit 1533cd1

2 files changed

Lines changed: 87 additions & 2 deletions

File tree

src/main/starlark/builtins_bzl/common/python/common.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,10 @@ def check_native_allowed(ctx):
486486
# package_group doesn't allow @repo syntax, so we work around that
487487
# by prefixing external repos with a fake package path. This also
488488
# makes it easy to enable or disable all external repos.
489-
check_label = Label("@//__EXTERNAL_REPOS__/{workspace}/{package}".format(
489+
check_label = Label("@//__EXTERNAL_REPOS__/{workspace}{package}".format(
490490
workspace = ctx.label.workspace_name,
491-
package = ctx.label.package,
491+
# Prevent a label with trailing slash, which is malformed.
492+
package = "/" + ctx.label.package if ctx.label.package else "",
492493
))
493494
allowlist = ctx.attr._native_rules_allowlist
494495
if allowlist:

src/test/shell/bazel/python_version_test.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,4 +449,88 @@ EOF
449449
fi
450450
}
451451

452+
function test_incompatible_python_disallow_native_rules_external_repos() {
453+
454+
mkdir ../external_repo
455+
external_repo=$(cd ../external_repo && pwd)
456+
457+
touch $external_repo/WORKSPACE
458+
touch $external_repo/WORKSPACE.bzlmod
459+
cat > $external_repo/MODULE.bazel <<EOF
460+
module(name="external_repo")
461+
EOF
462+
463+
# There's special logic to handle targets at the root.
464+
cat > $external_repo/BUILD <<EOF
465+
py_library(
466+
name = "root",
467+
visibility = ["//visibility:public"],
468+
)
469+
EOF
470+
mkdir $external_repo/pkg
471+
cat > $external_repo/pkg/BUILD <<EOF
472+
py_library(
473+
name = "pkg",
474+
visibility = ["//visibility:public"],
475+
)
476+
EOF
477+
478+
touch bin.py
479+
cat > BUILD <<EOF
480+
load("@rules_python//python:py_binary.bzl", "py_binary")
481+
load("@rules_python//python:py_runtime.bzl", "py_runtime")
482+
load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
483+
484+
py_binary(
485+
name = "bin",
486+
srcs = ["bin.py"],
487+
deps = [
488+
"@external_repo//:root",
489+
"@external_repo//pkg:pkg",
490+
],
491+
)
492+
py_runtime(
493+
name = "runtime",
494+
interpreter_path = "/fakepython",
495+
python_version = "PY3",
496+
)
497+
py_runtime_pair(
498+
name = "pair",
499+
py3_runtime = ":runtime",
500+
)
501+
# A custom toolchain is used so this test is independent of
502+
# custom python toolchain setup the integration test performs
503+
toolchain(
504+
name = "py_toolchain",
505+
toolchain = ":pair",
506+
toolchain_type = "@rules_python//python:toolchain_type",
507+
)
508+
package_group(
509+
name = "allowed",
510+
packages = [
511+
"//__EXTERNAL_REPOS__/external_repo/...",
512+
"//__EXTERNAL_REPOS__/external_repo~override/...",
513+
"//__EXTERNAL_REPOS__/bazel_tools/...",
514+
##"//tools/python/windows...",
515+
],
516+
)
517+
EOF
518+
cat > MODULE.bazel <<EOF
519+
module(name="python_version_test")
520+
bazel_dep(name = "external_repo", version="0.0.0")
521+
local_path_override(
522+
module_name = "external_repo",
523+
# A relative path must be used to keep Windows CI happy.
524+
path = "../external_repo",
525+
)
526+
EOF
527+
528+
bazel build \
529+
--extra_toolchains=//:py_toolchain \
530+
--incompatible_python_disallow_native_rules \
531+
--python_native_rules_allowlist=//:allowed \
532+
//:bin &> $TEST_log || fail "build failed"
533+
534+
}
535+
452536
run_suite "Tests for how the Python rules handle Python 2 vs Python 3"

0 commit comments

Comments
 (0)