Copy Proto rules from @protobuf instead of mocking

Setup copying. This will make it possible to use rules implementation from @protobuf repository in the tests.
Remove mocking of proto_lang_toolchain. It's not needed anymore
Rename protobuf_workspace to third_party/protobuf. This make loads nicer.
Keep mocking the rules. Tests are still using built-in rules and mixing them with @protobuf rules doesn't work well.
Remove some other unnecessary mocks.

Fix bazel_skylib to be imported in one go, instead of from the lib and rules directory.

PiperOrigin-RevId: 686052269
Change-Id: I9e7bcbcd1ce42fd27e88e4e446e74e9aec1605f8
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD
index 11e1770..5bc13b5 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BUILD
@@ -20,6 +20,8 @@
     ]),
     data = [
         "//tools/jdk:srcs",
+        "@bazel_skylib//:test_deps",
+        "@protobuf//bazel/private:for_bazel_tests",
         "@rules_cc//cc:srcs",
         "@rules_cc//cc/common:srcs",
         "@rules_cc//cc/toolchains:srcs",
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
index e1efd57..4578c7b 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/mock/BazelAnalysisMock.java
@@ -63,7 +63,8 @@
   @Override
   public ImmutableList<String> getWorkspaceContents(MockToolsConfig config) {
     String xcodeWorkspace = config.getPath("local_config_xcode_workspace").getPathString();
-    String protobufWorkspace = config.getPath("protobuf_workspace").getPathString();
+    String protobufWorkspace = config.getPath("third_party/protobuf").getPathString();
+    String protoBazelFeaturesWorkspace = config.getPath("proto_bazel_features").getPathString();
     String bazelToolWorkspace = config.getPath("embedded_tools").getPathString();
     String bazelPlatformsWorkspace = config.getPath("platforms_workspace").getPathString();
     String rulesJavaWorkspace = config.getPath("rules_java_workspace").getPathString();
@@ -91,6 +92,9 @@
             + "')",
         "local_repository(name = 'local_config_xcode', path = '" + xcodeWorkspace + "')",
         "local_repository(name = 'protobuf', path = '" + protobufWorkspace + "')",
+        "local_repository(name = 'proto_bazel_features', path = '"
+            + protoBazelFeaturesWorkspace
+            + "')",
         "local_repository(name = 'rules_java', path = '" + rulesJavaWorkspace + "')",
         "local_repository(name = 'rules_java_builtin', path = '" + rulesJavaWorkspace + "')",
         "local_repository(name = 'android_gmaven_r8', path = '" + androidGmavenR8Workspace + "')",
@@ -111,6 +115,7 @@
     return ImmutableList.of(
         "android_gmaven_r8",
         "protobuf",
+        "proto_bazel_features",
         "local_config_platform",
         "local_config_xcode",
         "internal_platforms_do_not_use",
@@ -132,18 +137,9 @@
     config.create("local_config_xcode_workspace/BUILD", "xcode_config(name = 'host_xcodes')");
     config.create(
         "local_config_xcode_workspace/MODULE.bazel", "module(name = 'local_config_xcode')");
-    config.create(
-        "protobuf_workspace/BUILD",
-        """
-        licenses(["notice"])
-
-        exports_files([
-            "protoc",
-            "cc_toolchain",
-        ])
-        """);
-    config.create("protobuf_workspace/WORKSPACE");
-    config.create("protobuf_workspace/MODULE.bazel", "module(name='protobuf')");
+    config.create("third_party/protobuf/WORKSPACE");
+    config.create("third_party/protobuf/BUILD");
+    config.create("third_party/protobuf/MODULE.bazel", "module(name='protobuf')");
     config.overwrite("WORKSPACE", workspaceContents.toArray(new String[0]));
     config.overwrite(
         "MODULE.bazel",
@@ -172,7 +168,8 @@
         "local_config_platform_workspace",
         "rules_java_workspace",
         "rules_python_workspace",
-        "protobuf_workspace",
+        "third_party/protobuf",
+        "proto_bazel_features_workspace",
         "build_bazel_apple_support",
         "local_config_xcode_workspace",
         "third_party/bazel_rules/rules_cc");
@@ -660,12 +657,9 @@
     config.create("embedded_tools/objcproto/well_known_type.proto");
 
     // Copies bazel_skylib from real @bazel_skylib (needed by rules_python)
-    PathFragment path = PathFragment.create(runfiles.rlocation("bazel_skylib/lib/paths.bzl"));
-    config.copyDirectory(
-        path.getParentDirectory().getParentDirectory(), "bazel_skylib_workspace", MAX_VALUE, true);
+    PathFragment path = PathFragment.create(runfiles.rlocation("bazel_skylib/BUILD"));
+    config.copyDirectory(path.getParentDirectory(), "bazel_skylib_workspace", MAX_VALUE, true);
     config.overwrite("bazel_skylib_workspace/MODULE.bazel", "module(name = 'bazel_skylib')");
-    config.overwrite("bazel_skylib_workspace/lib/BUILD");
-    config.overwrite("bazel_skylib_workspace/rules/BUILD");
 
     config.create(
         "embedded_tools/tools/allowlists/function_transition_allowlist/BUILD",
@@ -834,7 +828,8 @@
             .put("rules_python", "rules_python_workspace")
             .put("rules_python_internal", "rules_python_internal_workspace")
             .put("bazel_skylib", "bazel_skylib_workspace")
-            .put("protobuf", "protobuf_workspace")
+            .put("protobuf", "third_party/protobuf")
+            .put("proto_bazel_features", "proto_bazel_features_workspace")
             .put("build_bazel_apple_support", "build_bazel_apple_support")
             .put("local_config_xcode", "local_config_xcode_workspace")
             .put("rules_cc", "third_party/bazel_rules/rules_cc")
diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java b/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java
index 0114de1..7dc7f7d 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/util/MockProtoSupport.java
@@ -15,9 +15,12 @@
 package com.google.devtools.build.lib.packages.util;
 
 import static com.google.devtools.build.lib.rules.python.PythonTestUtils.getPyLoad;
+import static java.lang.Integer.MAX_VALUE;
 
 import com.google.devtools.build.lib.rules.proto.ProtoConstants;
 import com.google.devtools.build.lib.testutil.TestConstants;
+import com.google.devtools.build.lib.vfs.PathFragment;
+import com.google.devtools.build.runfiles.Runfiles;
 import java.io.IOException;
 
 /**
@@ -374,122 +377,72 @@
             visibility = ["//visibility:public"],
         )
         """);
-    config.create(
-        "protobuf_workspace/bazel/private/BUILD",
-        """
-        licenses(["notice"])
+    if (TestConstants.PRODUCT_NAME.equals("bazel")) {
+      Runfiles runfiles = Runfiles.preload().withSourceRepository("");
+      PathFragment path = PathFragment.create(runfiles.rlocation("protobuf/bazel/BUILD.bazel"));
+      config.copyDirectory(
+          path.getParentDirectory(), "third_party/protobuf/bazel", MAX_VALUE, false);
+      config.overwrite(
+          "proto_bazel_features_workspace/MODULE.bazel", "module(name = 'proto_bazel_features')");
+      // Overwritten to remove bazel7 toolchains from protobuf
+      config.overwrite(
+          "third_party/protobuf/bazel/private/toolchains/BUILD.bazel",
+          "load('@protobuf//bazel/toolchains:proto_toolchain.bzl', 'proto_toolchain')",
+          TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
+          "proto_toolchain(name = 'protoc_sources',"
+              + "proto_compiler = '"
+              + ProtoConstants.DEFAULT_PROTOC_LABEL
+              + "')");
+      config.overwrite("proto_bazel_features_workspace/BUILD");
+      config.overwrite("proto_bazel_features_workspace/WORKSPACE");
+      config.overwrite(
+          "proto_bazel_features_workspace/features.bzl",
+          "bazel_features = struct(",
+          "  globals = struct(PackageSpecificationInfo = PackageSpecificationInfo),",
+          "  proto = struct(starlark_proto_info = True),",
+          "  cc = struct(protobuf_on_allowlist = True),",
+          ")");
 
-        toolchain_type(
-            name = "proto_toolchain_type",
-            visibility = ["//visibility:public"],
-        )
-        """);
-    config.create("protobuf_workspace/bazel/BUILD");
-    config.create(
-        "protobuf_workspace/bazel/proto_library.bzl",
-        """
-        def proto_library(**kargs):
-            native.proto_library(**kargs)
-        """);
-    config.create(
-        "protobuf_workspace/bazel/java_proto_library.bzl",
-        """
-        def java_proto_library(**kargs):
-            native.java_proto_library(**kargs)
-        """);
-    config.create(
-        "protobuf_workspace/bazel/java_lite_proto_library.bzl",
-        """
-        def java_lite_proto_library(**kargs):
-            native.java_lite_proto_library(**kargs)
-        """);
-    config.create(
-        "protobuf_workspace/bazel/toolchains/proto_toolchain.bzl",
-        "load(':proto_toolchain_rule.bzl', _proto_toolchain_rule = 'proto_toolchain')",
-        "def proto_toolchain(*, name, proto_compiler, exec_compatible_with = []):",
-        "  _proto_toolchain_rule(name = name, proto_compiler = proto_compiler)",
-        "  native.toolchain(",
-        "    name = name + '_toolchain',",
-        "    toolchain_type = '" + TestConstants.PROTO_TOOLCHAIN + "',",
-        "    exec_compatible_with = exec_compatible_with,",
-        "    target_compatible_with = [],",
-        "    toolchain = name,",
-        "  )");
-    config.create("protobuf_workspace/bazel/toolchains/BUILD");
-    config.create(
-        "protobuf_workspace/bazel/toolchains/proto_toolchain_rule.bzl",
-        """
-ProtoLangToolchainInfo = proto_common_do_not_use.ProtoLangToolchainInfo
-
-def _impl(ctx):
-    return [
-        DefaultInfo(
-            files = depset(),
-            runfiles = ctx.runfiles(),
-        ),
-        platform_common.ToolchainInfo(
-            proto = ProtoLangToolchainInfo(
-                out_replacement_format_flag = ctx.attr.command_line,
-                output_files = ctx.attr.output_files,
-                plugin = None,
-                runtime = None,
-                proto_compiler = ctx.attr.proto_compiler.files_to_run,
-                protoc_opts = ctx.fragments.proto.experimental_protoc_opts,
-                progress_message = ctx.attr.progress_message,
-                mnemonic = ctx.attr.mnemonic,
-                allowlist_different_package = None,
-                toolchain_type =
-                    "//protobuf/bazel/private:proto_toolchain_type",
-            ),
-        ),
-    ]
-
-proto_toolchain = rule(
-    _impl,
-    attrs = {
-        "progress_message": attr.string(default =
-                                            "Generating Descriptor Set proto_library %{label}"),
-        "mnemonic": attr.string(default = "GenProtoDescriptorSet"),
-        "command_line": attr.string(default = "--descriptor_set_out=%s"),
-        "output_files": attr.string(
-            values = ["single", "multiple", "legacy"],
-            default = "single",
-        ),
-        "proto_compiler": attr.label(
-            cfg = "exec",
-            executable = True,
-            allow_files = True,
-        ),
-    },
-    provides = [platform_common.ToolchainInfo],
-    fragments = ["proto"],
-)
-""");
-    config.create(
-        "protobuf_workspace/bazel/toolchains/proto_lang_toolchain.bzl",
-        """
-        def proto_lang_toolchain(
-                *,
-                name,
-                toolchain_type = None,
-                exec_compatible_with = [],
-                target_compatible_with = [],
-                **attrs):
-            native.proto_lang_toolchain(name = name, toolchain_type = toolchain_type, **attrs)
-            if toolchain_type:
-                native.toolchain(
-                    name = name + "_toolchain",
-                    toolchain_type = toolchain_type,
-                    exec_compatible_with = exec_compatible_with,
-                    target_compatible_with = target_compatible_with,
-                    toolchain = name,
-                )
-        """);
-    config.create("protobuf_workspace/bazel/common/BUILD");
-    config.create(
-        "protobuf_workspace/bazel/common/proto_info.bzl",
-        """
-        ProtoInfo = provider()
-        """);
+      // TODO - ilist@: remove the following block when removing the rules
+      // This serves at the moment to keep using builitin rules in the tests
+      config.overwrite(
+          "third_party/protobuf/BUILD.bazel",
+          "filegroup(name='license')",
+          "exports_files(['protoc'])");
+      config.overwrite(
+          "third_party/protobuf/bazel/proto_library.bzl", //
+          "proto_library = native.proto_library");
+      config.overwrite(
+          "third_party/protobuf/bazel/java_proto_library.bzl",
+          "java_proto_library = native.java_proto_library");
+      config.overwrite(
+          "third_party/protobuf/bazel/java_lite_proto_library.bzl",
+          "java_lite_proto_library = native.java_lite_proto_library");
+      config.overwrite(
+          "third_party/protobuf/bazel/cc_proto_library.bzl",
+          "cc_proto_library = native.cc_proto_library");
+      config.create(
+          "third_party/protobuf/bazel/private/native_proto_info.bzl",
+          "NativeProtoInfo = ProtoInfo");
+      config.overwrite(
+          "third_party/protobuf/bazel/common/proto_info.bzl",
+          "load('@protobuf//bazel/private:native_proto_info.bzl', 'NativeProtoInfo')",
+          "ProtoInfo = NativeProtoInfo");
+      config.overwrite(
+          "third_party/protobuf/bazel/toolchains/proto_lang_toolchain.bzl",
+          """
+          def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [],
+                  target_compatible_with = [], **attrs):
+              native.proto_lang_toolchain(name = name, toolchain_type = toolchain_type, **attrs)
+              if toolchain_type:
+                  native.toolchain(
+                      name = name + "_toolchain",
+                      toolchain_type = toolchain_type,
+                      exec_compatible_with = exec_compatible_with,
+                      target_compatible_with = target_compatible_with,
+                      toolchain = name,
+                  )
+          """);
+    }
   }
 }
diff --git a/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java b/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java
index e0dc9d1..95f1854 100644
--- a/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/query2/testutil/AbstractQueryTest.java
@@ -1744,9 +1744,14 @@
     helper.writeFile("/workspace/bazel_skylib_workspace/WORKSPACE");
     helper.writeFile(
         "/workspace/bazel_skylib_workspace/MODULE.bazel", "module(name='bazel_skylib')");
-    helper.writeFile("/workspace/protobuf_workspace/BUILD");
-    helper.writeFile("/workspace/protobuf_workspace/WORKSPACE");
-    helper.writeFile("/workspace/protobuf_workspace/MODULE.bazel", "module(name='protobuf')");
+    helper.writeFile("/workspace/third_party/protobuf/BUILD");
+    helper.writeFile("/workspace/third_party/protobuf/WORKSPACE");
+    helper.writeFile("/workspace/third_party/protobuf/MODULE.bazel", "module(name='protobuf')");
+    helper.writeFile("/workspace/proto_bazel_features_workspace/BUILD");
+    helper.writeFile("/workspace/proto_bazel_features_workspace/WORKSPACE");
+    helper.writeFile(
+        "/workspace/proto_bazel_features_workspace/MODULE.bazel",
+        "module(name='proto_bazel_features')");
     helper.writeFile("/workspace/local_config_platform_workspace/BUILD");
     helper.writeFile("/workspace/local_config_platform_workspace/WORKSPACE");
     helper.writeFile(
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java
index 285fd12..1c1736d 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibraryTest.java
@@ -61,13 +61,11 @@
     scratch.overwriteFile(
         "third_party/bazel_rules/rules_cc/cc/proto/BUILD",
         "toolchain_type(name = 'toolchain_type', visibility = ['//visibility:public'])");
-    scratch.file("protobuf/WORKSPACE");
-    scratch.overwriteFile(
-        "protobuf_workspace/BUILD",
+    scratch.appendFile(
+        "third_party/protobuf/BUILD.bazel",
         TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
         TestConstants.LOAD_PROTO_LIBRARY,
         "package(default_visibility=['//visibility:public'])",
-        "exports_files(['protoc'])",
         "proto_library(",
         "    name = 'any_proto',",
         "    srcs = ['any.proto'],",
@@ -79,13 +77,7 @@
         "    progress_message = 'Generating C++ proto_library %{label}',",
         "    toolchain_type = '@rules_cc//cc/proto:toolchain_type',",
         ")");
-    scratch.appendFile(
-        "WORKSPACE",
-        "register_toolchains('@protobuf//:all')",
-        "local_repository(",
-        "    name = 'protobuf',",
-        "    path = 'protobuf_workspace',",
-        ")");
+    scratch.appendFile("third_party/protobuf/MODULE.bazel", "register_toolchains('//:all')");
     invalidatePackages(); // A dash of magic to re-evaluate the WORKSPACE file.
   }