Skip to content

Commit 0abb1df

Browse files
committed
add package to PyInfo as an experiment to make the aggregation of the builders deterministic
1 parent 5b7fb40 commit 0abb1df

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

python/private/common.bzl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def create_py_info(
378378
implicit_pyc_files,
379379
implicit_pyc_source_files,
380380
imports,
381+
package = None,
381382
venv_symlinks = []):
382383
"""Create PyInfo provider.
383384
@@ -396,6 +397,7 @@ def create_py_info(
396397
implicit_pyc_files: {type}`depset[File]` Implicitly generated pyc files
397398
that a binary can choose to include.
398399
imports: depset of strings; the import path values to propagate.
400+
package: TODO
399401
venv_symlinks: {type}`list[VenvSymlinkEntry]` instances for
400402
symlinks to create in the consuming binary's venv.
401403
@@ -418,9 +420,15 @@ def create_py_info(
418420
py_info.merge_has_py2_only_sources(ctx.attr.srcs_version in ("PY2", "PY2ONLY"))
419421
py_info.merge_has_py3_only_sources(ctx.attr.srcs_version in ("PY3", "PY3ONLY"))
420422

423+
# First merge the third party deps
424+
# TODO @aignas 2025-06-05: refactor the code
425+
421426
for target in ctx.attr.deps:
422427
# PyInfo may not be present e.g. cc_library rules.
423428
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
429+
if not target[PyInfo].package:
430+
continue
431+
424432
py_info.merge(_get_py_info(target))
425433
else:
426434
# TODO(b/228692666): Remove this once non-PyInfo targets are no
@@ -433,6 +441,34 @@ def create_py_info(
433441
for target in ctx.attr.pyi_deps:
434442
# PyInfo may not be present e.g. cc_library rules.
435443
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
444+
if not target[PyInfo].package:
445+
continue
446+
447+
py_info.merge(_get_py_info(target))
448+
449+
# Now proceed with the first party deps
450+
451+
for target in ctx.attr.deps:
452+
# PyInfo may not be present e.g. cc_library rules.
453+
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
454+
if target[PyInfo].package:
455+
continue
456+
457+
py_info.merge(_get_py_info(target))
458+
else:
459+
# TODO(b/228692666): Remove this once non-PyInfo targets are no
460+
# longer supported in `deps`.
461+
files = target.files.to_list()
462+
for f in files:
463+
if f.extension == "py":
464+
py_info.transitive_sources.add(f)
465+
py_info.merge_uses_shared_libraries(cc_helper.is_valid_shared_library_artifact(f))
466+
for target in ctx.attr.pyi_deps:
467+
# PyInfo may not be present e.g. cc_library rules.
468+
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
469+
if target[PyInfo].package:
470+
continue
471+
436472
py_info.merge(_get_py_info(target))
437473

438474
deps_transitive_sources = py_info.transitive_sources.build()
@@ -457,7 +493,7 @@ def create_py_info(
457493
if py_info.get_uses_shared_libraries():
458494
break
459495

460-
return py_info.build(), deps_transitive_sources, py_info.build_builtin_py_info()
496+
return py_info.build(package), deps_transitive_sources, py_info.build_builtin_py_info()
461497

462498
def _get_py_info(target):
463499
return target[PyInfo] if PyInfo in target or BuiltinPyInfo == None else target[BuiltinPyInfo]

python/private/py_info.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def _PyInfo_init(
107107
has_py2_only_sources = False,
108108
has_py3_only_sources = False,
109109
direct_pyc_files = depset(),
110+
package = None,
110111
transitive_pyc_files = depset(),
111112
transitive_implicit_pyc_files = depset(),
112113
transitive_implicit_pyc_source_files = depset(),
@@ -142,6 +143,7 @@ def _PyInfo_init(
142143
"has_py2_only_sources": has_py2_only_sources,
143144
"has_py3_only_sources": has_py2_only_sources,
144145
"imports": imports,
146+
"package": package,
145147
"transitive_implicit_pyc_files": transitive_implicit_pyc_files,
146148
"transitive_implicit_pyc_source_files": transitive_implicit_pyc_source_files,
147149
"transitive_original_sources": transitive_original_sources,
@@ -218,6 +220,7 @@ Python targets. These are accumulated from the transitive `deps`.
218220
The order of the depset is not guaranteed and may be changed in the future. It
219221
is recommended to use `default` order (the default).
220222
""",
223+
"package": "TODO",
221224
"transitive_implicit_pyc_files": """
222225
:type: depset[File]
223226
@@ -625,11 +628,12 @@ def _PyInfoBuilder_merge_targets(self, targets):
625628
self.merge_target(t)
626629
return self
627630

628-
def _PyInfoBuilder_build(self):
631+
def _PyInfoBuilder_build(self, package = None):
629632
"""Builds into a {obj}`PyInfo` object.
630633
631634
Args:
632635
self: implicitly added.
636+
package: TODO
633637
634638
Returns:
635639
{type}`PyInfo`
@@ -639,6 +643,7 @@ def _PyInfoBuilder_build(self):
639643
direct_original_sources = self.direct_original_sources.build(),
640644
direct_pyc_files = self.direct_pyc_files.build(),
641645
direct_pyi_files = self.direct_pyi_files.build(),
646+
package = package,
642647
transitive_implicit_pyc_files = self.transitive_implicit_pyc_files.build(),
643648
transitive_implicit_pyc_source_files = self.transitive_implicit_pyc_source_files.build(),
644649
transitive_original_sources = self.transitive_original_sources.build(),

python/private/py_library.bzl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ def py_library_impl(ctx, *, semantics):
159159
imports = []
160160
venv_symlinks = []
161161

162+
# TODO @aignas 2025-06-05: refactor code
163+
package = None
164+
dist_info_metadata = _get_distinfo_metadata(ctx)
165+
if dist_info_metadata:
166+
# in order to be able to have replacements in the venv, we have to add a
167+
# third value into the venv_symlinks, which would be the normalized
168+
# package name. This allows us to ensure that we can replace the `dist-info`
169+
# directories by checking if the package key is there.
170+
dist_info_dir = paths.basename(dist_info_metadata.dirname)
171+
package, _, _suffix = dist_info_dir.rpartition(".dist-info")
172+
162173
imports, venv_symlinks = _get_imports_and_venv_symlinks(ctx, semantics)
163174

164175
cc_info = semantics.get_cc_info_for_library(ctx)
@@ -169,6 +180,7 @@ def py_library_impl(ctx, *, semantics):
169180
required_pyc_files = required_pyc_files,
170181
implicit_pyc_files = implicit_pyc_files,
171182
implicit_pyc_source_files = implicit_pyc_source_files,
183+
package = package,
172184
imports = imports,
173185
venv_symlinks = venv_symlinks,
174186
)
@@ -222,7 +234,7 @@ def _get_distinfo_metadata(ctx):
222234

223235
def _get_imports_and_venv_symlinks(ctx, semantics):
224236
imports = depset()
225-
venv_symlinks = depset()
237+
venv_symlinks = []
226238
if VenvsSitePackages.is_enabled(ctx):
227239
venv_symlinks = _get_venv_symlinks(ctx)
228240
else:

0 commit comments

Comments
 (0)