@@ -101,7 +101,7 @@ def whl_library_targets(
101101 **kwargs: Extra args passed to the {obj}`whl_library_deps_targets` and {obj}`whl_library_srcs`.
102102 """
103103 create_extra_targets = bool (requires_dist or group_name ) and dep_template
104- whl_library_srcs (
104+ wrapper_srcs = whl_library_srcs (
105105 name = name ,
106106 sdist_filename = sdist_filename ,
107107 data_exclude = data_exclude ,
@@ -133,6 +133,7 @@ def whl_library_targets(
133133 dep_template = dep_template , # only needed if requires_dist is present
134134 repo = None , # set aliases in the same repo
135135 aliases = {},
136+ srcs = wrapper_srcs ,
136137 ** kwargs
137138 )
138139
@@ -193,9 +194,14 @@ def whl_library_srcs(
193194 pkg_name: {type}`str` The label name to use for the py_library target.
194195 native: {type}`native` The native struct for overriding in tests.
195196 rules: {type}`struct` A struct with references to rules for creating targets.
197+
198+ Returns:
199+ The source labels attached to the generated `py_library`, or `None` when
200+ `rules` does not define `py_library`.
196201 """
197202 tags = sorted (tags )
198203 data = [] + data
204+ wrapper_srcs = None
199205
200206 bins_for_data_label = []
201207
@@ -301,6 +307,7 @@ def whl_library_srcs(
301307 # pure-Python code, e.g. pymssql, which is written in Cython.
302308 allow_empty = True ,
303309 )
310+ wrapper_srcs = [pkg_name ] if srcs else []
304311
305312 # NOTE: pyi files should probably be excluded because they're carried
306313 # by the pyi_srcs attribute. However, historical behavior included
@@ -328,13 +335,19 @@ def whl_library_srcs(
328335 )
329336
330337 if not enable_implicit_namespace_pkgs :
338+ generated_namespace_package_files = rules .create_inits (
339+ srcs = srcs + data + pyi_srcs ,
340+ ignored_dirnames = [], # If you need to ignore certain folders, you can patch rules_python here to do so.
341+ root = "site-packages" ,
342+ )
343+ if not wrapper_srcs and generated_namespace_package_files :
344+ wrapper_srcs = select ({
345+ _IS_VENV_SITE_PACKAGES_YES : [],
346+ "//conditions:default" : [pkg_name ],
347+ })
331348 generated_namespace_package_files = select ({
332349 _IS_VENV_SITE_PACKAGES_YES : [],
333- "//conditions:default" : rules .create_inits (
334- srcs = srcs + data + pyi_srcs ,
335- ignored_dirnames = [], # If you need to ignore certain folders, you can patch rules_python here to do so.
336- root = "site-packages" ,
337- ),
350+ "//conditions:default" : generated_namespace_package_files ,
338351 })
339352 namespace_package_files += generated_namespace_package_files
340353 srcs = srcs + generated_namespace_package_files
@@ -356,6 +369,7 @@ def whl_library_srcs(
356369 experimental_venvs_site_packages = _VENV_SITE_PACKAGES_FLAG ,
357370 namespace_package_files = namespace_package_files ,
358371 )
372+ return wrapper_srcs
359373
360374def whl_library_deps_targets (
361375 * ,
@@ -369,6 +383,7 @@ def whl_library_deps_targets(
369383 group_deps = [],
370384 group_name = None ,
371385 dep_template ,
386+ srcs = None ,
372387 tags = [],
373388 visibility = ["//visibility:public" ],
374389 native = native ,
@@ -396,6 +411,9 @@ def whl_library_deps_targets(
396411 include: {type}`list[str]` The list of packages to include.
397412 group_name: {type}`str | None` name of the dependency group (if any).
398413 dep_template: {type}`str | None` The dep_template to use.
414+ srcs: {type}`list[Label] | None` or a configurable expression with the
415+ source labels attached to the wrapper `py_library`. If `None`, the
416+ source library target is used.
399417 tags: {type}`list[str]` The tags set on the targets.
400418 repo: {type}`str | Label | None` The BUILD.bazel label to the parent repo that has the
401419 sources. If none, then will take the targets from the current dir.
@@ -501,10 +519,13 @@ def whl_library_deps_targets(
501519 )
502520
503521 if hasattr (rules , "py_library" ):
522+ if srcs == None :
523+ srcs = [repo_label (PY_SRCS_LABEL )]
504524 rules .py_library (
505525 name = py_library_label ,
506- # We include as srcs to ensure that the (locations :pkg) works as expected.
507- srcs = [repo_label (PY_SRCS_LABEL )],
526+ # Forward source-producing targets through `srcs` so downstream
527+ # `$(locations :pkg)` expansion does not reject source-less wheels.
528+ srcs = srcs ,
508529 deps = _deps (
509530 # We include as deps, so that `PyInfo` and friends (e.g. `pyi_srcs`) get
510531 # propagated. Just passing the target as `srcs` is not enough to propagate
0 commit comments