Skip to content

Commit

Permalink
fix: keep import path values if Bazel-builtin PyInfo is removed (#2415)
Browse files Browse the repository at this point in the history
The collect_imports() function added import strings from BuiltinPyInfo
if it was non-None.
However, operator precedence caused the `if-else` ternary to ignore both
list comprehensions
(one for PyInfo and one for BuiltinPyInfo) if BuiltinPyInfo was None.

To fix, I rewrote the function as a regular for loop to eliminate the
ambiguous looking
ternary expression.

Fixes: #2414
  • Loading branch information
rickeylev authored Nov 16, 2024
1 parent 155efce commit b304fc6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ Unreleased changes template.

{#v0-0-0-fixed}
### Fixed
* Nothing yet.
* (rules) Don't drop custom import paths if Bazel-builtin PyInfo is removed.
([2414](https://github.com/bazelbuild/rules_python/issues/2414)).

{#v0-0-0-added}
### Added
Expand Down
25 changes: 16 additions & 9 deletions python/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,22 @@ def filter_to_py_srcs(srcs):
return [f for f in srcs if f.extension == "py"]

def collect_imports(ctx, semantics):
return depset(direct = semantics.get_imports(ctx), transitive = [
dep[PyInfo].imports
for dep in ctx.attr.deps
if PyInfo in dep
] + [
dep[BuiltinPyInfo].imports
for dep in ctx.attr.deps
if BuiltinPyInfo in dep
] if BuiltinPyInfo != None else [])
"""Collect the direct and transitive `imports` strings.
Args:
ctx: {type}`ctx` the current target ctx
semantics: semantics object for fetching direct imports.
Returns:
{type}`depset[str]` of import paths
"""
transitive = []
for dep in ctx.attr.deps:
if PyInfo in dep:
transitive.append(dep[PyInfo].imports)
if BuiltinPyInfo != None and BuiltinPyInfo in dep:
transitive.append(dep[BuiltinPyInfo].imports)
return depset(direct = semantics.get_imports(ctx), transitive = transitive)

def collect_runfiles(ctx, files = depset()):
"""Collects the necessary files from the rule's context.
Expand Down

0 comments on commit b304fc6

Please sign in to comment.