fix(whl_library): track sources in whl_library (#2526)

This is reusing a bit of code used in `evaluate_markers` and makes use
of the RECORD files in the `whl` files that we use to extract whls in
`whl_library`. This should be merged before #2514 to avoid any cache
invalidation issues downstream.

Fixes #2468
diff --git a/python/private/pypi/deps.bzl b/python/private/pypi/deps.bzl
index c6691d7..31a5201 100644
--- a/python/private/pypi/deps.bzl
+++ b/python/private/pypi/deps.bzl
@@ -124,6 +124,13 @@
 
 # Collate all the repository names so they can be easily consumed
 all_repo_names = [name for (name, _, _) in _RULE_DEPS]
+record_files = {
+    name: Label("@{}//:{}.dist-info/RECORD".format(
+        name,
+        url.rpartition("/")[-1].partition("-py3-none")[0],
+    ))
+    for (name, url, _) in _RULE_DEPS
+}
 
 def pypi_deps():
     """
diff --git a/python/private/pypi/evaluate_markers.bzl b/python/private/pypi/evaluate_markers.bzl
index c805fd7..ec5f576 100644
--- a/python/private/pypi/evaluate_markers.bzl
+++ b/python/private/pypi/evaluate_markers.bzl
@@ -14,13 +14,14 @@
 
 """A simple function that evaluates markers using a python interpreter."""
 
+load(":deps.bzl", "record_files")
 load(":pypi_repo_utils.bzl", "pypi_repo_utils")
 
 # Used as a default value in a rule to ensure we fetch the dependencies.
 SRCS = [
     # When the version, or any of the files in `packaging` package changes,
     # this file will change as well.
-    Label("@pypi__packaging//:packaging-24.0.dist-info/RECORD"),
+    record_files["pypi__packaging"],
     Label("//python/private/pypi/requirements_parser:resolve_target_platforms.py"),
     Label("//python/private/pypi/whl_installer:platform.py"),
 ]
diff --git a/python/private/pypi/whl_library.bzl b/python/private/pypi/whl_library.bzl
index 79a58a8..ef4077f 100644
--- a/python/private/pypi/whl_library.bzl
+++ b/python/private/pypi/whl_library.bzl
@@ -19,7 +19,7 @@
 load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
 load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
 load(":attrs.bzl", "ATTRS", "use_isolated")
-load(":deps.bzl", "all_repo_names")
+load(":deps.bzl", "all_repo_names", "record_files")
 load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel")
 load(":parse_whl_name.bzl", "parse_whl_name")
 load(":patch_whl.bzl", "patch_whl")
@@ -242,7 +242,7 @@
         else:
             op_tmpl = "whl_library.ResolveRequirement({name}, {requirement})"
 
-        repo_utils.execute_checked(
+        pypi_repo_utils.execute_checked(
             rctx,
             # truncate the requirement value when logging it / reporting
             # progress since it may contain several ' --hash=sha256:...
@@ -250,6 +250,7 @@
             op = op_tmpl.format(name = rctx.attr.name, requirement = rctx.attr.requirement.split(" ", 1)[0]),
             arguments = args,
             environment = environment,
+            srcs = rctx.attr._python_srcs,
             quiet = rctx.attr.quiet,
             timeout = rctx.attr.timeout,
             logger = logger,
@@ -291,13 +292,14 @@
                 )
             ]
 
-    repo_utils.execute_checked(
+    pypi_repo_utils.execute_checked(
         rctx,
         op = "whl_library.ExtractWheel({}, {})".format(rctx.attr.name, whl_path),
         arguments = args + [
             "--whl-file",
             whl_path,
         ] + ["--platform={}".format(p) for p in target_platforms],
+        srcs = rctx.attr._python_srcs,
         environment = environment,
         quiet = rctx.attr.quiet,
         timeout = rctx.attr.timeout,
@@ -450,6 +452,16 @@
             for repo in all_repo_names
         ],
     ),
+    "_python_srcs": attr.label_list(
+        # Used as a default value in a rule to ensure we fetch the dependencies.
+        default = [
+            Label("//python/private/pypi/whl_installer:platform.py"),
+            Label("//python/private/pypi/whl_installer:wheel.py"),
+            Label("//python/private/pypi/whl_installer:wheel_installer.py"),
+            Label("//python/private/pypi/whl_installer:arguments.py"),
+            Label("//python/private/pypi/whl_installer:namespace_pkgs.py"),
+        ] + record_files.values(),
+    ),
     "_rule_name": attr.string(default = "whl_library"),
 }, **ATTRS)
 whl_library_attrs.update(AUTH_ATTRS)