8000 fix(bzlmod): Fixing Windows Python Interpreter symlink issues (#1265) · cflewis/rules_python@3903d1a · GitHub
[go: up one dir, main page]

Skip to content

Commit 3903d1a

Browse files
fix(bzlmod): Fixing Windows Python Interpreter symlink issues (bazel-contrib#1265)
When using Windows you cannot run the symlink directly. Because of how symlinks work in Windows we need to path realpath resolve the link. Unlike Linux and OSX we cannot execute the Python symlink directly. Windows throws an error "-1073741515", when we execute the symlink directory. This error means that the Python binary cannot find dlls. This is because the symlink that we create is not in the same folder as the dlls. --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
1 parent 68db955 commit 3903d1a

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

python/pip_install/pip_repository.bzl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
""
1616

1717
load("//python:repositories.bzl", "get_interpreter_dirname", "is_standalone_interpreter")
18+
load("//python:versions.bzl", "WINDOWS_NAME")
1819
load("//python/pip_install:repositories.bzl", "all_requirements")
1920
load("//python/pip_install:requirements_parser.bzl", parse_requirements = "parse")
2021
load("//python/pip_install/private:srcs.bzl", "PIP_INSTALL_PY_SRCS")
22+
load("//python/private:toolchains_repo.bzl", "get_host_os_arch")
2123

2224
CPPFLAGS = "CPPFLAGS"
2325

@@ -72,8 +74,16 @@ def _resolve_python_interpreter(rctx):
7274
python_interpreter = _get_python_interpreter_attr(rctx)
7375

7476
if rctx.attr.python_interpreter_target != None:
75-
target = rctx.attr.python_interpreter_target
76-
python_interpreter = rctx.path(target)
77+
python_interpreter = rctx.path(rctx.attr.python_interpreter_target)
78+
79+
# If we have @@ we have bzlmod so we need to hand Windows differently.
80+
if str(Label("//:unused")).startswith("@@"):
81+
(os, _) = get_host_os_arch(rctx)
82+
83+
# On Windows, the symlink doesn't work because Windows attempts to find
84+
# Python DLLs where the symlink is, not where the symlink points.
85+
if os == WINDOWS_NAME:
86+
python_interpreter = python_interpreter.realpath
7787
elif "/" not in python_interpreter:
7888
found_python_interpreter = rctx.which(python_interpreter)
7989
if not found_python_interpreter:
@@ -670,7 +680,6 @@ py_binary(
670680

671681
def _whl_library_impl(rctx):
672682
python_interpreter = _resolve_python_interpreter(rctx)
673-
674683
args = [
675684
python_interpreter,
676685
"-m",
@@ -699,7 +708,7 @@ def _whl_library_impl(rctx):
699708
)
700709

701710
if result.return_code:
702-
fail("whl_library %s failed: %s (%s)" % (rctx.attr.name, result.stdout, result.stderr))
711+
fail("whl_library %s failed: %s (%s) error code: '%s'" % (rctx.attr.name, result.stdout, result.stderr, result.return_code))
703712

704713
return
705714

0 commit comments

Comments
 (0)
0