Skip to content

Commit ebd779e

Browse files
authored
fix(toolchains): register py cc toolchain for workspace builds (#1670)
The workspace `python_register_toolchains()` function only registered the plain Python toolchain by its specific name. The py cc toolchain wasn't also being named. This meant things like `//python/cc:current_py_cc_headers` couldn't find their toolchain. Bzlmod doesn't have this problem because it uses the `:all` pattern to register everything. To fix, also register the py cc toolchain where the plain Python toolchain is registered, which makes it available. Fixes #1669
1 parent 8aaaf93 commit ebd779e

File tree

10 files changed

+110
-2
lines changed

10 files changed

+110
-2
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/pip_repository_entry_points
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/pip_repository_entry_points
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/pip_repository_entry_points,tests/integration/py_cc_toolchain_registered
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/dupe_requirements,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/ignore_root_user_error,tests/integration/pip_repository_entry_points,tests/integration/py_cc_toolchain_registered
99

1010
test --test_output=errors
1111

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ A brief description of the categories of changes:
6464
* (coverage): coverage reports are now created when the version-aware
6565
rules are used.
6666
([#1600](https://github.com/bazelbuild/rules_python/issues/1600))
67+
* (toolchains) Workspace builds register the py cc toolchain (bzlmod already
68+
was). This makes e.g. `//python/cc:current_py_cc_headers` Just Work.
69+
([#1669](https://github.com/bazelbuild/rules_python/issues/1669))
6770

6871
### Added
6972

python/repositories.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,10 @@ def python_register_toolchains(
584584
toolchain_repo_name = toolchain_repo_name,
585585
platform = platform,
586586
))
587+
native.register_toolchains("@{toolchain_repo_name}//:{platform}_py_cc_toolchain".format(
588+
toolchain_repo_name = toolchain_repo_name,
589+
platform = platform,
590+
))
587591

588592
toolchain_aliases(
589593
name = name,

tests/cc/current_py_cc_headers/current_py_cc_headers_tests.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ def _test_current_toolchain_headers_impl(env, target):
6262

6363
_tests.append(_test_current_toolchain_headers)
6464

65+
def _test_toolchain_is_registered_by_default(name):
66+
analysis_test(
67+
name = name,
68+
impl = _test_toolchain_is_registered_by_default_impl,
69+
target = "//python/cc:current_py_cc_headers",
70+
)
71+
72+
def _test_toolchain_is_registered_by_default_impl(env, target):
73+
env.expect.that_target(target).has_provider(CcInfo)
74+
75+
_tests.append(_test_toolchain_is_registered_by_default)
76+
6577
def current_py_cc_headers_test_suite(name):
6678
test_suite(
6779
name = name,

tests/integration/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ rules_python_integration_test(
9191
bzlmod = False,
9292
workspace_path = "ignore_root_user_error",
9393
)
94+
95+
rules_python_integration_test(
96+
name = "py_cc_toolchain_registered_test",
97+
)
98+
99+
rules_python_integration_test(
100+
name = "py_cc_toolchain_registered_workspace_test",
101+
bzlmod = False,
102+
workspace_path = "py_cc_toolchain_registered",
103+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This aids debugging on failure
2+
build --toolchain_resolution_debug=python
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load(":defs.bzl", "py_cc_toolchain_available_test")
16+
17+
# Simple test to verify that the py_cc_toolchain is registered and available
18+
# by default (for bzlmod) and when users setup a hermetic toolchain (workspace)
19+
py_cc_toolchain_available_test(name = "py_cc_toolchain_available_test")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module(name = "py_cc_toolchain_registered")
2+
3+
bazel_dep(name = "rules_python", version = "0.0.0")
4+
local_path_override(
5+
module_name = "rules_python",
6+
path = "../../..",
7+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
local_repository(
2+
name = "rules_python",
3+
path = "../../..",
4+
)
5+
6+
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
7+
8+
py_repositories()
9+
10+
python_register_toolchains(
11+
name = "python_3_11",
12+
python_version = "3.11",
13+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2022 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Defs to implement tests."""
15+
16+
def _py_cc_toolchain_available_test_impl(ctx):
17+
toolchain = ctx.toolchains["@rules_python//python/cc:toolchain_type"]
18+
19+
if toolchain == None:
20+
fail("expected @rules_python//python/cc:toolchain_type toolchain " +
21+
"to be found, but it was not found")
22+
23+
executable = ctx.actions.declare_file(ctx.label.name)
24+
ctx.actions.write(executable, "# no-op file", is_executable = True)
25+
return [DefaultInfo(
26+
executable = executable,
27+
)]
28+
29+
py_cc_toolchain_available_test = rule(
30+
implementation = _py_cc_toolchain_available_test_impl,
31+
toolchains = [
32+
config_common.toolchain_type(
33+
"@rules_python//python/cc:toolchain_type",
34+
mandatory = False,
35+
),
36+
],
37+
test = True,
38+
)

0 commit comments

Comments
 (0)