Skip to content

Commit

Permalink
Fix(bzlmod): Windows Python Interpreter symlink issues
Browse files Browse the repository at this point in the history
When using Windows you cannot run the symlink directly.

Because of how symlinks work in Windows we need to use the path
realink.
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.

This commit introduces code that resolves the actual location of the
interpreter using the path realink when we are running bzlmod and
Windows.
  • Loading branch information
chrislovecnm committed Jun 13, 2023
1 parent 2c28e61 commit 0f08282
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions python/pip_install/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
""

load("//python:repositories.bzl", "get_interpreter_dirname", "is_standalone_interpreter")
load("//python:versions.bzl", "WINDOWS_NAME")
load("//python/pip_install:repositories.bzl", "all_requirements")
load("//python/pip_install:requirements_parser.bzl", parse_requirements = "parse")
load("//python/pip_install/private:srcs.bzl", "PIP_INSTALL_PY_SRCS")
load("//python/private:toolchains_repo.bzl", "get_host_os_arch")

CPPFLAGS = "CPPFLAGS"

Expand Down Expand Up @@ -72,8 +74,14 @@ def _resolve_python_interpreter(rctx):
python_interpreter = _get_python_interpreter_attr(rctx)

if rctx.attr.python_interpreter_target != None:
target = rctx.attr.python_interpreter_target
python_interpreter = rctx.path(target)
python_interpreter = rctx.path(rctx.attr.python_interpreter_target)
# If we have @@ we have bzlmod so we need to hand Windows differently.
if str(Label("//:unused")).startswith("@@"):
(os, _) = get_host_os_arch(rctx)
# If we have Windows the symlink will not work directly and we need
# to resolve the realpath.
if os == WINDOWS_NAME:
python_interpreter = python_interpreter.realpath
elif "/" not in python_interpreter:
found_python_interpreter = rctx.which(python_interpreter)
if not found_python_interpreter:
Expand Down Expand Up @@ -670,7 +678,6 @@ py_binary(

def _whl_library_impl(rctx):
python_interpreter = _resolve_python_interpreter(rctx)

args = [
python_interpreter,
"-m",
Expand Down Expand Up @@ -699,7 +706,7 @@ def _whl_library_impl(rctx):
)

if result.return_code:
fail("whl_library %s failed: %s (%s)" % (rctx.attr.name, result.stdout, result.stderr))
fail("whl_library %s failed: %s (%s) error code: '%s'" % (rctx.attr.name, result.stdout, result.stderr, result.return_code))

return

Expand Down

0 comments on commit 0f08282

Please sign in to comment.