Skip to content

Commit cadfc9a

Browse files
committed
get remote execution working
1 parent 46b8f89 commit cadfc9a

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

python/private/interpreter.bzl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,24 @@ def _interpreter_impl(ctx):
3131
# re-exec. If it's not a recognized name, then they fail.
3232
if runtime.interpreter:
3333
executable = ctx.actions.declare_file(runtime.interpreter.basename)
34-
ctx.actions.symlink(output = executable, target_file = runtime.interpreter, is_executable = True)
34+
ctx.actions.expand_template(
35+
template = ctx.file._template,
36+
output = executable,
37+
substitutions = {
38+
"%target_file%": runtime.interpreter.short_path,
39+
},
40+
is_executable = True,
41+
)
3542
else:
3643
executable = ctx.actions.declare_symlink(paths.basename(runtime.interpreter_path))
3744
ctx.actions.symlink(output = executable, target_path = runtime.interpreter_path)
3845

3946
return [
4047
DefaultInfo(
4148
executable = executable,
42-
runfiles = ctx.runfiles([executable], transitive_files = runtime.files),
49+
runfiles = ctx.runfiles([executable], transitive_files = runtime.files).merge_all([
50+
ctx.attr._bash_runfiles[DefaultInfo].default_runfiles,
51+
]),
4352
),
4453
]
4554

@@ -51,5 +60,12 @@ interpreter = rule(
5160
"binary": attr.label(
5261
mandatory = True,
5362
),
63+
"_template": attr.label(
64+
default = "//python/private:interpreter_tmpl.sh",
65+
allow_single_file = True,
66+
),
67+
"_bash_runfiles": attr.label(
68+
default = "@bazel_tools//tools/bash/runfiles",
69+
),
5470
},
5571
)

python/private/interpreter_tmpl.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
# --- begin runfiles.bash initialization v3 ---
4+
# Copy-pasted from the Bazel Bash runfiles library v3.
5+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
6+
# shellcheck disable=SC1090
7+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
8+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
9+
source "$0.runfiles/$f" 2>/dev/null || \
10+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
11+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
12+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
13+
# --- end runfiles.bash initialization v3 ---
14+
15+
readonly TARGET_FILE="%target_file%"
16+
17+
MAIN_BIN="$(rlocation "${TARGET_FILE#*/}")"
18+
19+
exec "${MAIN_BIN}" "$@"

tests/interpreter/interpreter_test.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,43 @@ def test_interpreter_version(self):
3232
"""Validates that we can successfully execute arbitrary code from the CLI."""
3333
expected_version = os.environ["EXPECTED_INTERPRETER_VERSION"]
3434

35-
result = subprocess.check_output(
36-
[self.interpreter],
37-
text=True,
38-
input="\r".join(
39-
[
40-
"import sys",
41-
"v = sys.version_info",
42-
"print(f'version: {v.major}.{v.minor}')",
43-
]
44-
),
45-
).strip()
35+
try:
36+
result = subprocess.check_output(
37+
[self.interpreter],
38+
text=True,
39+
stderr=subprocess.STDOUT,
40+
input="\r".join(
41+
[
42+
"import sys",
43+
"v = sys.version_info",
44+
"print(f'version: {v.major}.{v.minor}')",
45+
]
46+
),
47+
).strip()
48+
except subprocess.CalledProcessError as error:
49+
print("OUTPUT:", error.stdout)
50+
raise
51+
4652
self.assertEqual(result, f"version: {expected_version}")
4753

4854
def test_json_tool(self):
4955
"""Validates that we can successfully invoke a module from the CLI."""
5056
# Pass unformatted JSON to the json.tool module.
51-
result = subprocess.check_output(
52-
[
53-
self.interpreter,
54-
"-m",
55-
"json.tool",
56-
],
57-
text=True,
58-
input='{"json":"obj"}',
59-
).strip()
57+
try:
58+
result = subprocess.check_output(
59+
[
60+
self.interpreter,
61+
"-m",
62+
"json.tool",
63+
],
64+
text=True,
65+
stderr=subprocess.STDOUT,
66+
input='{"json":"obj"}',
67+
).strip()
68+
except subprocess.CalledProcessError as error:
69+
print("OUTPUT:", error.stdout)
70+
raise
71+
6072
# Validate that we get formatted JSON back.
6173
self.assertEqual(result, '{\n "json": "obj"\n}')
6274

0 commit comments

Comments
 (0)