Automated rollback of commit 2398d8502407e905edadde91c6e69c2411ca0884.

*** Reason for rollback ***

Breaks skylib :(
https://buildkite.com/bazel/bazel-at-head-plus-downstream/builds/861#8e4e9bc5-ea99-4036-9fc9-867a90e708a3

*** Original change description ***

Pass repoMapping through toolchain functions

Closes #7718.
Fixes #7654.

PiperOrigin-RevId: 238606863
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java b/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java
index 9c32f95..401a34a 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/ToolchainContext.java
@@ -17,14 +17,12 @@
 import static java.util.stream.Collectors.joining;
 
 import com.google.auto.value.AutoValue;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.devtools.build.lib.analysis.platform.PlatformInfo;
 import com.google.devtools.build.lib.analysis.platform.ToolchainInfo;
 import com.google.devtools.build.lib.analysis.platform.ToolchainTypeInfo;
-import com.google.devtools.build.lib.analysis.skylark.BazelStarlarkContext;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
 import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
@@ -135,8 +133,7 @@
     printer.append(">");
   }
 
-  private Label transformKey(Object key, Location loc, BazelStarlarkContext context)
-      throws EvalException {
+  private Label transformKey(Object key, Location loc) throws EvalException {
     if (key instanceof Label) {
       return (Label) key;
     } else if (key instanceof ToolchainTypeInfo) {
@@ -145,7 +142,7 @@
       Label toolchainType;
       String rawLabel = (String) key;
       try {
-        toolchainType = Label.parseAbsolute(rawLabel, context.getRepoMapping());
+        toolchainType = Label.parseAbsolute(rawLabel, ImmutableMap.of());
       } catch (LabelSyntaxException e) {
         throw new EvalException(
             loc, String.format("Unable to parse toolchain %s: %s", rawLabel, e.getMessage()), e);
@@ -163,8 +160,7 @@
   @Override
   public ToolchainInfo getIndex(Object key, Location loc, StarlarkContext context)
       throws EvalException {
-    Preconditions.checkArgument(context instanceof BazelStarlarkContext);
-    Label toolchainTypeLabel = transformKey(key, loc, (BazelStarlarkContext) context);
+    Label toolchainTypeLabel = transformKey(key, loc);
 
     if (!containsKey(key, loc, context)) {
       throw new EvalException(
@@ -184,8 +180,7 @@
   @Override
   public boolean containsKey(Object key, Location loc, StarlarkContext context)
       throws EvalException {
-    Preconditions.checkArgument(context instanceof BazelStarlarkContext);
-    Label toolchainTypeLabel = transformKey(key, loc, (BazelStarlarkContext) context);
+    Label toolchainTypeLabel = transformKey(key, loc);
     Optional<Label> matching =
         toolchains().keySet().stream()
             .map(ToolchainTypeInfo::typeLabel)
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java
index c1a1f4b..0c358e2 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkRuleClassFunctions.java
@@ -43,7 +43,6 @@
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
 import com.google.devtools.build.lib.cmdline.LabelValidator;
-import com.google.devtools.build.lib.cmdline.RepositoryName;
 import com.google.devtools.build.lib.events.Location;
 import com.google.devtools.build.lib.packages.Attribute;
 import com.google.devtools.build.lib.packages.AttributeMap;
@@ -290,7 +289,6 @@
       throws EvalException, ConversionException {
     SkylarkUtils.checkLoadingOrWorkspacePhase(funcallEnv, "rule", ast.getLocation());
 
-    Preconditions.checkArgument(context instanceof BazelStarlarkContext);
     BazelStarlarkContext bazelContext = (BazelStarlarkContext) context;
     // analysis_test=true implies test=true.
     test |= Boolean.TRUE.equals(analysisTest);
@@ -357,9 +355,7 @@
         funcallEnv.getGlobals().getLabel(), funcallEnv.getTransitiveContentHashCode());
     builder.addRequiredToolchains(
         collectToolchainLabels(
-            toolchains.getContents(String.class, "toolchains"),
-            bazelContext.getRepoMapping(),
-            ast.getLocation()));
+            toolchains.getContents(String.class, "toolchains"), ast.getLocation()));
 
     if (!buildSetting.equals(Runtime.NONE) && !cfg.equals(Runtime.NONE)) {
       throw new EvalException(
@@ -435,14 +431,11 @@
   }
 
   private static ImmutableList<Label> collectToolchainLabels(
-      Iterable<String> rawLabels,
-      ImmutableMap<RepositoryName, RepositoryName> repoMapping,
-      Location loc)
-      throws EvalException {
+      Iterable<String> rawLabels, Location loc) throws EvalException {
     ImmutableList.Builder<Label> requiredToolchains = new ImmutableList.Builder<>();
     for (String rawLabel : rawLabels) {
       try {
-        Label toolchainLabel = Label.parseAbsolute(rawLabel, repoMapping);
+        Label toolchainLabel = Label.parseAbsolute(rawLabel, ImmutableMap.of());
         requiredToolchains.add(toolchainLabel);
       } catch (LabelSyntaxException e) {
         throw new EvalException(
@@ -481,10 +474,8 @@
       SkylarkList<?> toolchains,
       String doc,
       FuncallExpression ast,
-      Environment funcallEnv,
-      StarlarkContext context)
+      Environment funcallEnv)
       throws EvalException {
-    Preconditions.checkArgument(context instanceof BazelStarlarkContext);
     Location location = ast.getLocation();
     ImmutableList.Builder<String> attrAspects = ImmutableList.builder();
     for (Object attributeAspect : attributeAspects) {
@@ -572,9 +563,7 @@
         HostTransition.INSTANCE,
         ImmutableSet.copyOf(hostFragments.getContents(String.class, "host_fragments")),
         collectToolchainLabels(
-            toolchains.getContents(String.class, "toolchains"),
-            ((BazelStarlarkContext) context).getRepoMapping(),
-            ast.getLocation()));
+            toolchains.getContents(String.class, "toolchains"), ast.getLocation()));
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/SkylarkRuleFunctionsApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/SkylarkRuleFunctionsApi.java
index 4a20d69..c2b82d1 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/SkylarkRuleFunctionsApi.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/SkylarkRuleFunctionsApi.java
@@ -374,104 +374,110 @@
               + "Please see the <a href=\"../aspects.md\">introduction to Aspects</a> for more "
               + "details.",
       parameters = {
-        @Param(
-            name = "implementation",
-            type = BaseFunction.class,
-            legacyNamed = true,
-            doc =
-                "the function implementing this aspect. Must have two parameters: "
-                    + "<a href=\"Target.html\">Target</a> (the target to which the aspect is "
-                    + "applied) and <a href=\"ctx.html\">ctx</a>. Attributes of the target are "
-                    + "available via ctx.rule field. The function is called during the analysis "
-                    + "phase for each application of an aspect to a target."),
-        @Param(
-            name = "attr_aspects",
-            type = SkylarkList.class,
-            legacyNamed = true,
-            generic1 = String.class,
-            defaultValue = "[]",
-            doc =
-                "List of attribute names.  The aspect propagates along dependencies specified by"
-                    + " attributes of a target with this name. The list can also contain a single "
-                    + "string '*': in that case aspect propagates along all dependencies of a"
-                    + " target."),
-        @Param(
-            name = "attrs",
-            type = SkylarkDict.class,
-            legacyNamed = true,
-            noneable = true,
-            defaultValue = "None",
-            doc =
-                "dictionary to declare all the attributes of the aspect.  It maps from an"
-                    + " attribute name to an attribute object (see <a href=\"attr.html\">attr</a>"
-                    + " module). Aspect attributes are available to implementation function as"
-                    + " fields of ctx parameter. Implicit attributes starting with <code>_</code>"
-                    + " must have default values, and have type <code>label</code> or"
-                    + " <code>label_list</code>. Explicit attributes must have type"
-                    + " <code>string</code>, and must use the <code>values</code> restriction. If"
-                    + " explicit attributes are present, the aspect can only be used with rules"
-                    + " that have attributes of the same name and type, with valid values."),
-        @Param(
-            name = "required_aspect_providers",
-            type = SkylarkList.class,
-            legacyNamed = true,
-            defaultValue = "[]",
-            doc =
-                "Allow the aspect to inspect other aspects. If the aspect propagates along a"
-                    + " dependency, and the underlying rule sends a different aspect along that "
-                    + "dependency, and that aspect provides one of the providers listed here, this"
-                    + " aspect will see the providers provided by that aspect. <p>The value should"
-                    + " be either a list of providers, or a list of lists of providers. This"
-                    + " aspect will 'see'  the underlying aspects that provide  ALL providers from"
-                    + " at least ONE of these lists. A single list of providers will be"
-                    + " automatically converted to a list containing one list of providers."),
-        @Param(
-            name = "provides",
-            type = SkylarkList.class,
-            legacyNamed = true,
-            defaultValue = "[]",
-            doc = PROVIDES_DOC),
-        @Param(
-            name = "fragments",
-            type = SkylarkList.class,
-            legacyNamed = true,
-            generic1 = String.class,
-            defaultValue = "[]",
-            doc =
-                "List of names of configuration fragments that the aspect requires "
-                    + "in target configuration."),
-        @Param(
-            name = "host_fragments",
-            type = SkylarkList.class,
-            legacyNamed = true,
-            generic1 = String.class,
-            defaultValue = "[]",
-            doc =
-                "List of names of configuration fragments that the aspect requires "
-                    + "in host configuration."),
-        @Param(
-            name = "toolchains",
-            type = SkylarkList.class,
-            legacyNamed = true,
-            generic1 = String.class,
-            defaultValue = "[]",
-            doc =
-                "<i>(Experimental)</i><br/><br/>"
-                    + "If set, the set of toolchains this rule requires. Toolchains will be "
-                    + "found by checking the current platform, and provided to the rule "
-                    + "implementation via <code>ctx.toolchain</code>."),
-        @Param(
-            name = "doc",
-            type = String.class,
-            legacyNamed = true,
-            defaultValue = "''",
-            doc =
-                "A description of the aspect that can be extracted by documentation generating "
-                    + "tools.")
+          @Param(
+              name = "implementation",
+              type = BaseFunction.class,
+              legacyNamed = true,
+              doc =
+                  "the function implementing this aspect. Must have two parameters: "
+                      + "<a href=\"Target.html\">Target</a> (the target to which the aspect is "
+                      + "applied) and <a href=\"ctx.html\">ctx</a>. Attributes of the target are "
+                      + "available via ctx.rule field. The function is called during the analysis "
+                      + "phase for each application of an aspect to a target."
+          ),
+          @Param(
+              name = "attr_aspects",
+              type = SkylarkList.class,
+              legacyNamed = true,
+              generic1 = String.class,
+              defaultValue = "[]",
+              doc = "List of attribute names.  The aspect propagates along dependencies specified "
+                  + "by attributes of a target with this name. The list can also contain a single "
+                  + "string '*': in that case aspect propagates along all dependencies of a target."
+          ),
+          @Param(
+              name = "attrs",
+              type = SkylarkDict.class,
+              legacyNamed = true,
+              noneable = true,
+              defaultValue = "None",
+              doc = "dictionary to declare all the attributes of the aspect.  "
+                  + "It maps from an attribute name to an attribute object "
+                  + "(see <a href=\"attr.html\">attr</a> module). "
+                  + "Aspect attributes are available to implementation function as fields of ctx "
+                  + "parameter. Implicit attributes starting with <code>_</code> must have default "
+                  + "values, and have type <code>label</code> or <code>label_list</code>. "
+                  + "Explicit attributes must have type <code>string</code>, and must use the "
+                  + "<code>values</code> restriction. If explicit attributes are present, the "
+                  + "aspect can only be used with rules that have attributes of the same name and "
+                  + "type, with valid values."
+          ),
+          @Param(
+              name = "required_aspect_providers",
+              type = SkylarkList.class,
+              legacyNamed = true,
+              defaultValue = "[]",
+              doc = "Allow the aspect to inspect other aspects. If the aspect propagates along "
+                  + "a dependency, and the underlying rule sends a different aspect along that "
+                  + "dependency, and that aspect provides one of the providers listed here, this "
+                  + "aspect will see the providers provided by that aspect. "
+                  + "<p>The value should be either a list of providers, or a "
+                  + "list of lists of providers. This aspect will 'see'  the underlying aspects "
+                  + "that provide  ALL providers from at least ONE of these lists. A single list "
+                  + "of providers will be automatically converted to a list containing one list of "
+                  + "providers."
+          ),
+          @Param(
+              name = "provides",
+              type = SkylarkList.class,
+              legacyNamed = true,
+              defaultValue = "[]",
+              doc = PROVIDES_DOC
+          ),
+          @Param(
+              name = "fragments",
+              type = SkylarkList.class,
+              legacyNamed = true,
+              generic1 = String.class,
+              defaultValue = "[]",
+              doc =
+                  "List of names of configuration fragments that the aspect requires "
+                      + "in target configuration."
+          ),
+          @Param(
+              name = "host_fragments",
+              type = SkylarkList.class,
+              legacyNamed = true,
+              generic1 = String.class,
+              defaultValue = "[]",
+              doc =
+                  "List of names of configuration fragments that the aspect requires "
+                      + "in host configuration."
+          ),
+          @Param(
+              name = "toolchains",
+              type = SkylarkList.class,
+              legacyNamed = true,
+              generic1 = String.class,
+              defaultValue = "[]",
+              doc =
+                  "<i>(Experimental)</i><br/><br/>"
+                      + "If set, the set of toolchains this rule requires. Toolchains will be "
+                      + "found by checking the current platform, and provided to the rule "
+                      + "implementation via <code>ctx.toolchain</code>."
+          ),
+          @Param(
+              name = "doc",
+              type = String.class,
+              legacyNamed = true,
+              defaultValue = "''",
+              doc = "A description of the aspect that can be extracted by documentation generating "
+                  + "tools."
+          )
       },
       useEnvironment = true,
-      useAst = true,
-      useContext = true)
+      useAst = true
+  )
   public SkylarkAspectApi aspect(
       BaseFunction implementation,
       SkylarkList<?> attributeAspects,
@@ -483,8 +489,7 @@
       SkylarkList<?> toolchains,
       String doc,
       FuncallExpression ast,
-      Environment funcallEnv,
-      StarlarkContext context)
+      Environment funcallEnv)
       throws EvalException;
 
   @SkylarkCallable(
diff --git a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeSkylarkRuleFunctionsApi.java b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeSkylarkRuleFunctionsApi.java
index 283535e..9690686 100644
--- a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeSkylarkRuleFunctionsApi.java
+++ b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeSkylarkRuleFunctionsApi.java
@@ -173,20 +173,10 @@
   }
 
   @Override
-  public SkylarkAspectApi aspect(
-      BaseFunction implementation,
-      SkylarkList<?> attributeAspects,
-      Object attrs,
-      SkylarkList<?> requiredAspectProvidersArg,
-      SkylarkList<?> providesArg,
-      SkylarkList<?> fragments,
-      SkylarkList<?> hostFragments,
-      SkylarkList<?> toolchains,
-      String doc,
-      FuncallExpression ast,
-      Environment funcallEnv,
-      StarlarkContext context)
-      throws EvalException {
+  public SkylarkAspectApi aspect(BaseFunction implementation, SkylarkList<?> attributeAspects,
+      Object attrs, SkylarkList<?> requiredAspectProvidersArg, SkylarkList<?> providesArg,
+      SkylarkList<?> fragments, SkylarkList<?> hostFragments, SkylarkList<?> toolchains, String doc,
+      FuncallExpression ast, Environment funcallEnv) throws EvalException {
     return new FakeSkylarkAspect();
   }
 
diff --git a/src/test/shell/bazel/toolchain_test.sh b/src/test/shell/bazel/toolchain_test.sh
index a4e5d6b..7eedb4e 100755
--- a/src/test/shell/bazel/toolchain_test.sh
+++ b/src/test/shell/bazel/toolchain_test.sh
@@ -144,49 +144,6 @@
 EOF
 }
 
-function test_toolchain_with_repository_remapping() {
-  write_test_toolchain
-  write_register_toolchain
-
-  # NOTE: fully qualified label "@main//toolchain:test_toolchain"
-  # is used here, but "//toolchain:test_toolchain" is used in
-  # the toolchain registration in the WORKSPACE file and the
-  # toolchain declaration. Bazel should treat those two labels
-  # as the same
-  rule_name="use_toolchain"
-  toolchain_name="test_toolchain"
-  mkdir -p toolchain
-  cat >> toolchain/rule_${rule_name}.bzl <<EOF
-def _impl(ctx):
-  toolchain = ctx.toolchains['@main//toolchain:${toolchain_name}']
-  message = ctx.attr.message
-  print(
-      'Using toolchain: rule message: "%s", toolchain extra_str: "%s"' %
-         (message, toolchain.extra_str))
-  return []
-
-${rule_name} = rule(
-    implementation = _impl,
-    attrs = {
-        'message': attr.string(),
-    },
-    toolchains = ['@main//toolchain:${toolchain_name}'],
-)
-EOF
-
-  mkdir -p demo
-  cat >> demo/BUILD <<EOF
-load('//toolchain:rule_use_toolchain.bzl', 'use_toolchain')
-use_toolchain(
-    name = 'use',
-    message = 'this is the rule')
-EOF
-
-  bazel build //demo:use --incompatible_remap_main_repo &> $TEST_log \
-      || fail "Build failed"
-  expect_log 'Using toolchain: rule message: "this is the rule", toolchain extra_str: "foo from test_toolchain"'
-}
-
 function test_toolchain_provider() {
   write_test_toolchain