3939 "filter_to_py_srcs" ,
4040 "get_imports" ,
4141 "runfiles_root_path" ,
42- "union_attrs" ,
4342)
44- load (":flags.bzl" , "AddSrcsToRunfilesFlag" , "PrecompileFlag" )
43+ load (":flags.bzl" , "AddSrcsToRunfilesFlag" , "PrecompileFlag" , "VenvsSitePackages" )
4544load (":precompile.bzl" , "maybe_precompile" )
4645load (":py_cc_link_params_info.bzl" , "PyCcLinkParamsInfo" )
4746load (":py_internal.bzl" , "py_internal" )
@@ -59,38 +58,25 @@ LIBRARY_ATTRS = dicts.add(
5958 PY_SRCS_ATTRS ,
6059 IMPORTS_ATTRS ,
6160 {
62- "experimental_venv_site_packages " : lambda : attrb .Bool (
61+ "experimental_venvs_site_packages " : lambda : attrb .Label (
6362 doc = """
64- Internal attribute. Should only be set by rules_python-internal code.
63+ **INTERNAL ATTRIBUTE. SHOULD ONLY BE SET BY rules_python-INTERNAL CODE.**
6564
6665:::{include} /_includes/experimental_api.md
6766:::
6867
69- If true, the library consults {flag}`//python/config_settings:venv_site_packages`
70- to decide if `srcs` is a site-packages relative layout.
71- """ ,
72- default = False ,
73- ),
74- "XXsite_packages_root" : lambda : attrb .String (
75- doc = """
76- Package relative prefix to remove from `srcs` for site-packages layouts.
77-
78- This attribute is mutually exclusive with the {attr}`imports` attribute.
68+ A flag that decides whether the library should treat its sources as a
69+ site-packages layout.
7970
80- When set, `srcs` are interpreted to have a file layout as if they were installed
81- in site-packages. This attribute specifies the directory within `srcs` to treat
82- as the site-packages root so the correct site-packages relative paths for
83- the files can be computed.
71+ When the flag is `yes`, then the `srcs` files are treated as a site-packages
72+ layout that is relative to the `imports` attribute. The `imports` attribute
73+ can have only a single element. It is a repo-relative runfiles path.
8474
85- :::{note}
86- This string is relative to the target's *Bazel package*. e.g. Relative to the
87- directory with the BUILD file that defines the target (the same as how e.g.
88- `srcs`).
89- :::
90-
91- For example, given `srcs=["site-packages/foo/bar.py"]`, specifying
92- `site_packages_root="site-packages/" means `foo/bar.py` is the file path
93- under the binary's venv site-packages directory that should be made available.
75+ For example, in the `my/pkg/BUILD.bazel` file, given
76+ `srcs=["site-packages/foo/bar.py"]`, specifying
77+ `imports=["my/pkg/site-packages"]` means `foo/bar.py` is the file path
78+ under the binary's venv site-packages directory that should be made available (i.e.
79+ `import foo.bar` will work).
9480
9581`__init__.py` files are treated specially to provide basic support for [implicit
9682namespace packages](
@@ -169,17 +155,7 @@ def py_library_impl(ctx, *, semantics):
169155 imports = []
170156 site_packages_symlinks = []
171157
172- ##if ctx.attr.imports and ctx.attr.site_packages_root:
173- ## fail(("Only one of the `imports` or `site_packages_root` attributes " +
174- ## "can be set: site_packages_root={}, imports={}").format(
175- ## ctx.attr.site_packages_root,
176- ## ctx.attr.imports,
177- ## ))
178- ##elif ctx.attr.site_packages_root:
179- ## site_packages_symlinks = _get_site_packages_symlinks(ctx)
180- ##elif ctx.attr.imports:
181- ## imports = collect_imports(ctx, semantics)
182- imports , site_packages_symlinks = _get_imports_and_site_packages_symlinks (ctx )
158+ imports , site_packages_symlinks = _get_imports_and_site_packages_symlinks (ctx , semantics )
183159
184160 cc_info = semantics .get_cc_info_for_library (ctx )
185161 py_info , deps_transitive_sources , builtins_py_info = create_py_info (
@@ -231,8 +207,7 @@ Source files are no longer added to the runfiles directly.
231207def _get_imports_and_site_packages_symlinks (ctx , semantics ):
232208 imports = depset ()
233209 site_packages_symlinks = depset ()
234- if (ctx .attr .experimental_venv_site_packages and
235- ctx .attr ._venv_site_packages_flag [BuildSettingInfo ].value ):
210+ if VenvsSitePackages .is_enabled (ctx ):
236211 site_packages_symlinks = _get_site_packages_symlinks (ctx )
237212 else :
238213 imports = collect_imports (ctx , semantics )
@@ -247,6 +222,14 @@ def _get_site_packages_symlinks(ctx):
247222 else :
248223 site_packages_root = imports [0 ]
249224
225+ if site_packages_root .endswith ("/" ):
226+ fail ("should not end in slash" )
227+ if site_packages_root .startswith ("/" ):
228+ fail ("cannot start with slash" )
229+
230+ # Append slash to prevent incorrectly prefix-string matches
231+ site_packages_root += "/"
232+
250233 # We have to build a list of (runfiles path, site-packages path) pairs of
251234 # the files to create in the consuming binary's venv site-packages directory.
252235 # To minimize the number of files to create, we just return the paths
@@ -262,7 +245,7 @@ def _get_site_packages_symlinks(ctx):
262245 # directories that _do_ have an `__init__.py` file and treat those as
263246 # the path to symlink to.
264247
265- site_packages_root = paths .join (ctx .label .package , site_packages_root )
248+ ## site_packages_root = paths.join(ctx.label.package, site_packages_root)
266249 repo_runfiles_dirname = None
267250 dirs_with_init = {} # dirname -> runfile path
268251 for src in ctx .files .srcs :
@@ -302,6 +285,8 @@ def _get_site_packages_symlinks(ctx):
302285 paths .join (repo_runfiles_dirname , site_packages_root , dirname ),
303286 dirname ,
304287 ))
288+ if not site_packages_symlinks :
289+ fail ("empty?" , ctx .label , site_packages_root , ctx .files .srcs [0 ])
305290 return site_packages_symlinks
306291
307292def _repo_relative_short_path (short_path ):
0 commit comments