Skip to content

Commit ddcf738

Browse files
committed
incorporate feedback
1 parent 5eae601 commit ddcf738

File tree

4 files changed

+37
-47
lines changed

4 files changed

+37
-47
lines changed

python/private/interpreter.bzl

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
1818
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
1919
load(":sentinel.bzl", "SentinelInfo")
2020
load(":toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
21-
load(":py_executable.bzl", "relative_path", "runfiles_root_path")
2221

2322
def _interpreter_binary_impl(ctx):
2423
if SentinelInfo in ctx.attr.binary:
@@ -31,30 +30,28 @@ def _interpreter_binary_impl(ctx):
3130
# because of things like pyenv: they use $0 to determine what to
3231
# re-exec. If it's not a recognized name, then they fail.
3332
if runtime.interpreter:
34-
# Option 1:
35-
# Works locally, but not remotely.
36-
#executable = ctx.actions.declare_file(runtime.interpreter.basename)
37-
#ctx.actions.symlink(output = executable, target_file = runtime.interpreter)
38-
39-
# Option 2:
40-
# Works locally, and remotely, but not on Windows.
33+
# In order for this to work both locally and remotely, we create a
34+
# shell script here that re-exec's into the real interpreter. Ideally,
35+
# we'd just use a symlink, but that breaks under certain conditions. If
36+
# we use a ctx.actions.symlink(target=...) then it fails under remote
37+
# execution. If we use ctx.actions.symlink(target_path=...) then it
38+
# behaves differently inside the runfiles tree and outside the runfiles
39+
# tree.
40+
#
41+
# This currently does not work on Windows. Need to find a way to enable
42+
# that.
4143
executable = ctx.actions.declare_file(runtime.interpreter.basename)
4244
ctx.actions.expand_template(
4345
template = ctx.file._template,
4446
output = executable,
4547
substitutions = {
48+
# Since we never invoke this rule from within the interpreter's
49+
# own repository, the short_path here should give us a
50+
# predictable path of "../<repo>/<path within repo>".
4651
"%target_file%": runtime.interpreter.short_path,
4752
},
4853
is_executable = True,
4954
)
50-
51-
# Option 3:
52-
# Works locally and remotely, but only in runfiles, doesn't work via "bazel run".
53-
#executable = ctx.actions.declare_symlink(runtime.interpreter.basename)
54-
#ctx.actions.symlink(output = executable, target_path = relative_path(
55-
# from_ = paths.dirname(runfiles_root_path(ctx, executable.short_path)),
56-
# to = runfiles_root_path(ctx, runtime.interpreter.short_path),
57-
#))
5855
else:
5956
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path))
6057
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path)

python/private/interpreter_tmpl.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
1414

1515
readonly TARGET_FILE="%target_file%"
1616

17+
# Strip the leading "../" from "../<repo>/<path within repo>" so that we can do
18+
# a runfiles lookup.
1719
MAIN_BIN="$(rlocation "${TARGET_FILE#*/}")"
1820

1921
exec "${MAIN_BIN}" "$@"

python/private/py_executable.bzl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ def _create_executable(
447447
)
448448

449449
def _create_zip_main(ctx, *, stage2_bootstrap, runtime_details, venv):
450-
python_binary = runfiles_root_path(ctx, venv.interpreter.short_path)
450+
python_binary = _runfiles_root_path(ctx, venv.interpreter.short_path)
451451
python_binary_actual = venv.interpreter_actual_path
452452

453453
# The location of this file doesn't really matter. It's added to
@@ -522,7 +522,7 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
522522

523523
if not venvs_use_declare_symlink_enabled:
524524
if runtime.interpreter:
525-
interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path)
525+
interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
526526
else:
527527
interpreter_actual_path = runtime.interpreter_path
528528

@@ -543,11 +543,11 @@ def _create_venv(ctx, output_prefix, imports, runtime_details):
543543
# may choose to write what symlink() points to instead.
544544
interpreter = ctx.actions.declare_symlink("{}/bin/{}".format(venv, py_exe_basename))
545545

546-
interpreter_actual_path = runfiles_root_path(ctx, runtime.interpreter.short_path)
546+
interpreter_actual_path = _runfiles_root_path(ctx, runtime.interpreter.short_path)
547547
rel_path = relative_path(
548548
# dirname is necessary because a relative symlink is relative to
549549
# the directory the symlink resides within.
550-
from_ = paths.dirname(runfiles_root_path(ctx, interpreter.short_path)),
550+
from_ = paths.dirname(_runfiles_root_path(ctx, interpreter.short_path)),
551551
to = interpreter_actual_path,
552552
)
553553

@@ -646,7 +646,7 @@ def _create_stage2_bootstrap(
646646
)
647647
return output
648648

649-
def runfiles_root_path(ctx, short_path):
649+
def _runfiles_root_path(ctx, short_path):
650650
"""Compute a runfiles-root relative path from `File.short_path`
651651
652652
Args:
@@ -676,7 +676,7 @@ def _create_stage1_bootstrap(
676676
runtime = runtime_details.effective_runtime
677677

678678
if venv:
679-
python_binary_path = runfiles_root_path(ctx, venv.interpreter.short_path)
679+
python_binary_path = _runfiles_root_path(ctx, venv.interpreter.short_path)
680680
else:
681681
python_binary_path = runtime_details.executable_interpreter_path
682682

tests/interpreter/BUILD.bazel

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,38 @@
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")
15+
load(":interpreter_tests.bzl", "py_reconfig_interpreter_tests", "PYTHON_VERSIONS_TO_TEST")
1616

17-
PYTHON_VERSIONS_TO_TEST = (
18-
"3.10",
19-
"3.11",
20-
"3.12",
21-
)
22-
23-
[py_reconfig_test(
24-
name = "python_version_%s_test" % python_version,
17+
py_reconfig_interpreter_tests(
18+
name = "interpreter_version_test",
19+
python_versions = PYTHON_VERSIONS_TO_TEST,
20+
# Both the interpreter and the test itself are expected to run under
21+
# the same version.
22+
expected_interpreter_version = None,
2523
srcs = ["interpreter_test.py"],
2624
data = [
2725
"//python/bin:python",
2826
],
2927
env = {
30-
# Both the interpreter and the test itself are expected to run under
31-
# the same version.
32-
"EXPECTED_INTERPRETER_VERSION": python_version,
33-
"EXPECTED_SELF_VERSION": python_version,
3428
"PYTHON_BIN": "$(rootpath //python/bin:python)",
3529
},
3630
main = "interpreter_test.py",
37-
python_version = python_version,
38-
) for python_version in PYTHON_VERSIONS_TO_TEST]
31+
)
3932

40-
[py_reconfig_test(
41-
name = "python_src_%s_test" % python_version,
33+
py_reconfig_interpreter_tests(
34+
name = "python_src_test",
4235
srcs = ["interpreter_test.py"],
4336
data = [
4437
"//python/bin:python",
4538
],
4639
env = {
47-
# Since we're grabbing the interpreter from a binary with a fixed
48-
# version, we expect to always see that version. It doesn't matter what
49-
# Python version the test itself is running with.
50-
"EXPECTED_INTERPRETER_VERSION": "3.11",
51-
# The test itself is still running under different Python versions.
52-
"EXPECTED_SELF_VERSION": python_version,
5340
"PYTHON_BIN": "$(rootpath //python/bin:python)",
5441
},
42+
python_versions = PYTHON_VERSIONS_TO_TEST,
43+
# Since we're grabbing the interpreter from a binary with a fixed
44+
# version, we expect to always see that version. It doesn't matter what
45+
# Python version the test itself is running with.
46+
expected_interpreter_version = "3.11",
5547
main = "interpreter_test.py",
5648
python_src = "//tools/publish:twine",
57-
python_version = python_version,
58-
) for python_version in PYTHON_VERSIONS_TO_TEST]
49+
)

0 commit comments

Comments
 (0)