Skip to content

Commit

Permalink
fix(pip.parse): allow absolute path for python_interpreter; skip herm…
Browse files Browse the repository at this point in the history
…etic toolchain lookup when used (#1619)

The `python_interpreter` arg wasn't being properly handled in the bzlmod
code in two ways.

1. Lookup of a hermetic runtime wasn't being skipped when it was set.
The net effect was it
ignored the specified interpreter and would try to lookup a hermetic
interpreter using
the python version. To fix, add a check for python_interpreter to the
guard of the lookup.

2. Specifying an absolute path for the value wasn't being converted to a
`path` object,
which meant a plain string eventually made its way to some code
expecting a `path`
object. To fix, call `repository_ctx.path()` on the path to convert it
to a path object.

Fixes #1618
  • Loading branch information
rickeylev authored Dec 16, 2023
1 parent 847c03f commit b99027f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ A brief description of the categories of changes:
`experimental_target_platforms = ["all"]` to the `pip_parse` or the `bzlmod`
equivalent. This may help in cases where fetching wheels for a different
platform using `download_only = True` feature.
* (bzlmod pip.parse) The `pip.parse(python_interpreter)` arg now works for
specifying a local system interpreter.

[0.XX.0]: https://github.com/bazelbuild/rules_python/releases/tag/0.XX.0

Expand Down
7 changes: 6 additions & 1 deletion python/pip_install/pip_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def _resolve_python_interpreter(rctx):
Args:
rctx: Handle to the rule repository context.
Returns: Python interpreter path.
Returns:
`path` object, for the resolved path to the Python interpreter.
"""
python_interpreter = _get_python_interpreter_attr(rctx)

Expand All @@ -91,10 +93,13 @@ def _resolve_python_interpreter(rctx):
if os == WINDOWS_NAME:
python_interpreter = python_interpreter.realpath
elif "/" not in python_interpreter:
# It's a plain command, e.g. "python3", to look up in the environment.
found_python_interpreter = rctx.which(python_interpreter)
if not found_python_interpreter:
fail("python interpreter `{}` not found in PATH".format(python_interpreter))
python_interpreter = found_python_interpreter
else:
python_interpreter = rctx.path(python_interpreter)
return python_interpreter

def _get_xcode_location_cflags(rctx):
Expand Down
11 changes: 6 additions & 5 deletions python/private/bzlmod/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def _create_whl_repos(module_ctx, pip_attr, whl_map, whl_overrides):
# if we do not have the python_interpreter set in the attributes
# we programmatically find it.
hub_name = pip_attr.hub_name
if python_interpreter_target == None:
if python_interpreter_target == None and not pip_attr.python_interpreter:
python_name = "python_" + version_label(pip_attr.python_version, sep = "_")
if python_name not in INTERPRETER_LABELS.keys():
fail((
Expand Down Expand Up @@ -357,13 +357,14 @@ Targets from different hubs should not be used together.
"python_version": attr.string(
mandatory = True,
doc = """
The Python version to use for resolving the pip dependencies, in Major.Minor
format (e.g. "3.11"). Patch level granularity (e.g. "3.11.1") is not supported.
The Python version the dependencies are targetting, in Major.Minor format
(e.g., "3.11"). Patch level granularity (e.g. "3.11.1") is not supported.
If not specified, then the default Python version (as set by the root module or
rules_python) will be used.
The version specified here must have a corresponding `python.toolchain()`
configured.
If an interpreter isn't explicitly provided (using `python_interpreter` or
`python_interpreter_target`), then the version specified here must have
a corresponding `python.toolchain()` configured.
""",
),
"whl_modifications": attr.label_keyed_string_dict(
Expand Down

0 comments on commit b99027f

Please sign in to comment.