Skip to content

Commit 81af833

Browse files
committed
switch to internal attr to trigger behavior
1 parent 501a75f commit 81af833

File tree

16 files changed

+78
-71
lines changed

16 files changed

+78
-71
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ Unreleased changes template.
9191
please check the {obj}`uv.configure` tag class.
9292
* Add support for riscv64 linux platform.
9393
* (toolchains) Add python 3.13.2 and 3.12.9 toolchains
94+
* (providers) (experimental) {obj}`PyInfo.site_packages_symlinks` field added to
95+
allow specifying links to create within the venv site packages (only
96+
applicable with {obj}`--bootstrap_impl=script`)
97+
([#2156](https://github.com/bazelbuild/rules_python/issues/2156)).
9498

9599
{#v0-0-0-removed}
96100
### Removed
@@ -156,10 +160,6 @@ Unreleased changes template.
156160
which allows pass arguments to the interpreter before the regular args.
157161
* (rules) Added {obj}`main_module` attribute to `py_binary` and `py_test`,
158162
which allows specifying a module name to run (i.e. `python -m <module>`).
159-
* (providers) (experimental) {obj}`PyInfo.site_packages_symlinks` field added to
160-
allow specifying links to create within the venv site packages (only
161-
applicable with {obj}`--bootstrap_impl=script`)
162-
([#2156](https://github.com/bazelbuild/rules_python/issues/2156)).
163163

164164
{#v1-3-0-removed}
165165
### Removed

docs/_includes/experimental_api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:::{warning}
2+
3+
**Experimental API.** This API is still under development and may change or be
4+
removed without notice.
5+
:::

docs/api/rules_python/python/config_settings/index.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,19 @@ Values:
215215

216216
::::
217217

218-
:::{flag} venv_site_packages
218+
:::{flag} venvs_site_packages
219+
220+
Determines if libraries use a site-packages layout for their files.
221+
222+
Note this flag only affects PyPI dependencies of `--bootstrap_impl=script` binaries
223+
224+
:::{include} /_includes/experimental_api.md
225+
:::
219226

220-
Determines if
221227

222228
Values:
223-
* `no` (default)
224-
* `yes`
229+
* `no` (default): Make libraries importable by adding to `sys.path`
230+
* `yes`: Make libraries importable by creating paths in a binary's site-packages directory.
225231
::::
226232

227233
::::{bzl:flag} bootstrap_impl

python/config_settings/BUILD.bazel

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ load(
99
"LibcFlag",
1010
"PrecompileFlag",
1111
"PrecompileSourceRetentionFlag",
12-
"VenvSitePackages",
12+
"VenvsSitePackages",
1313
"VenvsUseDeclareSymlinkFlag",
1414
)
1515
load(
@@ -197,8 +197,8 @@ string_flag(
197197
)
198198

199199
string_flag(
200-
name = "venv_site_packages",
201-
build_setting_default = VenvSitePackages.NO,
200+
name = "venvs_site_packages",
201+
build_setting_default = VenvsSitePackages.NO,
202202
# NOTE: Only public because it is used in pip hub repos.
203203
visibility = ["//visibility:public"],
204204
)

python/features.bzl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ def _features_typedef():
3131
:::
3232
::::
3333
34-
::::{field} site_packages_root_attr
35-
:type: bool
36-
37-
True if the {obj}`site_packages_root` attribute is available.
38-
39-
:::{versionadded} VERSION_NEXT_FEATURE
40-
:::
41-
::::
42-
4334
::::{field} uses_builtin_rules
4435
:type: bool
4536
@@ -61,7 +52,6 @@ def _features_typedef():
6152
features = struct(
6253
TYPEDEF = _features_typedef,
6354
precompile = True,
64-
site_packages_root_attr = True,
6555
uses_builtin_rules = not config.enable_pystar,
6656
version = _VERSION_PRIVATE if "$Format" not in _VERSION_PRIVATE else "",
6757
)

python/private/attributes.bzl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ that depend on this rule. The strings are repo-runfiles-root relative,
236236
237237
Absolute paths (paths that start with `/`) and paths that references a path
238238
above the execution root are not allowed and will result in an error.
239-
240-
This attribute is mutually exclusive with the {attr}`site_packages_root` attribute.
241239
""",
242240
),
243241
}

python/private/flags.bzl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,20 @@ VenvsUseDeclareSymlinkFlag = FlagEnum(
118118
get_value = _venvs_use_declare_symlink_flag_get_value,
119119
)
120120

121-
# Decides if libraries
122-
VenvSitePackages = FlagEnum(
121+
def _venvs_site_packages_is_enabled(ctx):
122+
if not ctx.attr.experimental_venvs_site_packages:
123+
return False
124+
flag_value = ctx.attr.experimental_venvs_site_packages[BuildSettingInfo].value
125+
return flag_value == VenvsSitePackages.YES
126+
127+
# Decides if libraries try to use a site-packages layout using site_packages_symlinks
128+
# buildifier: disable=name-conventions
129+
VenvsSitePackages = FlagEnum(
130+
# Use site_packages_symlinks
123131
YES = "yes",
132+
# Don't use site_packages_symlinks
124133
NO = "no",
134+
is_enabled = _venvs_site_packages_is_enabled,
125135
)
126136

127137
# Used for matching freethreaded toolchains and would have to be used in wheels

python/private/py_info.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ Tuples of `(runfiles_path, site_packages_path)`. Where
158158
the code in `runfiles_path` available for import. Note that this
159159
is created as a "raw" symlink (via `declare_symlink`).
160160
161+
:::{include} /_includes/experimental_api.md
162+
:::
163+
161164
:::{tip}
162165
The topological ordering means dependencies earlier and closer to the consumer
163166
have precedence. This allows e.g. a binary to add dependencies that override

python/private/py_library.bzl

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ load(
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")
4544
load(":precompile.bzl", "maybe_precompile")
4645
load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo")
4746
load(":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
9682
namespace 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.
231207
def _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

307292
def _repo_relative_short_path(short_path):

python/private/pypi/whl_library_targets.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def whl_library_targets(
266266
),
267267
tags = tags,
268268
visibility = impl_vis,
269+
experimental_venvs_site_packages = Label("@rules_python//python/config_settings:venvs_site_packages"),
269270
)
270271

271272
def _config_settings(dependencies_by_platform, native = native, **kwargs):

0 commit comments

Comments
 (0)