feat(py_info): support TreeArtifact venv entries via link_to_subpaths (#4396)

Targets that install a Python package into a directory artifact
(TreeArtifact) during the build phase cannot populate individual File
objects in VenvSymlinkEntry.files when constructing virtual environment
symlinks. Without sub-path metadata, overlapping package directories
across multiple dependencies cannot be merged at file granularity.

Add an experimental VenvSymlinkEntry.link_to_subpaths field (and
builder support) so directory-backed entries can list their relative
file paths for conflict merging, and teach virtual environment link
generation to produce and merge TreeArtifact-backed entries.
Single-file entries whose files holds an ancestor directory of
link_to_path are now linked during conflict merging rather than
skipped.

Also extract pure in-memory helpers for wheel RECORD and console-script
shebang rewriting so build-time installers can reuse them directly with
no change in default behavior.
diff --git a/news/4396.added.md b/news/4396.added.md
new file mode 100644
index 0000000..6773b0b
--- /dev/null
+++ b/news/4396.added.md
@@ -0,0 +1,2 @@
+(rules) Custom rules can now provide directory-backed (`TreeArtifact`) virtual
+environment symlink entries that merge with overlapping packages.
diff --git a/python/private/py_info.bzl b/python/private/py_info.bzl
index baaace1..ead24ec 100644
--- a/python/private/py_info.bzl
+++ b/python/private/py_info.bzl
@@ -56,6 +56,11 @@
 
 def _VenvSymlinkEntry_init(**kwargs):
     kwargs.setdefault("link_to_file", None)
+    link_to_subpaths = kwargs.get("link_to_subpaths")
+    if link_to_subpaths == None:
+        kwargs["link_to_subpaths"] = depset()
+        return kwargs
+    _check_arg_type("link_to_subpaths", "depset", link_to_subpaths)
     return kwargs
 
 # A provider is used for memory efficiency.
@@ -69,10 +74,20 @@
         "files": """
 :type: depset[File]
 
-Files under `link_to_path`.
+Files backing this entry in runfiles. Entries may be regular `File` objects or
+directory artifacts (`File.is_directory`).
 
-This is only used when multiple targets have overlapping `venv_path` paths. e.g.
-if one adds files to `venv_path=a/` and another adds files to `venv_path=a/b/`.
+A directory artifact's contents aren't known at analysis time, so conflict
+merging can't look inside one. To make an entry backed by a directory artifact
+mergeable, list the files under `link_to_path` in {obj}`link_to_subpaths`;
+conflict merging then uses those paths instead of `files`, and `files` only
+supplies runfiles. If {obj}`link_to_subpaths` is empty, the entry is linked as
+a single unit (`venv_path` to `link_to_path`) and can't be merged: analysis
+fails if another entry's files have to be created under its `venv_path`.
+
+Conflict merging occurs when multiple targets have overlapping `venv_path`
+paths, e.g. if one adds files to `venv_path=a/` and another adds files to
+`venv_path=a/b/`.
 """,
         "kind": """
 :type: str
@@ -95,6 +110,27 @@
 A runfiles-root relative path that `venv_path` will symlink to (if
 `link_to_file` is `None`). If `None`, it means to not create it in the venv.
 """,
+        "link_to_subpaths": """
+:type: depset[str]
+
+:::{include} /_includes/experimental_api.md
+:::
+
+Relative paths of the files under `link_to_path`, for entries whose `files`
+are directory artifacts (`File.is_directory`) and whose `link_to_path` is a
+directory. Optional. When non-empty, it must list every file under
+`link_to_path`, and conflict merging uses these paths instead of `files`
+(`files` then only supplies runfiles). When empty, the entry is linked as a
+single unit and can't be merged with entries under its `venv_path`.
+
+This is for entries whose target isn't a single `File`, so it's mutually
+exclusive with `link_to_file`: `link_to_file` is only used when an entry
+doesn't conflict, whereas `link_to_subpaths` is only used during conflict
+merging.
+
+:::{versionadded} VERSION_NEXT_FEATURE
+:::
+""",
         "package": """
 :type: str | None
 
@@ -125,6 +161,13 @@
     :type: DepsetBuilder[File]
     :::
 
+    ::::{field} link_to_subpaths
+    :type: DepsetBuilder[str]
+
+    :::{versionadded} VERSION_NEXT_FEATURE
+    :::
+    ::::
+
     :::{versionadded} 2.2.0
     :::
     """
@@ -141,6 +184,7 @@
             "version": None,
         },
         files = builders.DepsetBuilder(),
+        link_to_subpaths = builders.DepsetBuilder(),
         get_kind = lambda: _VenvSymlinkEntryBuilder_get_kind(self),
         set_kind = lambda k: _VenvSymlinkEntryBuilder_set_kind(self, k),
         get_link_to_file = lambda: _VenvSymlinkEntryBuilder_get_link_to_file(self),
@@ -327,6 +371,7 @@
         kind = self._state["kind"],
         link_to_file = self._state["link_to_file"],
         link_to_path = self._state["link_to_path"],
+        link_to_subpaths = self.link_to_subpaths.build(),
         package = self._state["package"],
         venv_path = self._state["venv_path"],
         version = self._state["version"],
diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel
index 03a9e11..e43ea3b 100644
--- a/python/private/pypi/BUILD.bazel
+++ b/python/private/pypi/BUILD.bazel
@@ -14,6 +14,7 @@
 
 load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
 load("//python:py_binary.bzl", "py_binary")
+load("//python:py_library.bzl", "py_library")
 load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
 
 package(default_visibility = ["//:__subpackages__"])
@@ -25,6 +26,15 @@
     visibility = ["//visibility:public"],
 )
 
+py_library(
+    name = "rewriters_lib",
+    srcs = [
+        "venv_shebang_rewriter.py",
+        "wheel_record_rewriter.py",
+    ],
+    visibility = ["//tests:__subpackages__"],
+)
+
 alias(
     name = "venv_entry_point_template",
     actual = select({
diff --git a/python/private/pypi/venv_shebang_rewriter.py b/python/private/pypi/venv_shebang_rewriter.py
index 48e615b..3ad62fc 100644
--- a/python/private/pypi/venv_shebang_rewriter.py
+++ b/python/private/pypi/venv_shebang_rewriter.py
@@ -1,42 +1,64 @@
-"""Rewrites a console_script wrapper's shebang into a batch/shell-Python polyglot."""
+"""Rewrites a console_script wrapper's shebang into a batch/shell-Python polyglot.
+
+Usage:
+    venv_shebang_rewriter.py <in_path> <out_path> <target_os>
+
+Input file format:
+    A wheel `.data/scripts/*` script whose first line starts with `#!python`
+    (or `#!pythonw`).
+"""
+
+from __future__ import annotations
 
 import os
 import sys
+from collections.abc import Sequence
 
 
-def main(argv):
-    in_path, out_path, target_os = argv[1:4]
+def rewrite_shebang_bytes(content: bytes, target_os: str) -> bytes:
+    """Replace the leading `#!python` shebang line with a portable polyglot header."""
+    nl = content.find(b"\n")
+    if nl == -1:
+        first_line = content
+        rest = b""
+    else:
+        first_line = content[: nl + 1]
+        rest = content[nl + 1 :]
+
+    if target_os == "windows":
+        python_exe = (
+            b"pythonw.exe" if first_line.startswith(b"#!pythonw") else b"python.exe"
+        )
+        # A Batch-Python polyglot. Batch executes the first line and exits,
+        # while Python (via -x) ignores the first line and executes the rest.
+        header = (
+            b'@setlocal enabledelayedexpansion & "%~dp0'
+            + python_exe
+            + b'" -x "%~f0" %* & exit /b !ERRORLEVEL!\r\n'
+        )
+    else:
+        # A Shell-Python polyglot. The shell executes the triple-quoted 'exec'
+        # command, re-running the script with python3 from the scripts directory.
+        # Python ignores the triple-quoted string and continues.
+        header = (
+            b"#!/bin/sh\n'''exec' \"$(dirname \"$0\")/python3\" \"$0\" \"$@\"\n' '''\n"
+        )
+    return header + rest
+
+
+def main(argv: Sequence[str] | None = None) -> None:
+    args = sys.argv[1:] if argv is None else argv
+    in_path, out_path, target_os = args[:3]
 
     with open(in_path, "rb") as in_file:
-        first_line = in_file.readline()
-        rest = in_file.read()
+        content = in_file.read()
 
     with open(out_path, "wb") as out_file:
-        if target_os == "windows":
-            python_exe = (
-                b"pythonw.exe" if first_line.startswith(b"#!pythonw") else b"python.exe"
-            )
-            # A Batch-Python polyglot. Batch executes the first line and exits,
-            # while Python (via -x) ignores the first line and executes the rest.
-            out_file.write(
-                b'@setlocal enabledelayedexpansion & "%~dp0'
-                + python_exe
-                + b'" -x "%~f0" %* & exit /b !ERRORLEVEL!\r\n',
-            )
-        else:
-            out_file.write(b"#!/bin/sh\n")
-            # A Shell-Python polyglot. The shell executes the triple-quoted 'exec'
-            # command, re-running the script with python3 from the scripts directory.
-            # Python ignores the triple-quoted string and continues.
-            out_file.write(
-                b"'''exec' \"$(dirname \"$0\")/python3\" \"$0\" \"$@\"\n' '''\n"
-            )
-
-        out_file.write(rest)
+        out_file.write(rewrite_shebang_bytes(content, target_os))
 
     mode = os.stat(out_path).st_mode
     os.chmod(out_path, mode | 0o111)
 
 
 if __name__ == "__main__":
-    main(sys.argv)
+    main()
diff --git a/python/private/pypi/wheel_record_rewriter.py b/python/private/pypi/wheel_record_rewriter.py
index 8931b30..deaad65 100644
--- a/python/private/pypi/wheel_record_rewriter.py
+++ b/python/private/pypi/wheel_record_rewriter.py
@@ -1,9 +1,26 @@
-"""Rewrites a wheel's RECORD file to reflect its final installed layout."""
+"""Rewrites a wheel's RECORD file to reflect its final installed layout.
 
+Usage:
+    wheel_record_rewriter.py <in_path> <out_path> <target_os> <data_dir_basename> [<rewritten_script> ...]
+
+Input file format:
+    A UTF-8 PEP 376 `RECORD` CSV file from `<dist>-<ver>.dist-info/RECORD`.
+"""
+
+from __future__ import annotations
+
+import os
 import sys
+from collections.abc import Iterable, Sequence, Set
 
 
-def _rewrite(in_path, out_path, target_os, data_dir_basename, rewritten_scripts):
+def rewrite_record_lines(
+    lines: Iterable[str],
+    target_os: str,
+    data_dir_basename: str,
+    rewritten_scripts: Set[str],
+) -> list[str]:
+    """Rewrite `<dist>.data/<scheme>/...` RECORD lines to installed paths."""
     data_prefix = data_dir_basename + "/"
     quoted_data_prefix = '"' + data_prefix
 
@@ -20,56 +37,70 @@
         purelib_repl = ""
         scripts_repl = "../../../bin/"
 
+    out_lines = []
+    for raw_line in lines:
+        line = raw_line.rstrip("\r\n")
+
+        if line.startswith(quoted_data_prefix):
+            quote = '"'
+            rest = line[len(quoted_data_prefix) :]
+        elif line.startswith(data_prefix):
+            quote = ""
+            rest = line[len(data_prefix) :]
+        else:
+            out_lines.append(line + "\n")
+            continue
+
+        if rest.startswith("purelib/"):
+            out_lines.append(quote + purelib_repl + rest[len("purelib/") :] + "\n")
+        elif rest.startswith("platlib/"):
+            out_lines.append(quote + platlib_repl + rest[len("platlib/") :] + "\n")
+        elif rest.startswith("scripts/"):
+            entry = rest[len("scripts/") :]
+            if target_os == "windows":
+                idx = entry.index('"' if quote else ",")
+                spath, suffix = entry[:idx], entry[idx:]
+                if spath in rewritten_scripts:
+                    spath += ".bat"
+                entry = spath + suffix
+            out_lines.append(quote + scripts_repl + entry + "\n")
+        elif rest.startswith("headers/"):
+            out_lines.append(quote + headers_repl + rest[len("headers/") :] + "\n")
+        elif rest.startswith("data/"):
+            out_lines.append(quote + data_repl + rest[len("data/") :] + "\n")
+        else:
+            out_lines.append(line + "\n")
+    return out_lines
+
+
+def rewrite_record_file(
+    in_path: str | os.PathLike[str],
+    out_path: str | os.PathLike[str],
+    target_os: str,
+    data_dir_basename: str,
+    rewritten_scripts: Set[str],
+) -> None:
+    """Read `in_path`, rewrite `.data/` entries, and write LF-terminated UTF-8 to `out_path`."""
     # PEP 427 specifies archive filenames are UTF-8, and while the encoding of
     # dist-info files isn't formally standardized in PEP 376, packaging tools
     # like pip and importlib.metadata treat them as UTF-8 in practice.
     # See: https://discuss.python.org/t/encoding-of-files-in-the-dist-info-directory/6734/4
-    with open(in_path, encoding="utf-8") as in_file, open(
-        out_path, "w", encoding="utf-8", newline="\n"
-    ) as out_file:
-        for raw_line in in_file:
-            line = raw_line.rstrip("\r\n")
-
-            if line.startswith(quoted_data_prefix):
-                quote = '"'
-                rest = line[len(quoted_data_prefix) :]
-            elif line.startswith(data_prefix):
-                quote = ""
-                rest = line[len(data_prefix) :]
-            else:
-                out_file.write(line + "\n")
-                continue
-
-            if rest.startswith("purelib/"):
-                out_file.write(quote + purelib_repl + rest[len("purelib/") :] + "\n")
-            elif rest.startswith("platlib/"):
-                out_file.write(quote + platlib_repl + rest[len("platlib/") :] + "\n")
-            elif rest.startswith("scripts/"):
-                entry = rest[len("scripts/") :]
-                if target_os == "windows":
-                    if quote == '"':
-                        idx = entry.index('"')
-                    else:
-                        idx = entry.index(",")
-                    spath, suffix = entry[:idx], entry[idx:]
-                    if spath in rewritten_scripts:
-                        spath += ".bat"
-                    out_file.write(quote + scripts_repl + spath + suffix + "\n")
-                else:
-                    out_file.write(quote + scripts_repl + entry + "\n")
-            elif rest.startswith("headers/"):
-                out_file.write(quote + headers_repl + rest[len("headers/") :] + "\n")
-            elif rest.startswith("data/"):
-                out_file.write(quote + data_repl + rest[len("data/") :] + "\n")
-            else:
-                out_file.write(line + "\n")
+    with open(in_path, encoding="utf-8") as in_file:
+        out_lines = rewrite_record_lines(
+            in_file, target_os, data_dir_basename, rewritten_scripts
+        )
+    with open(out_path, "w", encoding="utf-8", newline="\n") as out_file:
+        out_file.writelines(out_lines)
 
 
-def main(argv):
-    in_path, out_path, target_os, data_dir_basename = argv[1:5]
-    rewritten_scripts = set(argv[5:])
-    _rewrite(in_path, out_path, target_os, data_dir_basename, rewritten_scripts)
+def main(argv: Sequence[str] | None = None) -> None:
+    args = sys.argv[1:] if argv is None else argv
+    in_path, out_path, target_os, data_dir_basename = args[:4]
+    rewritten_scripts = set(args[4:])
+    rewrite_record_file(
+        in_path, out_path, target_os, data_dir_basename, rewritten_scripts
+    )
 
 
 if __name__ == "__main__":
-    main(sys.argv)
+    main()
diff --git a/python/private/venv_runfiles.bzl b/python/private/venv_runfiles.bzl
index 45d4d24..911e47b 100644
--- a/python/private/venv_runfiles.bzl
+++ b/python/private/venv_runfiles.bzl
@@ -126,7 +126,7 @@
     )
 
 # Visible for testing
-def build_link_map(ctx, entries, return_conflicts = False):
+def build_link_map(ctx, entries, return_conflicts = False, _fail = fail):
     """Compute the mapping of venv paths to their backing objects.
 
     Args:
@@ -136,12 +136,14 @@
         return_conflicts: {type}`bool`. Only present for testing. If True,
             also return a list of the groups that had overlapping paths and had
             to be resolved and merged.
+        _fail: {type}`callable`. Only present for testing. Called with the
+            error message when overlapping entries can't be merged.
 
     Returns:
-        {type}`dict[str, dict[str, str|File]]` Mappings of venv paths to their
-        backing files. The first key is a `VenvSymlinkKind` value.
-        The inner dict keys are venv paths relative to the kind's directory. The
-        inner dict values are strings or Files to link to.
+        {type}`dict[str, dict[str, File | str]]` Mappings of venv paths to their
+        backing files, keyed by `VenvSymlinkKind` value, then by venv path
+        relative to the kind's directory. Values are `File`s or
+        runfiles-root-relative path strings to link to.
     """
 
     version_by_pkg = {}  # dict[str pkg, str version]
@@ -164,10 +166,10 @@
         # else: ignore it; not the selected version
 
     # final paths to keep, grouped by kind
-    keep_link_map = {}  # dict[str kind, dict[path, str|File]]
+    keep_link_map = {}  # dict[str kind, dict[str venv_path, File | str rf_root_path]]
     conflicts = [] if return_conflicts else None
     for kind, entries in entries_by_kind.items():
-        # dict[str kind-relative path, str|File link_to]
+        # dict[str venv_path, File | str rf_root_path]
         keep_kind_link_map = {}
 
         groups = _group_venv_path_entries(entries)
@@ -184,8 +186,13 @@
                 if return_conflicts:
                     conflicts.append(group)
 
-                # Merge a group of overlapping prefixes
-                _merge_venv_path_group(ctx, group, keep_kind_link_map)
+                # Merge a group of overlapping prefixes. Groups never share
+                # venv paths, so merging into a separate map gives the same
+                # result and limits the nesting check to this group's paths.
+                group_link_map = {}
+                _merge_venv_path_group(ctx, group, group_link_map)
+                _check_no_nested_links(kind, group, group_link_map, _fail)
+                keep_kind_link_map.update(group_link_map)
 
         keep_link_map[kind] = keep_kind_link_map
     if return_conflicts:
@@ -193,6 +200,67 @@
     else:
         return keep_link_map
 
+def _check_no_nested_links(kind, group, group_link_map, _fail):
+    """Fails if a merged venv path is also the parent of another one.
+
+    An entry linked as a single unit (a directory `File`, or a path inside a
+    directory artifact that has no `link_to_subpaths`) can't have other
+    entries' files created under it: its venv path would have to be both a
+    symlink and a directory.
+
+    Args:
+        kind: {type}`str` the `VenvSymlinkKind` value of the group.
+        group: {type}`list[VenvSymlinkEntry]` the entries that were merged.
+        group_link_map: {type}`dict[str, File | str]` the merged venv paths of
+            the group.
+        _fail: {type}`callable` called with the error message.
+    """
+
+    # Directories that were already walked up from. Files usually share
+    # directories, so this avoids checking the same parents for every path.
+    checked_dirs = {}
+    for venv_path in group_link_map:
+        parent = venv_path.rpartition("/")[0]
+        for _ in range(venv_path.count("/")):
+            if parent in checked_dirs:
+                break
+            checked_dirs[parent] = None
+            if parent in group_link_map:
+                _fail((
+                    "venv {kind} path '{parent}' is linked as a single " +
+                    "unit, but '{child}' must also be created under it. " +
+                    "Overlapping entries can only be merged when their " +
+                    "files are known at analysis time. Overlapping entries:\n" +
+                    "{overlapping}"
+                ).format(
+                    kind = kind,
+                    parent = parent,
+                    child = venv_path,
+                    overlapping = _describe_entries_above(group, venv_path),
+                ))
+                return
+            parent = parent.rpartition("/")[0]
+
+def _describe_entries_above(group, venv_path):
+    """Describes the entries in `group` that are at or above `venv_path`.
+
+    Args:
+        group: {type}`list[VenvSymlinkEntry]` the entries that were merged.
+        venv_path: {type}`str` the venv path that couldn't be created.
+
+    Returns:
+        {type}`str` one line per entry, for use in an error message.
+    """
+    lines = []
+    for entry in group:
+        if not (venv_path + "/").startswith(entry.venv_path + "/"):
+            continue
+        line = "  '{}' -> {}".format(entry.venv_path, entry.link_to_path)
+        if entry.package:
+            line += " (package: {})".format(entry.package)
+        lines.append(line)
+    return "\n".join(lines)
+
 def _group_venv_path_entries(entries):
     """Group entries by VenvSymlinkEntry.venv_path overlap.
 
@@ -235,8 +303,9 @@
         ctx: {type}`ctx` current ctx.
         group: {type}`list[VenvSymlinkEntry]` a group of entries with overlapping
             `venv_path` prefixes, ordered from shortest to longest path.
-        keep_map: {type}`dict[str, str|File]` files kept after merging are
-            populated into this map.
+        keep_map: {type}`dict[str, File | str]` files kept after merging are
+            populated into this map, keyed by venv path. Values are `File`s or
+            runfiles-root-relative path strings to link to.
     """
 
     # TODO: Compute the minimum number of entries to create. This can't avoid
@@ -247,6 +316,16 @@
     # dynamic linker symlink resolution semantics on Linux.
     for entry in group:
         root_venv_path = entry.venv_path
+        link_to_subpaths = entry.link_to_subpaths.to_list()
+        if link_to_subpaths:
+            # subpath is relative to both entry.link_to_path (runfiles side) and
+            # entry.venv_path (venv side); the two trees mirror each other below them.
+            for subpath in link_to_subpaths:
+                venv_path = paths.join(root_venv_path, subpath)
+                if venv_path not in keep_map:
+                    keep_map[venv_path] = paths.join(entry.link_to_path, subpath)
+            continue
+
         anchored_link_to_path = entry.link_to_path + "/"
         for file in entry.files.to_list():
             rf_root_path = runfiles_root_path(ctx, file.short_path)
@@ -263,52 +342,105 @@
                 # preconditions and can't link anyways), or not under
                 # the prefix (violates preconditions).
                 break
-            else:
-                # Compute the file-specific venv path. i.e. the relative
-                # path of the file under entry.venv_path, joined with
-                # entry.venv_path
-                head, match, rel_venv_path = rf_root_path.partition(anchored_link_to_path)
-                if not match or head:
-                    # If link_to_path didn't match, then obviously skip.
-                    # If head is non-empty, it means link_to_path wasn't
-                    # found at the start
-                    # This shouldn't occur in practice, but guard against it
-                    # just in case
-                    continue
 
-                venv_path = paths.join(root_venv_path, rel_venv_path)
-
+            # A directory artifact that contains link_to_path, e.g. the
+            # installed wheel tree backing a single-file entry. There's no
+            # File for the target itself, so link to its runfiles path.
+            if file.is_directory and entry.link_to_path.startswith(rf_root_path + "/"):
                 # For lack of a better option, first added wins.
-                if venv_path not in keep_map:
-                    keep_map[venv_path] = file
+                if entry.venv_path not in keep_map:
+                    keep_map[entry.venv_path] = entry.link_to_path
+                break
 
-def _get_file_venv_path(ctx, f, site_packages_root):
-    """Computes a file's venv_path if it's under the site_packages_root.
+            # Compute the file-specific venv path. i.e. the relative
+            # path of the file under entry.venv_path, joined with
+            # entry.venv_path
+            head, match, rel_venv_path = rf_root_path.partition(anchored_link_to_path)
+            if not match or head:
+                # If link_to_path didn't match, then obviously skip.
+                # If head is non-empty, it means link_to_path wasn't
+                # found at the start
+                # This shouldn't occur in practice, but guard against it
+                # just in case
+                continue
+
+            venv_path = paths.join(root_venv_path, rel_venv_path)
+
+            # For lack of a better option, first added wins.
+            if venv_path not in keep_map:
+                keep_map[venv_path] = file
+
+def _repo_rel_path_to_venv_path(repo_rel_path, site_packages_root):
+    """Computes a file's venv_path from its repo-relative (or tree-relative) path.
 
     Args:
-        ctx: The current ctx.
-        f: The file to compute the venv_path for.
-        site_packages_root: The site packages root path; repo-relative
-            path.
+        repo_rel_path: {type}`str` Repo-relative (or tree-relative) path.
+        site_packages_root: {type}`str` Trailing-slash-terminated site-packages
+            prefix (e.g. `"site-packages/"`).
 
     Returns:
-        A tuple `(venv_path, rf_root_path)` if the file is under
-        `site_packages_root` or data/, bin/, include/ otherwise `(None, None)`.
+        {type}`str | None` The `venv_path` if `repo_rel_path` is under `data/`,
+        `bin/`, `include/`, or `site_packages_root`; otherwise `None`.
     """
-    rf_root_path = runfiles_root_path(ctx, f.short_path)
-    _, _, repo_rel_path = rf_root_path.partition("/")
 
     # Check for wheel data/bin/include folders first
     for prefix in ["data/", "bin/", "include/"]:
         if repo_rel_path.startswith(prefix):
-            return (repo_rel_path, rf_root_path)
+            return repo_rel_path
 
     head, found_sp_root, venv_path = repo_rel_path.partition(site_packages_root)
     if head or not found_sp_root:
         # If head is set, then the path didn't start with site_packages_root
         # if found_sp_root is empty, then it means it wasn't found at all.
-        return (None, None)
-    return (venv_path, rf_root_path)
+        return None
+    return venv_path
+
+def _venv_candidates(ctx, files, site_packages_root, tree_artifact):
+    """Normalizes input Files or tree-relative path strings into sorted candidates.
+
+    Args:
+        ctx: {type}`ctx` The current ctx.
+        files: {type}`list[File] | list[str]` Regular `File` objects, or
+            `tree_artifact`-relative path strings (`tree_rel_path`) when
+            `tree_artifact` is given.
+        site_packages_root: {type}`str` Trailing-slash-terminated site-packages
+            prefix (e.g. `"site-packages/"`).
+        tree_artifact: {type}`File | None` Optional directory artifact backing
+            the relative path strings in `files`.
+
+    Returns:
+        {type}`list[struct]` Sorted list of `struct(venv_path, rf_root_path, file)`
+        for entries mapped into the venv, where `file` is {type}`File | None`
+        (`None` when backed by `tree_artifact`).
+    """
+    if not files:
+        return []
+
+    candidates = []
+    if tree_artifact != None:
+        tree_rf_root = runfiles_root_path(ctx, tree_artifact.short_path)
+        for tree_rel_path in sorted(files):
+            venv_path = _repo_rel_path_to_venv_path(tree_rel_path, site_packages_root)
+            if venv_path == None:
+                continue
+            candidates.append(struct(
+                venv_path = venv_path,
+                rf_root_path = "{}/{}".format(tree_rf_root, tree_rel_path),
+                file = None,
+            ))
+    else:
+        for file in sorted(files, key = lambda f: f.short_path):
+            rf_root_path = runfiles_root_path(ctx, file.short_path)
+            _, _, repo_rel_path = rf_root_path.partition("/")
+            venv_path = _repo_rel_path_to_venv_path(repo_rel_path, site_packages_root)
+            if venv_path == None:
+                continue
+            candidates.append(struct(
+                venv_path = venv_path,
+                rf_root_path = rf_root_path,
+                file = file,
+            ))
+    return candidates
 
 def get_venv_symlinks(
         ctx,
@@ -316,21 +448,26 @@
         package,
         version_str,
         site_packages_root,
-        namespace_package_files = []):
+        namespace_package_files = [],
+        tree_artifact = None):
     """Compute the VenvSymlinkEntry objects for a library.
 
     Args:
         ctx: {type}`ctx` the current ctx.
-        files: {type}`list[File]` the underlying files that are under
-            `site_packages_root` and intended to be part of the venv
-            contents.
+        files: {type}`list[File] | list[str]` the underlying files (or
+            `tree_artifact`-relative path strings when `tree_artifact` is given)
+            that are under `site_packages_root` and intended to be part of the
+            venv contents.
         package: {type}`str` the Python distribution name.
         version_str: {type}`str` the distribution's version.
         site_packages_root: {type}`str` prefix under which files are
             considered to be part of the installed files.
-        namespace_package_files: {type}`list[File]` a list of files
-            that are pkgutil-style namespace packages and cannot be
+        namespace_package_files: {type}`list[File] | list[str]` a list of files
+            (or `tree_artifact`-relative path strings when `tree_artifact` is
+            given) that are pkgutil-style namespace packages and cannot be
             directly linked.
+        tree_artifact: {type}`File | None` optional directory artifact backing
+            the relative path strings in `files`.
 
     Returns:
         {type}`list[VenvSymlinkEntry]` the entries that describe how
@@ -346,7 +483,11 @@
     # Append slash to prevent incorrect prefix-string matches
     site_packages_root += "/"
 
-    all_files = sorted(files, key = lambda f: f.short_path)
+    candidates = _venv_candidates(ctx, files, site_packages_root, tree_artifact)
+    tree_rf_root = (
+        runfiles_root_path(ctx, tree_artifact.short_path) if tree_artifact != None else None
+    )
+    tree_files_depset = depset([tree_artifact]) if tree_artifact != None else None
 
     cannot_be_linked_directly = {}
     for dirname in [
@@ -378,11 +519,13 @@
         dirname: True
         for dirname in namespace_package_dirs.keys()
     })
-    for f in namespace_package_files:
-        venv_path, _ = _get_file_venv_path(ctx, f, site_packages_root)
-        if venv_path == None:
-            continue
-        ns_dir = paths.dirname(venv_path)
+    for ns_candidate in _venv_candidates(
+        ctx,
+        namespace_package_files,
+        site_packages_root,
+        tree_artifact,
+    ):
+        ns_dir = paths.dirname(ns_candidate.venv_path)
         namespace_package_dirs[ns_dir] = True
         cannot_be_linked_directly[ns_dir] = True
 
@@ -390,8 +533,8 @@
     # Where path is the venv path (i.e. relative to site_packages_prefix)
     venv_symlinks = {}
 
-    # List of (File, str venv_path) tuples
-    files_left_to_link = []
+    # List of candidate structs; see _venv_candidates.
+    candidates_left_to_link = []
 
     # We want to minimize the number of files symlinked. Ideally, only the
     # top-level directories are symlinked. Unfortunately, shared libraries
@@ -401,20 +544,20 @@
     # To fix, we have to directly link shared libraries. This then means that
     # all the parent directories of the shared library can't be linked
     # directly.
-    for src in all_files:
-        venv_path, rf_root_path = _get_file_venv_path(ctx, src, site_packages_root)
-        if venv_path == None:
-            continue
-
+    for candidate in candidates:
+        venv_path = candidate.venv_path
         filename = paths.basename(venv_path)
         if _is_linker_loaded_library(filename):
+            # Tree-backed single-file entries set link_to_file = None because
+            # ctx.runfiles(root_symlinks=...) requires individual File objects,
+            # so consuming binaries materialize them via ctx.actions.declare_symlink().
             venv_symlinks[venv_path] = VenvSymlinkEntry(
                 kind = VenvSymlinkKind.LIB,
-                link_to_path = rf_root_path,
-                link_to_file = src,
+                link_to_path = candidate.rf_root_path,
+                link_to_file = candidate.file,
                 package = package,
                 version = version_str,
-                files = depset([src]),
+                files = tree_files_depset or depset([candidate.file]),
                 venv_path = venv_path,
             )
             parent = paths.dirname(venv_path)
@@ -427,7 +570,7 @@
                 cannot_be_linked_directly[parent] = True
                 parent = paths.dirname(parent)
         else:
-            files_left_to_link.append((src, venv_path))
+            candidates_left_to_link.append(candidate)
 
         top_level_dirname, _, tail = venv_path.partition("/")
         if (
@@ -460,22 +603,23 @@
     # directly linked. Next, we loop over the remaining files and group
     # them into the highest level directory that can be linked.
 
-    # dict[str venv_path, list[File]]
+    # dict[str venv_path, list[candidate struct]]
     optimized_groups = {}
 
-    for src, venv_path in files_left_to_link:
+    for candidate in candidates_left_to_link:
+        venv_path = candidate.venv_path
         parent = paths.dirname(venv_path)
         if not parent:
             # File in root, must be linked directly
             optimized_groups.setdefault(venv_path, [])
-            optimized_groups[venv_path].append(src)
+            optimized_groups[venv_path].append(candidate)
             continue
 
         if parent in cannot_be_linked_directly:
             # File in a directory that cannot be directly linked,
             # so link the file directly
             optimized_groups.setdefault(venv_path, [])
-            optimized_groups[venv_path].append(src)
+            optimized_groups[venv_path].append(candidate)
         else:
             # This path can be grouped. Find the highest-level directory to link.
             venv_path = parent
@@ -491,10 +635,10 @@
                     break
 
             optimized_groups.setdefault(venv_path, [])
-            optimized_groups[venv_path].append(src)
+            optimized_groups[venv_path].append(candidate)
 
     # Finally, for each group, we create the VenvSymlinkEntry objects
-    for venv_path, files in optimized_groups.items():
+    for venv_path, members in optimized_groups.items():
         if venv_path.startswith("data/"):
             out_venv_path = venv_path[len("data/"):]
             kind = VenvSymlinkKind.DATA
@@ -512,20 +656,38 @@
             kind = VenvSymlinkKind.LIB
             prefix = site_packages_root
 
-        link_to_path = (
-            _get_label_runfiles_repo(ctx, files[0].owner) +
-            "/" +
-            prefix +
-            venv_path
-        )
+        if tree_artifact != None:
+            link_to_path = tree_rf_root + "/" + prefix + venv_path
+            link_to_file = None
+            entry_files = tree_files_depset
+
+            # A group keyed by its member's own path links that single file
+            # directly, so it has no subpaths.
+            is_file_link = members[0].venv_path == venv_path
+            link_to_subpaths = depset([] if is_file_link else [
+                m.venv_path[len(venv_path) + 1:]
+                for m in members
+            ])
+        else:
+            link_to_path = (
+                _get_label_runfiles_repo(ctx, members[0].file.owner) +
+                "/" +
+                prefix +
+                venv_path
+            )
+            link_to_file = members[0].file if kind == VenvSymlinkKind.BIN and len(members) == 1 else None
+            entry_files = depset([m.file for m in members])
+            link_to_subpaths = None
+
         venv_symlinks[venv_path] = VenvSymlinkEntry(
             kind = kind,
             link_to_path = link_to_path,
-            link_to_file = files[0] if kind == VenvSymlinkKind.BIN and len(files) == 1 else None,
+            link_to_file = link_to_file,
             package = package,
             version = version_str,
             venv_path = out_venv_path,
-            files = depset(files),
+            files = entry_files,
+            link_to_subpaths = link_to_subpaths,
         )
 
     return venv_symlinks.values()
diff --git a/tests/base_rules/py_info/py_info_tests.bzl b/tests/base_rules/py_info/py_info_tests.bzl
index 087b115..d27a336 100644
--- a/tests/base_rules/py_info/py_info_tests.bzl
+++ b/tests/base_rules/py_info/py_info_tests.bzl
@@ -181,6 +181,7 @@
     symlink.set_venv_path("test_path")
     symlink.set_link_to_path("test_target")
     symlink.files.add(trans)
+    symlink.link_to_subpaths.add("sub/mod.py")
 
     builder.merge_target(targets.py1)
     builder.merge_targets([targets.py2])
@@ -307,6 +308,7 @@
             env.expect.that_str(entry.link_to_path).equals("test_target")
             env.expect.that_str(entry.kind).equals(VenvSymlinkKind.LIB)
             env.expect.that_collection(entry.files.to_list()).contains_exactly([trans])
+            env.expect.that_collection(entry.link_to_subpaths.to_list()).contains_exactly(["sub/mod.py"])
             env.expect.that_bool(entry.package == None).equals(True)
             env.expect.that_bool(entry.version == None).equals(True)
             env.expect.that_bool(entry.link_to_file == None).equals(True)
diff --git a/tests/pypi/whl_extract/BUILD.bazel b/tests/pypi/whl_extract/BUILD.bazel
index 3983477..cfbafe2 100644
--- a/tests/pypi/whl_extract/BUILD.bazel
+++ b/tests/pypi/whl_extract/BUILD.bazel
@@ -1,4 +1,5 @@
 load("@rules_shell//shell:sh_test.bzl", "sh_test")
+load("//python:py_test.bzl", "py_test")
 load(":whl_extract_tests.bzl", "whl_extract_test_suite")
 
 whl_extract_test_suite(name = "whl_extract_tests")
@@ -9,3 +10,9 @@
     args = ["$(location //python/private/pypi:wheel_record_rewriter)"],
     data = ["//python/private/pypi:wheel_record_rewriter"],
 )
+
+py_test(
+    name = "rewriters_test",
+    srcs = ["rewriters_test.py"],
+    deps = ["//python/private/pypi:rewriters_lib"],
+)
diff --git a/tests/pypi/whl_extract/rewriters_test.py b/tests/pypi/whl_extract/rewriters_test.py
new file mode 100644
index 0000000..4f4988c
--- /dev/null
+++ b/tests/pypi/whl_extract/rewriters_test.py
@@ -0,0 +1,102 @@
+"""Unit tests for venv_shebang_rewriter and wheel_record_rewriter helpers."""
+
+from __future__ import annotations
+
+import tempfile
+import unittest
+from pathlib import Path
+
+from python.private.pypi import venv_shebang_rewriter, wheel_record_rewriter
+
+
+class RewritersTest(unittest.TestCase):
+    def test_rewrite_shebang_posix_and_windows(self) -> None:
+        src = b"#!python\nimport sys\nprint(sys.executable)\n"
+        posix_out = venv_shebang_rewriter.rewrite_shebang_bytes(src, "linux")
+        self.assertTrue(posix_out.startswith(b"#!/bin/sh\n'''exec' "))
+        self.assertTrue(posix_out.endswith(b"import sys\nprint(sys.executable)\n"))
+
+        win_out = venv_shebang_rewriter.rewrite_shebang_bytes(src, "windows")
+        self.assertIn(b"python.exe", win_out)
+        self.assertTrue(win_out.endswith(b"import sys\nprint(sys.executable)\n"))
+
+        gui_out = venv_shebang_rewriter.rewrite_shebang_bytes(
+            b"#!pythonw\nprint(1)\n", "windows"
+        )
+        self.assertIn(b"pythonw.exe", gui_out)
+
+    def test_shebang_main_writes_executable_file(self) -> None:
+        with tempfile.TemporaryDirectory() as tmp:
+            in_file = Path(tmp) / "in_script"
+            out_file = Path(tmp) / "out_script"
+            in_file.write_bytes(b"#!python\nprint('hi')\n")
+            venv_shebang_rewriter.main([str(in_file), str(out_file), "linux"])
+            self.assertIn(b"#!/bin/sh\n", out_file.read_bytes())
+
+    def test_rewrite_record_lines_posix_and_windows(self) -> None:
+        lines = [
+            "pkg/__init__.py,sha256=abc,10\r\n",
+            "pkg-1.0.data/purelib/pure.py,sha256=def,20\n",
+            "pkg-1.0.data/scripts/mytool,sha256=ghi,30\n",
+            "pkg-1.0.data/scripts/native_bin,sha256=jkl,40\n",
+            '"pkg-1.0.data/scripts/quoted,tool",sha256=mno,50\n',
+            "pkg-1.0.data/headers/hdr.h,sha256=pqr,60\n",
+            "pkg-1.0.data/data/share/doc.txt,sha256=stu,70\n",
+        ]
+        posix = wheel_record_rewriter.rewrite_record_lines(
+            lines, "linux", "pkg-1.0.data", {"mytool", "quoted,tool"}
+        )
+        self.assertEqual(
+            posix,
+            [
+                "pkg/__init__.py,sha256=abc,10\n",
+                "pure.py,sha256=def,20\n",
+                "../../../bin/mytool,sha256=ghi,30\n",
+                "../../../bin/native_bin,sha256=jkl,40\n",
+                '"../../../bin/quoted,tool",sha256=mno,50\n',
+                "../../../include/hdr.h,sha256=pqr,60\n",
+                "../../../share/doc.txt,sha256=stu,70\n",
+            ],
+        )
+
+        windows = wheel_record_rewriter.rewrite_record_lines(
+            lines, "windows", "pkg-1.0.data", {"mytool", "quoted,tool"}
+        )
+        self.assertEqual(
+            windows,
+            [
+                "pkg/__init__.py,sha256=abc,10\n",
+                "pure.py,sha256=def,20\n",
+                "../../Scripts/mytool.bat,sha256=ghi,30\n",
+                "../../Scripts/native_bin,sha256=jkl,40\n",
+                '"../../Scripts/quoted,tool.bat",sha256=mno,50\n',
+                "../../Include/hdr.h,sha256=pqr,60\n",
+                "../../share/doc.txt,sha256=stu,70\n",
+            ],
+        )
+
+    def test_record_main_writes_lf_newlines(self) -> None:
+        with tempfile.TemporaryDirectory() as tmp:
+            in_file = Path(tmp) / "RECORD.in"
+            out_file = Path(tmp) / "RECORD"
+            in_file.write_bytes(b"pkg-1.0.data/scripts/tool,sha256=a,1\r\n")
+            wheel_record_rewriter.main(
+                [str(in_file), str(out_file), "windows", "pkg-1.0.data", "tool"]
+            )
+            self.assertEqual(
+                out_file.read_bytes(),
+                b"../../Scripts/tool.bat,sha256=a,1\n",
+            )
+
+    def test_malformed_quoted_scripts_entry_raises(self) -> None:
+        with self.assertRaises(ValueError):
+            wheel_record_rewriter.rewrite_record_lines(
+                ['"pkg-1.0.data/scripts/broken,sha256=x,1\n'],
+                "windows",
+                "pkg-1.0.data",
+                set(),
+            )
+
+
+if __name__ == "__main__":
+    unittest.main()
diff --git a/tests/venv_site_packages_libs/app_files_building/BUILD.bazel b/tests/venv_site_packages_libs/app_files_building/BUILD.bazel
index 60afd34..60a5d37 100644
--- a/tests/venv_site_packages_libs/app_files_building/BUILD.bazel
+++ b/tests/venv_site_packages_libs/app_files_building/BUILD.bazel
@@ -1,5 +1,91 @@
-load(":app_files_building_tests.bzl", "app_files_building_test_suite")
+load("//python:py_binary.bzl", "py_binary")
+load("//python:pytest_test.bzl", "pytest_test")
+load("//tests/support:support.bzl", "NOT_WINDOWS")
+load(":app_files_building_tests.bzl", "app_files_building_test_suite", "tree_venv_lib")
+
+py_binary(
+    name = "populate_tree",
+    srcs = ["populate_tree.py"],
+    main = "populate_tree.py",
+)
 
 app_files_building_test_suite(
     name = "app_files_building_tests",
 )
+
+_PKGUTIL_INIT = "__path__ = __import__('pkgutil').extend_path(__path__, __name__)\n"
+
+tree_venv_lib(
+    name = "tree_venv_runtime_a",
+    contents = {
+        "site-packages/foo/__init__.py": "ORIGIN = 'tree_a'\n",
+        "site-packages/foo/a.py": "VALUE = 'from_a'\n",
+        "site-packages/foo/plugins/__init__.py": "",
+        "site-packages/foo/plugins/shared.py": "WINNER = 'tree_a'\n",
+        "site-packages/pkgutil_ns/__init__.py": _PKGUTIL_INIT,
+        "site-packages/pkgutil_ns/one.py": "VALUE = 1\n",
+    },
+    namespace_package_files = ["site-packages/pkgutil_ns/__init__.py"],
+    package = "tree-a",
+)
+
+tree_venv_lib(
+    name = "tree_venv_runtime_b",
+    contents = {
+        "site-packages/foo/plugins/__init__.py": "",
+        "site-packages/foo/plugins/b.py": "VALUE = 'from_b'\n",
+        "site-packages/foo/plugins/shared.py": "WINNER = 'tree_b'\n",
+        "site-packages/pkgutil_ns/__init__.py": _PKGUTIL_INIT,
+        "site-packages/pkgutil_ns/two.py": "VALUE = 2\n",
+    },
+    namespace_package_files = ["site-packages/pkgutil_ns/__init__.py"],
+    package = "tree-b",
+)
+
+pytest_test(
+    name = "tree_venv_merge_test",
+    srcs = ["tree_venv_merge_test.py"],
+    config_settings = select({
+        "@platforms//os:windows": {
+            "//python/config_settings:bootstrap_impl": "system_python",
+            "//python/config_settings:venvs_site_packages": "yes",
+        },
+        "//conditions:default": {
+            "//python/config_settings:bootstrap_impl": "script",
+            "//python/config_settings:venvs_site_packages": "yes",
+        },
+    }),
+    deps = [
+        ":tree_venv_runtime_a",
+        ":tree_venv_runtime_b",
+    ],
+)
+
+tree_venv_lib(
+    name = "tree_ext_with_libs",
+    contents = {
+        "site-packages/tree_ext_with_libs/__init__.py": "",
+    },
+    files = {
+        "//tests/venv_site_packages_libs/ext_with_libs:adder": "site-packages/tree_ext_with_libs",
+        "//tests/venv_site_packages_libs/ext_with_libs:increment": "site-packages/tree_ext_with_libs/libs",
+    },
+    package = "tree-ext-with-libs",
+    tags = ["manual"],
+    target_compatible_with = NOT_WINDOWS,
+)
+
+pytest_test(
+    name = "tree_shared_lib_loading_test",
+    srcs = ["tree_shared_lib_loading_test.py"],
+    config_settings = {
+        "//python/config_settings:bootstrap_impl": "script",
+        "//python/config_settings:venvs_site_packages": "yes",
+    },
+    target_compatible_with = NOT_WINDOWS,
+    deps = [
+        ":tree_ext_with_libs",
+        "@dev_pip//macholib",
+        "@dev_pip//pyelftools",
+    ],
+)
diff --git a/tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl b/tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl
index 663ab69..4754e75 100644
--- a/tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl
+++ b/tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl
@@ -1,10 +1,13 @@
 ""
 
+load("@bazel_skylib//lib:types.bzl", "types")
 load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
 load("@rules_testing//lib:test_suite.bzl", "test_suite")
+load("@rules_testing//lib:util.bzl", rt_util = "util")
 load("//python:py_info.bzl", "PyInfo", "VenvSymlinkEntry", "VenvSymlinkKind")
 load("//python:py_library.bzl", "py_library")
 load("//python/private:common_labels.bzl", "labels")  # buildifier: disable=bzl-visibility
+load("//python/private:py_info.bzl", "PyInfoBuilder")  # buildifier: disable=bzl-visibility
 load("//python/private:venv_runfiles.bzl", "build_link_map", "get_venv_symlinks")  # buildifier: disable=bzl-visibility
 load("//tests/support:support.bzl", "SUPPORTS_BZLMOD")
 
@@ -39,9 +42,11 @@
         label = Label("@@FAKE-CTX//:fake_ctx"),
     )
 
-def _file(short_path):
+def _file(short_path, is_directory = False):
     return struct(
         short_path = short_path,
+        is_directory = is_directory,
+        owner = Label("//:fake_ctx"),
     )
 
 def _venv_symlink(venv_path, *, link_to_path = None, files = []):
@@ -74,6 +79,109 @@
         **kwargs
     )
 
+def _assert_tree_artifact_equivalence(
+        env,
+        test_ctx,
+        files,
+        file_entries,
+        package,
+        version_str,
+        namespace_package_files = []):
+    """Verifies at analysis time that tree-mode matches file-mode venv entries.
+
+    Checks using fake `File` structs that `get_venv_symlinks(tree_artifact = ...)`
+    and `build_link_map` produce entries and conflict-merged link maps equivalent
+    to file mode for the same package layout. This is an analysis-only check and
+    does not load modules or shared libraries at runtime; see
+    `//tests/venv_site_packages_libs/app_files_building:tree_shared_lib_loading_test`
+    (and `:tree_venv_merge_test`) for runtime verification.
+
+    Args:
+        env: `rules_testing` analysis test environment.
+        test_ctx: Fake rule context struct from `_ctx()`.
+        files: Fake `File` structs passed to file-mode `get_venv_symlinks`.
+        file_entries: Expected `VenvSymlinkEntry` list produced in file mode.
+        package: Distribution name.
+        version_str: Distribution version string.
+        namespace_package_files: Optional fake `File` structs for pkgutil-style
+            namespace package `__init__.py` files.
+    """
+    pkg_prefix = env.ctx.label.package + "/"
+    tree_artifact = _file(pkg_prefix + "srcs", is_directory = True)
+    tree_files = [f.short_path[len(pkg_prefix):] for f in files]
+    tree_ns_files = [f.short_path[len(pkg_prefix):] for f in namespace_package_files]
+    tree_entries = get_venv_symlinks(
+        test_ctx,
+        tree_files,
+        package = package,
+        version_str = version_str,
+        site_packages_root = "site-packages",
+        namespace_package_files = tree_ns_files,
+        tree_artifact = tree_artifact,
+    )
+    file_prefix = "{}/{}site-packages/".format(test_ctx.workspace_name, pkg_prefix)
+    tree_prefix = "{}/{}srcs/site-packages/".format(test_ctx.workspace_name, pkg_prefix)
+    file_summary = sorted([
+        (
+            e.kind,
+            e.venv_path,
+            e.package,
+            e.version,
+            e.link_to_path.replace(file_prefix, tree_prefix),
+        )
+        for e in file_entries
+    ])
+    tree_summary = sorted([
+        (
+            e.kind,
+            e.venv_path,
+            e.package,
+            e.version,
+            e.link_to_path,
+        )
+        for e in tree_entries
+    ])
+    env.expect.that_collection(tree_summary).contains_exactly(file_summary)
+
+    # Make another package conflict with every entry: it adds a file under each
+    # directory entry and claims the same path as each single-file entry (a
+    # file can't have anything created under it).
+    dir_entries = {
+        (e.kind, e.venv_path): True
+        for e in tree_entries
+        if e.link_to_subpaths.to_list()
+    }
+    other_entries = []
+    for e in file_entries:
+        other_path = "+other/site-packages/" + e.venv_path
+        if (e.kind, e.venv_path) in dir_entries:
+            other_file = other_path + "/__other__.py"
+        else:
+            other_file = other_path
+        other_entries.append(_entry(
+            e.venv_path,
+            other_path,
+            [_file("../" + other_file)],
+            kind = e.kind,
+        ))
+    file_merged = build_link_map(test_ctx, list(file_entries) + other_entries)
+    tree_merged = build_link_map(test_ctx, list(tree_entries) + other_entries)
+    for kind, kind_map in file_merged.items():
+        expected_resolved = {}
+        for k, v in kind_map.items():
+            if types.is_string(v):
+                expected_resolved[k] = v.replace(file_prefix, tree_prefix)
+            else:
+                expected_resolved[k] = "{}/{}".format(
+                    test_ctx.workspace_name,
+                    v.short_path,
+                ).replace(file_prefix, tree_prefix)
+        actual_resolved = {
+            k: v if types.is_string(v) else "{}/{}".format(test_ctx.workspace_name, v.short_path)
+            for k, v in tree_merged[kind].items()
+        }
+        env.expect.that_dict(actual_resolved).contains_exactly(expected_resolved)
+
 def _test_conflict_merging(name):
     analysis_test(
         name = name,
@@ -113,6 +221,26 @@
         _entry("ff/fmod.py", "+ff_b/site-packages/ff/fmod.py", [
             _file("../+ff_b/site-packages/ff/fmod.py"),
         ]),
+        # Case: two TreeArtifact distributions provide overlapping directories via link_to_subpaths
+        _entry(
+            "tree_a",
+            "+tree_a/srcs/site-packages/tree_a",
+            [_file("../+tree_a/srcs", is_directory = True)],
+            link_to_subpaths = depset(["a.txt"]),
+        ),
+        _entry(
+            "tree_a/b",
+            "+tree_a_b/srcs/site-packages/tree_a/b",
+            [_file("../+tree_a_b/srcs", is_directory = True)],
+            link_to_subpaths = depset(["b.txt"]),
+        ),
+        # Case: two TreeArtifact distributions provide the same pkgutil __init__.py
+        _entry("tree_ns/__init__.py", "+tree_a/srcs/site-packages/tree_ns/__init__.py", [
+            _file("../+tree_a/srcs", is_directory = True),
+        ]),
+        _entry("tree_ns/__init__.py", "+tree_b/srcs/site-packages/tree_ns/__init__.py", [
+            _file("../+tree_b/srcs", is_directory = True),
+        ]),
     ]
 
     actual, conflicts = build_link_map(_ctx(), entries, return_conflicts = True)
@@ -122,6 +250,9 @@
         "a/b/b.txt": _file("../+pypi_a_b/site-packages/a/b/b.txt"),
         "duplicate/d.py": _file("../+dupe_a/site-packages/duplicate/d.py"),
         "ff/fmod.py": _file("../+ff_a/site-packages/ff/fmod.py"),
+        "tree_a/a.txt": "+tree_a/srcs/site-packages/tree_a/a.txt",
+        "tree_a/b/b.txt": "+tree_a_b/srcs/site-packages/tree_a/b/b.txt",
+        "tree_ns/__init__.py": "+tree_a/srcs/site-packages/tree_ns/__init__.py",
         "x/p/p.txt": _file("src-dev/x/p/p.txt"),
         "x/x.txt": _file("src/x/x.txt"),
     }
@@ -169,6 +300,14 @@
         version_str = "1.0",
         site_packages_root = env.ctx.label.package + "/site-packages",
     )
+    _assert_tree_artifact_equivalence(
+        env,
+        test_ctx,
+        target.files.to_list(),
+        entries,
+        package = "pkg1",
+        version_str = "1.0",
+    )
     actual = _venv_symlinks_from_entries(entries)
 
     rr = "{}/{}/site-packages/".format(test_ctx.workspace_name, env.ctx.label.package)
@@ -259,6 +398,14 @@
         version_str = "1.0",
         site_packages_root = env.ctx.label.package + "/site-packages",
     )
+    _assert_tree_artifact_equivalence(
+        env,
+        test_ctx,
+        target.files.to_list(),
+        entries,
+        package = "pkg2",
+        version_str = "1.0",
+    )
     actual = _venv_symlinks_from_entries(entries)
 
     rr = "{}/{}/site-packages/".format(test_ctx.workspace_name, env.ctx.label.package)
@@ -312,6 +459,14 @@
         version_str = "1.0",
         site_packages_root = env.ctx.label.package + "/site-packages",
     )
+    _assert_tree_artifact_equivalence(
+        env,
+        test_ctx,
+        target.files.to_list(),
+        entries,
+        package = "pkg3",
+        version_str = "1.0",
+    )
     actual = _venv_symlinks_from_entries(entries)
 
     rr = "{}/{}/site-packages/".format(test_ctx.workspace_name, env.ctx.label.package)
@@ -380,6 +535,15 @@
         site_packages_root = env.ctx.label.package + "/site-packages",
         namespace_package_files = ns_inits,
     )
+    _assert_tree_artifact_equivalence(
+        env,
+        test_ctx,
+        files,
+        entries,
+        package = "pkgutilns",
+        version_str = "1.0",
+        namespace_package_files = ns_inits,
+    )
     actual = _venv_symlinks_from_entries(entries)
 
     rr = "{}/{}/site-packages/".format(test_ctx.workspace_name, env.ctx.label.package)
@@ -727,6 +891,14 @@
         version_str = "1.0",
         site_packages_root = env.ctx.label.package + "/site-packages",
     )
+    _assert_tree_artifact_equivalence(
+        env,
+        _ctx(),
+        srcs,
+        actual_entries,
+        package = "foo",
+        version_str = "1.0",
+    )
 
     actual = _venv_symlinks_from_entries(actual_entries)
 
@@ -756,6 +928,321 @@
     }
     env.expect.that_dict(actual[VenvSymlinkKind.LIB]).contains_at_least(expected_libs)
 
+def _test_default_mode_directory_merge(name):
+    analysis_test(
+        name = name,
+        impl = _test_default_mode_directory_merge_impl,
+        target = "//python:none",
+    )
+
+_tests.append(_test_default_mode_directory_merge)
+
+def _test_default_mode_directory_merge_impl(env, _):
+    test_ctx = _ctx()
+    srcs = [
+        _file("pkg/site-packages/foo/__init__.py"),
+        _file("pkg/site-packages/foo/gen_dir", is_directory = True),
+        _file("pkg/site-packages/foo/bar.py"),
+    ]
+    entries = get_venv_symlinks(
+        test_ctx,
+        srcs,
+        package = "foo",
+        version_str = "1.0",
+        site_packages_root = "pkg/site-packages",
+    )
+    env.expect.that_int(len(entries)).equals(1)
+    env.expect.that_collection(entries[0].link_to_subpaths.to_list()).contains_exactly([])
+
+    conflicting = _entry(
+        "foo",
+        "+other/site-packages/foo",
+        [_file("../+other/site-packages/foo/other.py")],
+    )
+    merged = build_link_map(test_ctx, list(entries) + [conflicting])
+    env.expect.that_dict(merged[VenvSymlinkKind.LIB]).contains_exactly({
+        "foo/__init__.py": srcs[0],
+        "foo/bar.py": srcs[2],
+        "foo/gen_dir": srcs[1],
+        "foo/other.py": _file("../+other/site-packages/foo/other.py"),
+    })
+
+    # Direct-link case: a directory File that is itself the top-level venv entry
+    # must remain the File object (not a path string) after conflict merging so
+    # default mode materializes a runfiles root_symlink rather than
+    # ctx.actions.declare_symlink().
+    direct_dir = _file("pkg/site-packages/foo", is_directory = True)
+    direct_entries = get_venv_symlinks(
+        test_ctx,
+        [direct_dir],
+        package = "foo",
+        version_str = "1.0",
+        site_packages_root = "pkg/site-packages",
+    )
+    env.expect.that_int(len(direct_entries)).equals(1)
+    other_direct_dir = _entry(
+        "foo",
+        "+other/site-packages/foo",
+        [_file("../+other/site-packages/foo", is_directory = True)],
+    )
+    direct_merged = build_link_map(test_ctx, list(direct_entries) + [other_direct_dir])
+    env.expect.that_bool(types.is_string(direct_merged[VenvSymlinkKind.LIB]["foo"])).equals(False)
+    env.expect.that_dict(direct_merged[VenvSymlinkKind.LIB]).contains_exactly({
+        "foo": direct_dir,
+    })
+
+def _test_unmergeable_overlap_fails(name):
+    analysis_test(
+        name = name,
+        impl = _test_unmergeable_overlap_fails_impl,
+        target = "//python:none",
+    )
+
+_tests.append(_test_unmergeable_overlap_fails)
+
+def _test_unmergeable_overlap_fails_impl(env, _):
+    test_ctx = _ctx()
+    other = _entry(
+        "foo",
+        "+other/site-packages/foo",
+        [_file("../+other/site-packages/foo/other.py")],
+        package = "other",
+    )
+
+    # A directly linked directory File: `foo` would have to be both a symlink
+    # and a directory containing `other.py`.
+    direct_dir = _entry(
+        "foo",
+        "_main/pkg/site-packages/foo",
+        [_file("pkg/site-packages/foo", is_directory = True)],
+        package = "direct",
+    )
+    failures = []
+    build_link_map(test_ctx, [direct_dir, other], _fail = failures.append)
+    env.expect.that_collection(failures).has_size(1)
+    env.expect.that_str(failures[0]).contains("'foo/other.py'")
+    env.expect.that_str(failures[0]).contains("(package: direct)")
+    env.expect.that_str(failures[0]).contains("(package: other)")
+
+    # A directory inside a directory artifact without link_to_subpaths: its
+    # files aren't known, so it can't be merged file by file either. The
+    # result doesn't depend on the order of the entries.
+    tree_dir = _entry(
+        "foo",
+        "_main/pkg/srcs/site-packages/foo",
+        [_file("pkg/srcs", is_directory = True)],
+        package = "tree",
+    )
+    failures = []
+    build_link_map(test_ctx, [other, tree_dir], _fail = failures.append)
+    env.expect.that_collection(failures).has_size(1)
+    env.expect.that_str(failures[0]).contains("'foo/other.py'")
+    env.expect.that_str(failures[0]).contains("(package: tree)")
+
+    # The single unit can be any ancestor, not only the immediate parent.
+    other_subdir = _entry(
+        "foo",
+        "+other/site-packages/foo",
+        [
+            _file("../+other/site-packages/foo/sub/a.py"),
+            _file("../+other/site-packages/foo/sub/b.py"),
+        ],
+        package = "other",
+    )
+    failures = []
+    build_link_map(test_ctx, [tree_dir, other_subdir], _fail = failures.append)
+    env.expect.that_collection(failures).has_size(1)
+    env.expect.that_str(failures[0]).contains("'foo/sub/a.py'")
+
+    # With link_to_subpaths, the same layout merges.
+    tree_dir_with_subpaths = _entry(
+        "foo",
+        "_main/pkg/srcs/site-packages/foo",
+        [_file("pkg/srcs", is_directory = True)],
+        package = "tree",
+        link_to_subpaths = depset(["__init__.py"]),
+    )
+    failures = []
+    merged = build_link_map(
+        test_ctx,
+        [tree_dir_with_subpaths, other],
+        _fail = failures.append,
+    )
+    env.expect.that_collection(failures).has_size(0)
+    env.expect.that_dict(merged[VenvSymlinkKind.LIB]).contains_exactly({
+        "foo/__init__.py": "_main/pkg/srcs/site-packages/foo/__init__.py",
+        "foo/other.py": _file("../+other/site-packages/foo/other.py"),
+    })
+
+def _tree_venv_lib_impl(ctx):
+    """Populates a directory artifact and returns tree-backed `PyInfo.venv_symlinks`."""
+    out = ctx.actions.declare_directory(ctx.label.name + "_srcs")
+    args = ctx.actions.args()
+    args.add_all("--output-dir", [out], expand_directories = False)
+    for p in ctx.attr.paths:
+        args.add("--path", p)
+    for rel_path, content in ctx.attr.contents.items():
+        args.add("--entry", "{}={}".format(rel_path, content))
+
+    inputs = []
+    copied_paths = []
+    for src_target, dest_dir in ctx.attr.files.items():
+        clean_dir = dest_dir.strip("/")
+        for src_file in src_target[DefaultInfo].files.to_list():
+            if clean_dir:
+                rel_path = "{}/{}".format(clean_dir, src_file.basename)
+            else:
+                rel_path = src_file.basename
+            args.add("--copy", "{}={}".format(rel_path, src_file.path))
+            inputs.append(src_file)
+            copied_paths.append(rel_path)
+
+    ctx.actions.run(
+        executable = ctx.executable._populate_tree,
+        inputs = inputs,
+        arguments = [args],
+        outputs = [out],
+    )
+    all_paths = sorted({
+        p: True
+        for p in ctx.attr.paths + ctx.attr.contents.keys() + copied_paths
+    }.keys())
+    entries = get_venv_symlinks(
+        ctx,
+        all_paths,
+        package = ctx.attr.package,
+        version_str = ctx.attr.version,
+        site_packages_root = "site-packages",
+        namespace_package_files = ctx.attr.namespace_package_files,
+        tree_artifact = out,
+    )
+    builder = PyInfoBuilder.new()
+    builder.transitive_sources.add([out])
+    builder.venv_symlinks.add(entries)
+    return [
+        DefaultInfo(
+            files = depset([out]),
+            runfiles = ctx.runfiles(files = [out]),
+        ),
+        builder.build(),
+    ]
+
+tree_venv_lib = rule(
+    implementation = _tree_venv_lib_impl,
+    doc = """
+Test-only rule that writes the given paths, contents, and files into a
+directory artifact (`TreeArtifact`) and returns a `PyInfo` whose
+`venv_symlinks` are tree-backed via `get_venv_symlinks(tree_artifact = ...)`,
+standing in for a target that installs a package into a directory artifact at
+build time.
+""",
+    attrs = {
+        "contents": attr.string_dict(
+            doc = "Mapping of tree-relative file paths to UTF-8 text contents.",
+        ),
+        "files": attr.label_keyed_string_dict(
+            allow_files = True,
+            doc = """
+Mapping of source file targets to destination directories inside the tree
+artifact; each file's basename is preserved.
+""",
+        ),
+        "namespace_package_files": attr.string_list(
+            doc = """
+Tree-relative paths to pkgutil-style namespace `__init__.py` files.
+""",
+        ),
+        "package": attr.string(
+            doc = "Python distribution name passed to `get_venv_symlinks`.",
+            mandatory = True,
+        ),
+        "paths": attr.string_list(
+            doc = "Tree-relative paths to create as empty files.",
+        ),
+        "version": attr.string(
+            default = "1.0",
+            doc = "Python distribution version passed to `get_venv_symlinks`.",
+        ),
+        "_populate_tree": attr.label(
+            default = ":populate_tree",
+            executable = True,
+            cfg = "exec",
+        ),
+    },
+)
+
+def _test_tree_artifact_rule_merging(name):
+    rt_util.helper_target(
+        tree_venv_lib,
+        name = name + "_tree_a",
+        package = "tree_a",
+        paths = [
+            "bin/tool_a",
+            "site-packages/foo/__init__.py",
+            "site-packages/foo/a.py",
+            "site-packages/foo/plugins/shared.py",
+            "site-packages/libnative.so",
+            "site-packages/pkgutil_ns/__init__.py",
+            "site-packages/pkgutil_ns/one.py",
+        ],
+        namespace_package_files = ["site-packages/pkgutil_ns/__init__.py"],
+    )
+    rt_util.helper_target(
+        tree_venv_lib,
+        name = name + "_tree_b",
+        package = "tree_b",
+        paths = [
+            "site-packages/foo/plugins/b.py",
+            "site-packages/foo/plugins/shared.py",
+            "site-packages/pkgutil_ns/__init__.py",
+            "site-packages/pkgutil_ns/two.py",
+        ],
+        namespace_package_files = ["site-packages/pkgutil_ns/__init__.py"],
+    )
+    rt_util.helper_target(
+        py_library,
+        name = name + "_combined",
+        deps = [
+            ":" + name + "_tree_a",
+            ":" + name + "_tree_b",
+        ],
+    )
+    analysis_test(
+        name = name,
+        impl = _test_tree_artifact_rule_merging_impl,
+        target = name + "_combined",
+    )
+
+_tests.append(_test_tree_artifact_rule_merging)
+
+def _test_tree_artifact_rule_merging_impl(env, target):
+    entries = target[PyInfo].venv_symlinks.to_list()
+    all_dirs = []
+    for e in entries:
+        for f in e.files.to_list():
+            all_dirs.append(f.is_directory)
+    env.expect.that_bool(all(all_dirs) and len(all_dirs) > 0).equals(True)
+
+    test_ctx = _ctx(workspace_name = env.ctx.workspace_name)
+    pkg = env.ctx.label.package
+    tree_a_prefix = "{}/{}/{}_tree_a_srcs/".format(env.ctx.workspace_name, pkg, env.ctx.label.name)
+    tree_b_prefix = "{}/{}/{}_tree_b_srcs/".format(env.ctx.workspace_name, pkg, env.ctx.label.name)
+
+    merged = build_link_map(test_ctx, entries)
+    env.expect.that_dict(merged[VenvSymlinkKind.LIB]).contains_exactly({
+        "foo/__init__.py": tree_a_prefix + "site-packages/foo/__init__.py",
+        "foo/a.py": tree_a_prefix + "site-packages/foo/a.py",
+        "foo/plugins/b.py": tree_b_prefix + "site-packages/foo/plugins/b.py",
+        "foo/plugins/shared.py": tree_a_prefix + "site-packages/foo/plugins/shared.py",
+        "libnative.so": tree_a_prefix + "site-packages/libnative.so",
+        "pkgutil_ns/__init__.py": tree_a_prefix + "site-packages/pkgutil_ns/__init__.py",
+        "pkgutil_ns/one.py": tree_a_prefix + "site-packages/pkgutil_ns/one.py",
+        "pkgutil_ns/two.py": tree_b_prefix + "site-packages/pkgutil_ns/two.py",
+    })
+    env.expect.that_dict(merged[VenvSymlinkKind.BIN]).contains_exactly({
+        "tool_a": tree_a_prefix + "bin/tool_a",
+    })
+
 def app_files_building_test_suite(name):
     test_suite(
         name = name,
diff --git a/tests/venv_site_packages_libs/app_files_building/populate_tree.py b/tests/venv_site_packages_libs/app_files_building/populate_tree.py
new file mode 100644
index 0000000..8879f61
--- /dev/null
+++ b/tests/venv_site_packages_libs/app_files_building/populate_tree.py
@@ -0,0 +1,54 @@
+"""Populates a declared directory artifact with paths, text entries, and files.
+
+Usage:
+    populate_tree.py --output-dir <dir> [--path <rel_path> ...]
+        [--entry <rel_path>=<text> ...] [--copy <rel_path>=<src_path> ...]
+
+Input format:
+    `--path` creates an empty file at `<dir>/<rel_path>`; `--entry` writes UTF-8
+    text; `--copy` copies `<src_path>` to `<dir>/<rel_path>`.
+"""
+
+from __future__ import annotations
+
+import argparse
+import os
+import shutil
+from collections.abc import Sequence
+from pathlib import Path
+
+
+def main(argv: Sequence[str] | None = None) -> None:
+    parser = argparse.ArgumentParser(description="Populate a test TreeArtifact.")
+    parser.add_argument("--output-dir", type=Path, required=True)
+    parser.add_argument("--path", action="append", dest="paths", default=[])
+    parser.add_argument("--entry", action="append", dest="entries", default=[])
+    parser.add_argument("--copy", action="append", dest="copies", default=[])
+    args = parser.parse_args(argv)
+
+    args.output_dir.mkdir(parents=True, exist_ok=True)
+    contents: dict[str, str] = {p: "" for p in args.paths}
+    for item in args.entries:
+        rel_path, _, text = item.partition("=")
+        contents[rel_path] = text
+
+    for rel_path, text in sorted(contents.items()):
+        dest = args.output_dir / rel_path
+        dest.parent.mkdir(parents=True, exist_ok=True)
+        with open(dest, "w", encoding="utf-8", newline="\n") as out_file:
+            out_file.write(text)
+
+    for item in args.copies:
+        rel_path, _, src_str = item.partition("=")
+        src_path = Path(src_str)
+        dest = args.output_dir / rel_path
+        dest.parent.mkdir(parents=True, exist_ok=True)
+        if dest.exists():
+            dest.unlink()
+        shutil.copyfile(src_path, dest)
+        if os.access(src_path, os.X_OK):
+            dest.chmod(0o755)
+
+
+if __name__ == "__main__":
+    main()
diff --git a/tests/venv_site_packages_libs/app_files_building/tree_shared_lib_loading_test.py b/tests/venv_site_packages_libs/app_files_building/tree_shared_lib_loading_test.py
new file mode 100644
index 0000000..d45bee8
--- /dev/null
+++ b/tests/venv_site_packages_libs/app_files_building/tree_shared_lib_loading_test.py
@@ -0,0 +1,209 @@
+"""Runtime test loading C extensions and shared libraries from a tree-backed venv."""
+
+from __future__ import annotations
+
+import ctypes
+import importlib
+import importlib.util
+import os
+import sys
+from pathlib import Path
+
+# Platform-conditional binary header parsers (pyelftools on Linux, macholib on macOS).
+if os.name == "posix" and sys.platform != "darwin":
+    from elftools.elf.elffile import ELFFile  # pyrefly: ignore[missing-import]
+else:
+    ELFFile = None
+
+if sys.platform == "darwin":
+    from macholib import mach_o  # pyrefly: ignore[missing-import]
+    from macholib.MachO import MachO  # pyrefly: ignore[missing-import]
+else:
+    mach_o = None
+    MachO = None
+
+_ELF_MAGIC = b"\x7fELF"
+_MACHO_MAGICS = (
+    b"\xce\xfa\xed\xfe",  # 32-bit big-endian
+    b"\xcf\xfa\xed\xfe",  # 64-bit big-endian
+    b"\xfe\xed\xfa\xce",  # 32-bit little-endian
+    b"\xfe\xed\xfa\xcf",  # 64-bit little-endian
+)
+
+
+def _get_loaded_shared_libs() -> set[Path]:
+    if sys.platform == "darwin":
+        libsystem = ctypes.CDLL(None)
+        dyld_image_count = libsystem._dyld_image_count
+        dyld_image_count.argtypes = []
+        dyld_image_count.restype = ctypes.c_uint32
+        dyld_get_image_name = libsystem._dyld_get_image_name
+        dyld_get_image_name.argtypes = [ctypes.c_uint32]
+        dyld_get_image_name.restype = ctypes.c_char_p
+        loaded: set[Path] = set()
+        for idx in range(dyld_image_count()):
+            raw_name = dyld_get_image_name(idx)
+            if raw_name is not None:
+                loaded.add(Path(os.fsdecode(raw_name)).resolve())
+        return loaded
+
+    loaded = set()
+    with open("/proc/self/maps", encoding="utf-8") as maps_file:
+        for line in maps_file:
+            parts = line.split(maxsplit=5)
+            if len(parts) == 6 and parts[5].startswith("/"):
+                mapped = parts[5].strip()
+                if not mapped.endswith(" (deleted)"):
+                    loaded.add(Path(mapped).resolve())
+    return loaded
+
+
+def _get_elf_info(path: str) -> dict[str, list[str]]:
+    assert ELFFile is not None  # type assert
+    info: dict[str, list[str]] = {
+        "rpaths": [],
+        "needed": [],
+        "undefined_symbols": [],
+    }
+    with open(path, "rb") as f:
+        elf = ELFFile(f)
+        dynamic = elf.get_section_by_name(".dynamic")
+        if dynamic:
+            for tag in dynamic.iter_tags():
+                # pyelftools generates tag attributes dynamically by d_tag kind.
+                if tag.entry.d_tag == "DT_NEEDED":  # pyrefly: ignore[missing-attribute]
+                    info["needed"].append(tag.needed)  # pyrefly: ignore[missing-attribute]
+                elif tag.entry.d_tag == "DT_RPATH":  # pyrefly: ignore[missing-attribute]
+                    info["rpaths"].append(tag.rpath)  # pyrefly: ignore[missing-attribute]
+                elif tag.entry.d_tag == "DT_RUNPATH":  # pyrefly: ignore[missing-attribute]
+                    info["rpaths"].append(tag.runpath)  # pyrefly: ignore[missing-attribute]
+
+        dynsym = elf.get_section_by_name(".dynsym")
+        if dynsym:
+            info["undefined_symbols"] = [
+                s.name
+                for s in dynsym.iter_symbols()
+                if s.entry["st_shndx"] == "SHN_UNDEF"  # pyrefly: ignore[missing-attribute]
+            ]
+    return info
+
+
+def _get_macho_info(path: str) -> dict[str, list[str]]:
+    assert MachO is not None and mach_o is not None  # type assert
+    info: dict[str, list[str]] = {"rpaths": [], "needed": []}
+    macho = MachO(path)
+    for header in macho.headers:
+        for cmd_load, _, data in header.commands:
+            if cmd_load.cmd == mach_o.LC_LOAD_DYLIB:
+                info["needed"].append(data.decode().strip("\x00"))
+            elif cmd_load.cmd == mach_o.LC_RPATH:
+                info["rpaths"].append(data.decode().strip("\x00"))
+    return info
+
+
+def _get_linking_info(path: str) -> dict[str, list[str]]:
+    real_path = os.path.realpath(path)
+    with open(real_path, "rb") as f:
+        magic_bytes = f.read(4)
+
+    if magic_bytes == _ELF_MAGIC:
+        return _get_elf_info(real_path)
+    if magic_bytes in _MACHO_MAGICS:
+        return _get_macho_info(real_path)
+    return {}
+
+
+def _assert_elf_linking(path: str) -> None:
+    info = _get_elf_info(path)
+    assert "libincrement.so" in info["needed"]
+    assert "increment" in info["undefined_symbols"]
+    assert any("$ORIGIN/libs" in rpath for rpath in info["rpaths"]), (
+        f"Expected $ORIGIN/libs in ELF rpaths, got {info['rpaths']}"
+    )
+
+
+def _assert_macho_linking(path: str) -> None:
+    info = _get_macho_info(path)
+    assert "@rpath/libincrement.dylib" in info["needed"]
+    assert any("@loader_path/libs" in rpath for rpath in info["rpaths"]), (
+        f"Expected @loader_path/libs in Mach-O rpaths, got {info['rpaths']}"
+    )
+
+
+def test_tree_backed_shared_library_linking() -> None:
+    assert sys.prefix != sys.base_prefix, "Not running under a venv"
+    venv = Path(sys.prefix)
+
+    try:
+        adder = importlib.import_module("tree_ext_with_libs.adder")
+    except ImportError as err:
+        spec = importlib.util.find_spec("tree_ext_with_libs.adder")
+        if not spec or not spec.origin:
+            raise AssertionError(
+                f"Import failed and could not find module spec: {err}"
+            ) from err
+
+        info = _get_linking_info(spec.origin)
+        raise AssertionError(
+            f"Failed to import tree_ext_with_libs.adder.\n"
+            f"Original error: {err}\n"
+            f"Linking info for {spec.origin}:\n"
+            f"  RPATHs: {info.get('rpaths', 'N/A')}\n"
+            f"  Needed libs: {info.get('needed', 'N/A')}"
+        ) from err
+
+    assert adder.__file__ is not None
+    assert ".venv/" in adder.__file__
+
+    adder_venv_path = Path(adder.__file__)
+    assert str(adder_venv_path).startswith(str(venv)), (
+        f"Extension {adder_venv_path} was not loaded from venv {venv}"
+    )
+
+    # Shared libraries must be linked directly as file symlinks rather than
+    # via a directory symlink on the enclosing package or `libs/` directory.
+    pkg_dir = adder_venv_path.parent
+    libs_dir = pkg_dir / "libs"
+    libincrement_name = (
+        "libincrement.dylib" if sys.platform == "darwin" else "libincrement.so"
+    )
+    increment_venv_path = libs_dir / libincrement_name
+
+    assert pkg_dir.is_dir()
+    assert not pkg_dir.is_symlink(), (
+        f"Expected {pkg_dir} to be a real directory, not a symlink"
+    )
+    assert libs_dir.is_dir()
+    assert not libs_dir.is_symlink(), (
+        f"Expected {libs_dir} to be a real directory, not a symlink"
+    )
+    assert adder_venv_path.is_symlink(), (
+        f"Expected {adder_venv_path} to be a direct file symlink"
+    )
+    assert increment_venv_path.is_symlink(), (
+        f"Expected {increment_venv_path} to be a direct file symlink"
+    )
+
+    adder_real = adder_venv_path.resolve()
+    increment_real = increment_venv_path.resolve()
+    assert adder_real.parent / "libs" / libincrement_name == increment_real
+    assert "tree_ext_with_libs_srcs/" in adder_real.as_posix()
+    assert "tree_ext_with_libs_srcs/" in increment_real.as_posix()
+
+    with open(adder_real, "rb") as f:
+        magic_bytes = f.read(4)
+
+    if magic_bytes == _ELF_MAGIC:
+        _assert_elf_linking(str(adder_real))
+    elif magic_bytes in _MACHO_MAGICS:
+        _assert_macho_linking(str(adder_real))
+    else:
+        raise AssertionError(
+            f"Unsupported file format for adder: magic bytes {magic_bytes!r}"
+        )
+
+    assert adder.do_add() == 2
+
+    loaded_libs = _get_loaded_shared_libs()
+    assert adder_real in loaded_libs
+    assert increment_real in loaded_libs
diff --git a/tests/venv_site_packages_libs/app_files_building/tree_venv_merge_test.py b/tests/venv_site_packages_libs/app_files_building/tree_venv_merge_test.py
new file mode 100644
index 0000000..d633e76
--- /dev/null
+++ b/tests/venv_site_packages_libs/app_files_building/tree_venv_merge_test.py
@@ -0,0 +1,38 @@
+"""Runtime test verifying merged TreeArtifact-backed venv site-packages."""
+
+from __future__ import annotations
+
+import importlib
+import os
+import sys
+from pathlib import Path
+
+
+def _assert_in_venv(mod_file: str | None, mod_name: str) -> None:
+    assert mod_file is not None, f"Expected __file__ on {mod_name}"
+    actual = str(Path(mod_file)).lower() if os.name == "nt" else mod_file
+    prefix = str(Path(sys.prefix)).lower() if os.name == "nt" else sys.prefix
+    assert actual.startswith(prefix), (
+        f"{mod_name} ({mod_file}) was not loaded from venv ({sys.prefix})"
+    )
+
+
+def test_imports_from_merged_tree_artifacts() -> None:
+    assert sys.prefix != sys.base_prefix, "Expected to run inside a venv"
+
+    foo = importlib.import_module("foo")
+    foo_a = importlib.import_module("foo.a")
+    foo_b = importlib.import_module("foo.plugins.b")
+    foo_shared = importlib.import_module("foo.plugins.shared")
+    ns_one = importlib.import_module("pkgutil_ns.one")
+    ns_two = importlib.import_module("pkgutil_ns.two")
+
+    assert foo.ORIGIN == "tree_a"
+    assert foo_a.VALUE == "from_a"
+    assert foo_b.VALUE == "from_b"
+    assert foo_shared.WINNER == "tree_a"
+    assert ns_one.VALUE == 1
+    assert ns_two.VALUE == 2
+
+    for mod in (foo, foo_a, foo_b, foo_shared, ns_one, ns_two):
+        _assert_in_venv(mod.__file__, mod.__name__)