[8.0.0] Backport MODULE.bazel file changes (#24481)

- 30f5c27dc952724fcd5d4fef2c05c1f3c225dec5
- face2be
diff --git a/BUILD b/BUILD
index bd5db63..21477a3 100644
--- a/BUILD
+++ b/BUILD
@@ -2,6 +2,7 @@
 
 load("@bazel_skylib//rules:write_file.bzl", "write_file")
 load("@rules_license//rules:license.bzl", "license")
+load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_files")
 load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
 load("@rules_python//python:defs.bzl", "py_binary")
 load("//src/tools/bzlmod:utils.bzl", "get_canonical_repo_name")
@@ -155,16 +156,28 @@
     ],
 )
 
+# Bazel sources excluding files that are not needed in the distfile.
+pkg_files(
+    name = "dist-srcs",
+    srcs = ["//:srcs"],
+    attributes = pkg_attributes(mode = "0755"),
+    excludes = [
+        "//examples:srcs",
+        "//site:srcs",
+        "//src:srcs-to-exclude-in-distfile",
+    ],
+    renames = {
+        "MODULE.bazel.lock.dist": "MODULE.bazel.lock",
+    },
+    strip_prefix = "/",  # Ensure paths are relative to the workspace root.
+)
+
 pkg_tar(
     name = "bazel-srcs",
     srcs = [
+        ":dist-srcs",
         ":generated_resources",
-        ":srcs",
     ],
-    # TODO(aiuto): Replace with pkg_filegroup when that is available.
-    remap_paths = {
-        "MODULE.bazel.lock.dist": "MODULE.bazel.lock",
-    },
     strip_prefix = ".",
     # Public but bazel-only visibility.
     visibility = ["//:__subpackages__"],
diff --git a/MODULE.bazel b/MODULE.bazel
index cff405e..a205559 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -76,6 +76,7 @@
 # The following Bazel modules are not direct dependencies for building Bazel,
 # but are required for visibility from DIST_ARCHIVE_REPOS in repositories.bzl
 bazel_dep(name = "apple_support", version = "1.15.1")
+bazel_dep(name = "bazel_features", version = "1.21.0")
 bazel_dep(name = "c-ares", version = "1.15.0")
 bazel_dep(name = "rules_go", version = "0.48.0")
 bazel_dep(name = "rules_kotlin", version = "1.9.6")
@@ -361,16 +362,28 @@
 
 bazel_dep(name = "bazel_ci_rules", version = "1.0.0")
 
-bazel_test_deps = use_extension("//:extensions.bzl", "bazel_test_deps")
-use_repo(
-    bazel_test_deps,
-    "kythe_release",
-    "local_bazel_source_list",
-    "local_config_winsdk",
+rbe_preconfig = use_repo_rule("@bazel_ci_rules//:rbe_repo.bzl", "rbe_preconfig")
+
+rbe_preconfig(
+    name = "rbe_ubuntu2004",
+    toolchain = "ubuntu2004",
 )
 
-bazel_rbe_deps = use_extension("//:rbe_extension.bzl", "bazel_rbe_deps")
-use_repo(bazel_rbe_deps, "rbe_ubuntu2004")
+list_source_repository = use_repo_rule("//src/test/shell/bazel:list_source_repository.bzl", "list_source_repository")
+
+list_source_repository(name = "local_bazel_source_list")
+
+winsdk_configure = use_repo_rule("//src/main/res:winsdk_configure.bzl", "winsdk_configure")
+
+winsdk_configure(name = "local_config_winsdk")
+
+# /usr/local/kythe is setup on Bazel CI machines
+local_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository")
+
+local_repository(
+    name = "kythe_release",
+    path = "/usr/local/kythe",
+)
 
 remote_coverage_tools_extension = use_extension("//tools/test:extensions.bzl", "remote_coverage_tools_extension")
 use_repo(remote_coverage_tools_extension, "remote_coverage_tools")
diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock
index 37670ac..aef4a96 100644
--- a/MODULE.bazel.lock
+++ b/MODULE.bazel.lock
@@ -27,7 +27,8 @@
     "https://bcr.bazel.build/modules/bazel_features/1.17.0/MODULE.bazel": "039de32d21b816b47bd42c778e0454217e9c9caac4a3cf8e15c7231ee3ddee4d",
     "https://bcr.bazel.build/modules/bazel_features/1.18.0/MODULE.bazel": "1be0ae2557ab3a72a57aeb31b29be347bcdc5d2b1eb1e70f39e3851a7e97041a",
     "https://bcr.bazel.build/modules/bazel_features/1.19.0/MODULE.bazel": "59adcdf28230d220f0067b1f435b8537dd033bfff8db21335ef9217919c7fb58",
-    "https://bcr.bazel.build/modules/bazel_features/1.19.0/source.json": "d7bf14517c1b25b9d9c580b0f8795fceeae08a7590f507b76aace528e941375d",
+    "https://bcr.bazel.build/modules/bazel_features/1.21.0/MODULE.bazel": "675642261665d8eea09989aa3b8afb5c37627f1be178382c320d1b46afba5e3b",
+    "https://bcr.bazel.build/modules/bazel_features/1.21.0/source.json": "3e8379efaaef53ce35b7b8ba419df829315a880cb0a030e5bb45c96d6d5ecb5f",
     "https://bcr.bazel.build/modules/bazel_features/1.3.0/MODULE.bazel": "cdcafe83ec318cda34e02948e81d790aab8df7a929cec6f6969f13a489ccecd9",
     "https://bcr.bazel.build/modules/bazel_features/1.4.1/MODULE.bazel": "e45b6bb2350aff3e442ae1111c555e27eac1d915e77775f6fdc4b351b758b5d7",
     "https://bcr.bazel.build/modules/bazel_features/1.9.1/MODULE.bazel": "8f679097876a9b609ad1f60249c49d68bfab783dd9be012faf9d82547b14815a",
@@ -710,7 +711,7 @@
     },
     "@@rules_apple+//apple:apple.bzl%provisioning_profile_repository_extension": {
       "general": {
-        "bzlTransitiveDigest": "csKlPWUWnllWTbqKd25aCDlEM+HC1Vwkco0G+nQkx2A=",
+        "bzlTransitiveDigest": "q9xjvYjuEInch+xBf39T5fOMbcAKM8yOF3oBTeL7Ojw=",
         "usagesDigest": "lkQku2fyVfziUrJ74C6SyS/ntzJOwa6ZF8hyCIWhsDw=",
         "recordedFileInputs": {},
         "recordedDirentsInputs": {},
@@ -5306,7 +5307,7 @@
     },
     "@@rules_python+//python/private/pypi:pip.bzl%pip_internal": {
       "general": {
-        "bzlTransitiveDigest": "Ia+WlCPeYdZM39vUMNpEI4N0sQyzLK3j231Xqd6nSHs=",
+        "bzlTransitiveDigest": "Tkx3CGKY3kzE5ObWsQX2OIv9IOF7ycNkOH50c1Wnxso=",
         "usagesDigest": "OLoIStnzNObNalKEMRq99FqenhPGLFZ5utVLV4sz7OI=",
         "recordedFileInputs": {
           "@@rules_python+//tools/publish/requirements_windows.txt": "7673adc71dc1a81d3661b90924d7a7c0fc998cd508b3cb4174337cef3f2de556",
diff --git a/extensions.bzl b/extensions.bzl
index d84047d..9d7797e 100644
--- a/extensions.bzl
+++ b/extensions.bzl
@@ -16,12 +16,9 @@
 
 """
 
-load("@bazel_tools//tools/build_defs/repo:local.bzl", "local_repository")
 load("//:distdir.bzl", "distdir_tar", "repo_cache_tar")
 load("//:repositories.bzl", "DIST_ARCHIVE_REPOS", "embedded_jdk_repositories")
 load("//:workspace_deps.bzl", "WORKSPACE_REPOS")
-load("//src/main/res:winsdk_configure.bzl", "winsdk_configure")
-load("//src/test/shell/bazel:list_source_repository.bzl", "list_source_repository")
 load("//src/tools/bzlmod:utils.bzl", "parse_bazel_module_repos")
 load("//tools/distributions/debian:deps.bzl", "debian_deps")
 
@@ -46,17 +43,3 @@
     return ctx.extension_metadata(reproducible = True)
 
 bazel_build_deps = module_extension(implementation = _bazel_build_deps)
-
-### Dependencies for testing Bazel
-def _bazel_test_deps(ctx):
-    list_source_repository(name = "local_bazel_source_list")
-    winsdk_configure(name = "local_config_winsdk")
-
-    # /usr/local/kythe is setup on Bazel CI machines
-    local_repository(
-        name = "kythe_release",
-        path = "/usr/local/kythe",
-    )
-    return ctx.extension_metadata(reproducible = True)
-
-bazel_test_deps = module_extension(implementation = _bazel_test_deps)
diff --git a/rbe_extension.bzl b/rbe_extension.bzl
deleted file mode 100644
index 7f24eb0..0000000
--- a/rbe_extension.bzl
+++ /dev/null
@@ -1,28 +0,0 @@
-# Copyright 2023 The Bazel Authors. All rights reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-#    http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-
-"""Module extensions for loading RBE toolchains.
-
-"""
-
-load("@bazel_ci_rules//:rbe_repo.bzl", "rbe_preconfig")
-
-def _bazel_rbe_deps(ctx):
-    rbe_preconfig(
-        name = "rbe_ubuntu2004",
-        toolchain = "ubuntu2004",
-    )
-    return ctx.extension_metadata(reproducible = True)
-
-bazel_rbe_deps = module_extension(implementation = _bazel_rbe_deps)
diff --git a/src/BUILD b/src/BUILD
index c3b96f0..329f22c 100644
--- a/src/BUILD
+++ b/src/BUILD
@@ -413,6 +413,29 @@
     visibility = ["//:__pkg__"],
 )
 
+# Source files that can be excluded from //:bazel-distfile to avoid triggering the bootstrap test.
+filegroup(
+    name = "srcs-to-exclude-in-distfile",
+    srcs = [
+        "//src/test/cpp:srcs",
+        "//src/test/gen:srcs",
+        "//src/test/java/com/google/devtools/build/docgen:srcs",
+        "//src/test/java/com/google/devtools/build/lib:srcs",
+        "//src/test/java/com/google/devtools/build/lib/shell:srcs",
+        "//src/test/java/com/google/devtools/build/skyframe:srcs",
+        "//src/test/java/com/google/devtools/common/options:srcs",
+        "//src/test/java/net/starlark/java/eval:srcs",
+        "//src/test/java/net/starlark/java/spelling:srcs",
+        "//src/test/native/windows:srcs",
+        "//src/test/py/bazel:srcs",
+        "//src/test/res:srcs",
+        "//src/test/shell:srcs",
+        "//src/test/testdata/test_tls_certificate",
+        "//src/test/tools:srcs",
+    ],
+    visibility = ["//:__pkg__"],
+)
+
 genrule(
     name = "derived_java_srcs",
     srcs = [
diff --git a/src/MODULE.tools b/src/MODULE.tools
index 915a2a9..d61647f 100644
--- a/src/MODULE.tools
+++ b/src/MODULE.tools
@@ -11,6 +11,9 @@
 bazel_dep(name = "zlib", version = "1.3.1.bcr.3")
 bazel_dep(name = "rules_proto", version = "7.0.2")
 
+# Enforce a minimal required version
+bazel_dep(name = "bazel_features", version = "1.21.0")
+
 xcode_configure = use_extension("//tools/osx:xcode_configure.bzl", "xcode_configure_extension")
 use_repo(xcode_configure, "local_config_xcode")
 
diff --git a/src/test/tools/bzlmod/MODULE.bazel.lock b/src/test/tools/bzlmod/MODULE.bazel.lock
index 434815e..cf4d1b6 100644
--- a/src/test/tools/bzlmod/MODULE.bazel.lock
+++ b/src/test/tools/bzlmod/MODULE.bazel.lock
@@ -16,7 +16,8 @@
     "https://bcr.bazel.build/modules/bazel_features/1.17.0/MODULE.bazel": "039de32d21b816b47bd42c778e0454217e9c9caac4a3cf8e15c7231ee3ddee4d",
     "https://bcr.bazel.build/modules/bazel_features/1.18.0/MODULE.bazel": "1be0ae2557ab3a72a57aeb31b29be347bcdc5d2b1eb1e70f39e3851a7e97041a",
     "https://bcr.bazel.build/modules/bazel_features/1.19.0/MODULE.bazel": "59adcdf28230d220f0067b1f435b8537dd033bfff8db21335ef9217919c7fb58",
-    "https://bcr.bazel.build/modules/bazel_features/1.19.0/source.json": "d7bf14517c1b25b9d9c580b0f8795fceeae08a7590f507b76aace528e941375d",
+    "https://bcr.bazel.build/modules/bazel_features/1.21.0/MODULE.bazel": "675642261665d8eea09989aa3b8afb5c37627f1be178382c320d1b46afba5e3b",
+    "https://bcr.bazel.build/modules/bazel_features/1.21.0/source.json": "3e8379efaaef53ce35b7b8ba419df829315a880cb0a030e5bb45c96d6d5ecb5f",
     "https://bcr.bazel.build/modules/bazel_features/1.4.1/MODULE.bazel": "e45b6bb2350aff3e442ae1111c555e27eac1d915e77775f6fdc4b351b758b5d7",
     "https://bcr.bazel.build/modules/bazel_features/1.9.1/MODULE.bazel": "8f679097876a9b609ad1f60249c49d68bfab783dd9be012faf9d82547b14815a",
     "https://bcr.bazel.build/modules/bazel_skylib/1.0.3/MODULE.bazel": "bcb0fd896384802d1ad283b4e4eb4d718eebd8cb820b0a2c3a347fb971afd9d8",
@@ -355,7 +356,7 @@
     },
     "@@rules_python+//python/private/pypi:pip.bzl%pip_internal": {
       "general": {
-        "bzlTransitiveDigest": "nMxNfech6e0MDgkciI/sKryYD99ePLF8e4C6ySTOksg=",
+        "bzlTransitiveDigest": "6j7OeER97/sUx3f8grT22jx9XEhLfoBOWPtfkHNYcm4=",
         "usagesDigest": "OLoIStnzNObNalKEMRq99FqenhPGLFZ5utVLV4sz7OI=",
         "recordedFileInputs": {
           "@@rules_python+//tools/publish/requirements_darwin.txt": "2994136eab7e57b083c3de76faf46f70fad130bc8e7360a7fed2b288b69e79dc",