Skip to content

Commit 0139ae8

Browse files
committed
have non-regex_match fallback for pre bazel 8
1 parent 84ccbb8 commit 0139ae8

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

python/private/venv_runfiles.bzl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ def _merge_venv_path_group(ctx, group, keep_map):
215215
if venv_path not in keep_map:
216216
keep_map[venv_path] = file
217217

218+
def _is_importable_name(name):
219+
# Requires Bazel 8+
220+
if hasattr(py_internal, "regex_match"):
221+
# ?U means activates unicode matching (Python allows most unicode
222+
# in module names / identifiers).
223+
# \w matches alphanumeric and underscore.
224+
# NOTE: regex_match has an implicit ^ and $
225+
return py_internal.regex_match(name, "(?U)\\w+")
226+
else:
227+
# Otherwise, use a rough hueristic that should catch most cases.
228+
return (
229+
"." not in name and
230+
"-" not in name
231+
)
232+
218233
def get_venv_symlinks(ctx, files, package, version_str, site_packages_root):
219234
"""Compute the VenvSymlinkEntry objects for a library.
220235
@@ -304,7 +319,7 @@ def get_venv_symlinks(ctx, files, package, version_str, site_packages_root):
304319
# If its already known to be non-implicit namespace, then skip
305320
namespace_package_dirs.get(top_level_dirname, True) and
306321
# It must be an importable name to be an implicit namespace package
307-
py_internal.regex_match(top_level_dirname, "(?U)\\w+")
322+
_is_importable_name(top_level_dirname)
308323
):
309324
namespace_package_dirs.setdefault(top_level_dirname, True)
310325

tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,11 @@ def _test_optimized_grouping_implicit_namespace_packages(name):
269269
empty_files(
270270
name = name + "_files",
271271
paths = [
272-
"site-packages/namespace/part1/foo.py",
273-
"site-packages/namespace/part2/bar.py",
274-
"site-packages/namespace-1.0.dist-info/METADATA",
272+
# NOTE: An alphanumeric name with underscores is used to verify
273+
# name matching is correct.
274+
"site-packages/name_space9/part1/foo.py",
275+
"site-packages/name_space9/part2/bar.py",
276+
"site-packages/name_space9-1.0.dist-info/METADATA",
275277
],
276278
)
277279
analysis_test(
@@ -296,24 +298,24 @@ def _test_optimized_grouping_implicit_namespace_packages_impl(env, target):
296298
rr = "{}/{}/site-packages/".format(test_ctx.workspace_name, env.ctx.label.package)
297299
expected = [
298300
_venv_symlink(
299-
"namespace/part1",
300-
link_to_path = rr + "namespace/part1",
301+
"name_space9/part1",
302+
link_to_path = rr + "name_space9/part1",
301303
files = [
302-
"tests/venv_site_packages_libs/app_files_building/site-packages/namespace/part1/foo.py",
304+
"tests/venv_site_packages_libs/app_files_building/site-packages/name_space9/part1/foo.py",
303305
],
304306
),
305307
_venv_symlink(
306-
"namespace/part2",
307-
link_to_path = rr + "namespace/part2",
308+
"name_space9/part2",
309+
link_to_path = rr + "name_space9/part2",
308310
files = [
309-
"tests/venv_site_packages_libs/app_files_building/site-packages/namespace/part2/bar.py",
311+
"tests/venv_site_packages_libs/app_files_building/site-packages/name_space9/part2/bar.py",
310312
],
311313
),
312314
_venv_symlink(
313-
"namespace-1.0.dist-info",
314-
link_to_path = rr + "namespace-1.0.dist-info",
315+
"name_space9-1.0.dist-info",
316+
link_to_path = rr + "name_space9-1.0.dist-info",
315317
files = [
316-
"tests/venv_site_packages_libs/app_files_building/site-packages/namespace-1.0.dist-info/METADATA",
318+
"tests/venv_site_packages_libs/app_files_building/site-packages/name_space9-1.0.dist-info/METADATA",
317319
],
318320
),
319321
]

0 commit comments

Comments
 (0)