Skip to content

Commit 59bb4a8

Browse files
authored
fix: add missing +x to runtime env toolchain interpreter script (#2086)
The `runtime_env_toolchain_interpreter.sh` file was missing the executable bit, which prevented the file from actually be runnable later. To fix, just `chmod +x` it. I also added tests to actually run using it and verify that it is the toolchain used by the test. Fixes #2085
1 parent eef1d81 commit 59bb4a8

File tree

6 files changed

+114
-10
lines changed

6 files changed

+114
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ A brief description of the categories of changes:
4141
([#2064](https://github.com/bazelbuild/rules_python/issues/2064)).
4242
* (pip) Fixed pypi parse_simpleapi_html function for feeds with package metadata
4343
containing ">" sign
44+
* (toolchains) Added missing executable permission to
45+
`//python/runtime_env_toolchains` interpreter script so that it is runnable.
46+
([#2085](https://github.com/bazelbuild/rules_python/issues/2085)).
4447

4548
### Added
4649
* (rules) `PYTHONSAFEPATH` is inherited from the calling environment to allow

python/private/runtime_env_toolchain_interpreter.sh

100644100755
File mode changed.

tests/runtime_env_toolchain/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("//tests/support:sh_py_run_test.bzl", "py_reconfig_test")
1516
load(":runtime_env_toolchain_tests.bzl", "runtime_env_toolchain_test_suite")
1617

1718
runtime_env_toolchain_test_suite(name = "runtime_env_toolchain_tests")
19+
20+
py_reconfig_test(
21+
name = "toolchain_runs_test",
22+
srcs = ["toolchain_runs_test.py"],
23+
data = [
24+
"//tests/support:current_build_settings",
25+
],
26+
extra_toolchains = [
27+
"//python/runtime_env_toolchains:all",
28+
# Necessary for RBE CI
29+
"//tests/cc:all",
30+
],
31+
main = "toolchain_runs_test.py",
32+
deps = ["//python/runfiles"],
33+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import pathlib
3+
import platform
4+
import unittest
5+
6+
from python.runfiles import runfiles
7+
8+
9+
class RunTest(unittest.TestCase):
10+
def test_ran(self):
11+
rf = runfiles.Create()
12+
settings_path = rf.Rlocation(
13+
"rules_python/tests/support/current_build_settings.json"
14+
)
15+
settings = json.loads(pathlib.Path(settings_path).read_text())
16+
if platform.system() == "Windows":
17+
self.assertEqual(
18+
"/_magic_pyruntime_sentinel_do_not_use", settings["interpreter_path"]
19+
)
20+
else:
21+
self.assertIn(
22+
"runtime_env_toolchain_interpreter.sh",
23+
settings["interpreter"]["short_path"],
24+
)
25+
26+
27+
if __name__ == "__main__":
28+
unittest.main()

tests/support/BUILD.bazel

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020

2121
load("//python:py_runtime.bzl", "py_runtime")
2222
load("//python:py_runtime_pair.bzl", "py_runtime_pair")
23+
load(":sh_py_run_test.bzl", "current_build_settings")
24+
25+
package(
26+
default_visibility = ["//:__subpackages__"],
27+
)
2328

2429
platform(
2530
name = "mac",
@@ -104,3 +109,7 @@ toolchain(
104109
toolchain = ":platform_runtime_pair",
105110
toolchain_type = "//python:toolchain_type",
106111
)
112+
113+
current_build_settings(
114+
name = "current_build_settings",
115+
)

tests/support/sh_py_run_test.bzl

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@ without the overhead of a bazel-in-bazel integration test.
1919

2020
load("//python:py_binary.bzl", "py_binary")
2121
load("//python:py_test.bzl", "py_test")
22+
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE") # buildifier: disable=bzl-visibility
2223

2324
def _perform_transition_impl(input_settings, attr):
2425
settings = dict(input_settings)
2526
settings["//command_line_option:build_python_zip"] = attr.build_python_zip
2627
if attr.bootstrap_impl:
2728
settings["//python/config_settings:bootstrap_impl"] = attr.bootstrap_impl
29+
if attr.extra_toolchains:
30+
settings["//command_line_option:extra_toolchains"] = attr.extra_toolchains
31+
else:
32+
settings["//command_line_option:extra_toolchains"] = input_settings["//command_line_option:extra_toolchains"]
2833
return settings
2934

3035
_perform_transition = transition(
3136
implementation = _perform_transition_impl,
3237
inputs = [
3338
"//python/config_settings:bootstrap_impl",
39+
"//command_line_option:extra_toolchains",
3440
],
3541
outputs = [
3642
"//command_line_option:build_python_zip",
43+
"//command_line_option:extra_toolchains",
3744
"//python/config_settings:bootstrap_impl",
3845
],
3946
)
@@ -79,17 +86,27 @@ def _py_reconfig_impl(ctx):
7986
]
8087

8188
def _make_reconfig_rule(**kwargs):
89+
attrs = {
90+
"bootstrap_impl": attr.string(),
91+
"build_python_zip": attr.string(default = "auto"),
92+
"env": attr.string_dict(),
93+
"extra_toolchains": attr.string_list(
94+
doc = """
95+
Value for the --extra_toolchains flag.
96+
97+
NOTE: You'll likely have to also specify //tests/cc:all (or some CC toolchain)
98+
to make the RBE presubmits happy, which disable auto-detection of a CC
99+
toolchain.
100+
""",
101+
),
102+
"target": attr.label(executable = True, cfg = "target"),
103+
"_allowlist_function_transition": attr.label(
104+
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
105+
),
106+
}
82107
return rule(
83108
implementation = _py_reconfig_impl,
84-
attrs = {
85-
"bootstrap_impl": attr.string(),
86-
"build_python_zip": attr.string(default = "auto"),
87-
"env": attr.string_dict(),
88-
"target": attr.label(executable = True, cfg = "target"),
89-
"_allowlist_function_transition": attr.label(
90-
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
91-
),
92-
},
109+
attrs = attrs,
93110
cfg = _perform_transition,
94111
**kwargs
95112
)
@@ -106,7 +123,8 @@ def py_reconfig_test(*, name, **kwargs):
106123
**kwargs: kwargs to pass along to _py_reconfig_test and py_test.
107124
"""
108125
reconfig_kwargs = {}
109-
reconfig_kwargs["bootstrap_impl"] = kwargs.pop("bootstrap_impl")
126+
reconfig_kwargs["bootstrap_impl"] = kwargs.pop("bootstrap_impl", None)
127+
reconfig_kwargs["extra_toolchains"] = kwargs.pop("extra_toolchains", None)
110128
reconfig_kwargs["env"] = kwargs.get("env")
111129
inner_name = "_{}_inner" + name
112130
_py_reconfig_test(
@@ -147,3 +165,33 @@ def sh_py_run_test(*, name, sh_src, py_src, **kwargs):
147165
main = py_src,
148166
tags = ["manual"],
149167
)
168+
169+
def _current_build_settings_impl(ctx):
170+
info = ctx.actions.declare_file(ctx.label.name + ".json")
171+
toolchain = ctx.toolchains[TARGET_TOOLCHAIN_TYPE]
172+
runtime = toolchain.py3_runtime
173+
files = [info]
174+
ctx.actions.write(
175+
output = info,
176+
content = json.encode({
177+
"interpreter": {
178+
"short_path": runtime.interpreter.short_path if runtime.interpreter else None,
179+
},
180+
"interpreter_path": runtime.interpreter_path,
181+
}),
182+
)
183+
return [DefaultInfo(
184+
files = depset(files),
185+
)]
186+
187+
current_build_settings = rule(
188+
doc = """
189+
Writes information about the current build config to JSON for testing.
190+
191+
This is so tests can verify information about the build config used for them.
192+
""",
193+
implementation = _current_build_settings_impl,
194+
toolchains = [
195+
TARGET_TOOLCHAIN_TYPE,
196+
],
197+
)

0 commit comments

Comments
 (0)