Skip to content

Commit c70f05a

Browse files
committed
make the linking algo a little bit more robust by adding a test for external data inclusion.
1 parent 0c594e5 commit c70f05a

11 files changed

Lines changed: 65 additions & 22 deletions

File tree

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# (Note, we cannot use `common --deleted_packages` because the bazel version command doesn't support it)
55
# To update these lines, execute
66
# `bazel run @rules_bazel_integration_test//tools:update_deleted_packages`
7-
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2
8-
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2
7+
build --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data
8+
query --deleted_packages=examples/build_file_generation,examples/build_file_generation/random_number_generator,examples/bzlmod,examples/bzlmod_build_file_generation,examples/bzlmod_build_file_generation/other_module/other_module/pkg,examples/bzlmod_build_file_generation/runfiles,examples/bzlmod/entry_points,examples/bzlmod/entry_points/tests,examples/bzlmod/libs/my_lib,examples/bzlmod/other_module,examples/bzlmod/other_module/other_module/pkg,examples/bzlmod/patches,examples/bzlmod/py_proto_library,examples/bzlmod/py_proto_library/example.com/another_proto,examples/bzlmod/py_proto_library/example.com/proto,examples/bzlmod/runfiles,examples/bzlmod/tests,examples/bzlmod/tests/other_module,examples/bzlmod/whl_mods,examples/multi_python_versions/libs/my_lib,examples/multi_python_versions/requirements,examples/multi_python_versions/tests,examples/pip_parse,examples/pip_parse_vendored,examples/pip_repository_annotations,examples/py_proto_library,examples/py_proto_library/example.com/another_proto,examples/py_proto_library/example.com/proto,gazelle,gazelle/manifest,gazelle/manifest/generate,gazelle/manifest/hasher,gazelle/manifest/test,gazelle/modules_mapping,gazelle/python,gazelle/pythonconfig,gazelle/python/private,tests/integration/compile_pip_requirements,tests/integration/compile_pip_requirements_test_from_external_repo,tests/integration/custom_commands,tests/integration/ignore_root_user_error,tests/integration/ignore_root_user_error/submodule,tests/integration/local_toolchains,tests/integration/pip_parse,tests/integration/pip_parse/empty,tests/integration/py_cc_toolchain_registered,tests/modules/another_module,tests/modules/other,tests/modules/other/nspkg_delta,tests/modules/other/nspkg_gamma,tests/modules/other/nspkg_single,tests/modules/other/simple_v1,tests/modules/other/simple_v2,tests/modules/other/with_external_data
99

1010
test --test_output=errors
1111

MODULE.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ bazel_dep(name = "rules_multirun", version = "0.9.0", dev_dependency = True)
8686
bazel_dep(name = "bazel_ci_rules", version = "1.0.0", dev_dependency = True)
8787
bazel_dep(name = "rules_pkg", version = "1.0.1", dev_dependency = True)
8888
bazel_dep(name = "other", version = "0", dev_dependency = True)
89+
bazel_dep(name = "another_module", version = "0", dev_dependency = True)
8990

9091
# Extra gazelle plugin deps so that WORKSPACE.bzlmod can continue including it for e2e tests.
9192
# We use `WORKSPACE.bzlmod` because it is impossible to have dev-only local overrides.
@@ -116,6 +117,11 @@ local_path_override(
116117
path = "tests/modules/other",
117118
)
118119

120+
local_path_override(
121+
module_name = "another_module",
122+
path = "tests/modules/another_module",
123+
)
124+
119125
dev_python = use_extension(
120126
"//python/extensions:python.bzl",
121127
"python",

python/private/py_library.bzl

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,6 @@ def _get_venv_symlinks(ctx, dist_info_metadata):
269269
# directories that _do_ have an `__init__.py` file and treat those as
270270
# the path to symlink to.
271271

272-
repo_runfiles_dirname = None
273272
dir_symlinks = {} # dirname -> runfile path
274273
venv_symlinks = []
275274
package = None
@@ -286,45 +285,36 @@ def _get_venv_symlinks(ctx, dist_info_metadata):
286285
version.normalize(version_str),
287286
)
288287

289-
repo_runfiles_dirname = runfiles_root_path(ctx, dist_info_metadata.short_path).partition("/")[0]
290-
venv_symlinks.append(VenvSymlinkEntry(
291-
kind = VenvSymlinkKind.LIB,
292-
link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, dist_info_dir),
293-
package = package,
294-
venv_path = dist_info_dir,
295-
))
296-
297288
for src in ctx.files.srcs + ctx.files.data:
298289
path = _repo_relative_short_path(src.short_path)
299290
if not path.startswith(site_packages_root):
300291
continue
301292
path = path.removeprefix(site_packages_root)
302293
dir_name, _, filename = path.rpartition("/")
303294

304-
if dist_info_metadata and dir_name in path:
305-
# we have already handled the stuff
295+
if dir_name in dir_symlinks:
296+
# we already have this dir.
306297
continue
307298

299+
runfiles_dir_name, _, _ = runfiles_root_path(ctx, src.short_path).partition("/")
308300
if dir_name:
309301
# This can be either a directory with libs (e.g. numpy.libs)
310302
# or a directory with `__init__.py` file that potentially also needs to be
311303
# symlinked.
312304
#
313305
# This could be also regular files, that just need to be symlinked, so we will
314306
# add the directory here.
315-
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
316-
dir_symlinks[dir_name] = repo_runfiles_dirname
307+
dir_symlinks[dir_name] = runfiles_dir_name
317308
elif src.extension in PYTHON_FILE_EXTENSIONS:
318-
repo_runfiles_dirname = runfiles_root_path(ctx, src.short_path).partition("/")[0]
319-
320309
# This would be files that do not have directories and we just need to add
321310
# direct symlinks to them as is:
322-
venv_symlinks.append(VenvSymlinkEntry(
311+
entry = VenvSymlinkEntry(
323312
kind = VenvSymlinkKind.LIB,
324-
link_to_path = paths.join(repo_runfiles_dirname, site_packages_root, filename),
313+
link_to_path = paths.join(runfiles_dir_name, site_packages_root, filename),
325314
package = package,
326315
venv_path = filename,
327-
))
316+
)
317+
venv_symlinks.append(entry)
328318

329319
# Sort so that we encounter `foo` before `foo/bar`. This ensures we
330320
# see the top-most explicit package first.
@@ -342,12 +332,14 @@ def _get_venv_symlinks(ctx, dist_info_metadata):
342332

343333
for dirname in first_level_explicit_packages:
344334
prefix = dir_symlinks[dirname]
345-
venv_symlinks.append(VenvSymlinkEntry(
335+
entry = VenvSymlinkEntry(
346336
kind = VenvSymlinkKind.LIB,
347337
link_to_path = paths.join(prefix, site_packages_root, dirname),
348338
package = package,
349339
venv_path = dirname,
350-
))
340+
)
341+
venv_symlinks.append(entry)
342+
351343
return venv_symlinks
352344

353345
def _repo_relative_short_path(short_path):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filegroup(
2+
name = "data",
3+
srcs = ["another_module_data.txt"],
4+
visibility = ["//visibility:public"],
5+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module(name = "another_module")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("token")

tests/modules/other/MODULE.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module(name = "other")
22

33
bazel_dep(name = "rules_python", version = "0")
4+
bazel_dep(name = "bazel_skylib", version = "1.7.1")
5+
bazel_dep(name = "another_module", version = "0")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
2+
load("@rules_python//python:py_library.bzl", "py_library")
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
# The users may include data through other repos via annotations and copy_file
7+
# just add this edge case.
8+
#
9+
# NOTE: if the data is not copied to `site-packages/<some dir>` then it will not
10+
# appear.
11+
copy_file(
12+
name = "external_data",
13+
src = "@another_module//:data",
14+
out = "site-packages/external_data/another_module_data.txt",
15+
)
16+
17+
py_library(
18+
name = "with_external_data",
19+
srcs = ["site-packages/with_external_data.py"],
20+
data = [":external_data"],
21+
experimental_venvs_site_packages = "@rules_python//python/config_settings:venvs_site_packages",
22+
imports = [package_name() + "/site-packages"],
23+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Intentionally blank

tests/venv_site_packages_libs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ py_reconfig_test(
2929
"@other//nspkg_delta",
3030
"@other//nspkg_gamma",
3131
"@other//nspkg_single",
32+
"@other//with_external_data",
3233
],
3334
)

0 commit comments

Comments
 (0)