From 4baebd5eb2bc53cd21331ec0bc6fdd8c0251d6c1 Mon Sep 17 00:00:00 2001 From: Luca Di Grazia Date: Sun, 4 Sep 2022 15:51:11 +0200 Subject: [PATCH] bzlmod: Store a Package instead of a Rule in BzlmodRepoRuleValue (https://github.com/bazelbuild/bazel/issues/13316) This is to work around a restriction in Google. The external packages we create in BzlmodRepoRuleFunction host just one rule each; so instead of storing a rule, we can just store the package and get the rule from there. This does require adding the rule to the package, which we weren't doing before. This involved a bit of a detour to clean up the RuleFactory#createAndAddRule method, which for some reason has had a much more sensible overload that's package private since time immemorial. This CL simply removes the overload that just gets two fields out of PackageContext. PiperOrigin-RevId: 387543141 --- .../starlark/StarlarkRuleClassFunctions.java | 566 +++--- .../bazel/bzlmod/BzlmodRepoRuleCreator.java | 5 +- .../lib/bazel/bzlmod/BzlmodRepoRuleValue.java | 14 +- .../starlark/StarlarkRepositoryModule.java | 73 +- .../build/lib/packages/PackageFactory.java | 1800 +++++++---------- .../build/lib/packages/RuleFactory.java | 299 ++- .../lib/skyframe/BzlmodRepoRuleFunction.java | 129 +- .../build/lib/packages/RuleFactoryTest.java | 267 +-- 8 files changed, 1600 insertions(+), 1553 deletions(-) diff --git a/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java b/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java index 3a68926e525..71efd94b35e 100644 --- a/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java +++ b/dataset/GitHub_Java/bazelbuild.bazel/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleClassFunctions.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.analysis.starlark; import static com.google.devtools.build.lib.analysis.BaseRuleClasses.RUN_UNDER; +import static com.google.devtools.build.lib.analysis.BaseRuleClasses.TIMEOUT_DEFAULT; import static com.google.devtools.build.lib.analysis.BaseRuleClasses.getTestRuntimeLabelList; import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; @@ -25,36 +26,43 @@ import static com.google.devtools.build.lib.packages.Type.STRING; import static com.google.devtools.build.lib.packages.Type.STRING_LIST; +import com.github.benmanes.caffeine.cache.CacheLoader; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; import com.google.common.base.Preconditions; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.devtools.build.lib.actions.Artifact; +import com.google.devtools.build.lib.analysis.Allowlist; import com.google.devtools.build.lib.analysis.BaseRuleClasses; -import com.google.devtools.build.lib.analysis.ExecGroupCollection; -import com.google.devtools.build.lib.analysis.RuleDefinitionContext; +import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment; import com.google.devtools.build.lib.analysis.TemplateVariableInfo; import com.google.devtools.build.lib.analysis.config.ConfigAwareRuleClassBuilder; import com.google.devtools.build.lib.analysis.config.HostTransition; import com.google.devtools.build.lib.analysis.config.StarlarkDefinedConfigTransition; +import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition; +import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; +import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory.TransitionType; +import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo; import com.google.devtools.build.lib.analysis.starlark.StarlarkAttrModule.Descriptor; import com.google.devtools.build.lib.analysis.test.TestConfiguration; 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.cmdline.PackageIdentifier; +import com.google.devtools.build.lib.events.Event; +import com.google.devtools.build.lib.events.EventHandler; +import com.google.devtools.build.lib.packages.AllowlistChecker; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.Attribute.StarlarkComputedDefaultTemplate; -import com.google.devtools.build.lib.packages.AttributeContainer; -import com.google.devtools.build.lib.packages.AttributeMap; import com.google.devtools.build.lib.packages.AttributeValueSource; import com.google.devtools.build.lib.packages.BazelModuleContext; import com.google.devtools.build.lib.packages.BazelStarlarkContext; import com.google.devtools.build.lib.packages.BuildSetting; import com.google.devtools.build.lib.packages.BuildType; +import com.google.devtools.build.lib.packages.BuildType.LabelConversionContext; +import com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy.MissingFragmentPolicy; import com.google.devtools.build.lib.packages.ExecGroup; import com.google.devtools.build.lib.packages.FunctionSplitTransitionAllowlist; import com.google.devtools.build.lib.packages.ImplicitOutputsFunction.StarlarkImplicitOutputsFunctionWithCallback; @@ -65,10 +73,12 @@ import com.google.devtools.build.lib.packages.Provider; import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType; +import com.google.devtools.build.lib.packages.RuleClass.ToolchainTransitionMode; import com.google.devtools.build.lib.packages.RuleFactory; import com.google.devtools.build.lib.packages.RuleFactory.BuildLangTypedAttributeValuesMap; import com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException; import com.google.devtools.build.lib.packages.RuleFunction; +import com.google.devtools.build.lib.packages.RuleTransitionData; import com.google.devtools.build.lib.packages.StarlarkAspect; import com.google.devtools.build.lib.packages.StarlarkCallbackHelper; import com.google.devtools.build.lib.packages.StarlarkDefinedAspect; @@ -76,38 +86,40 @@ import com.google.devtools.build.lib.packages.StarlarkProvider; import com.google.devtools.build.lib.packages.StarlarkProviderIdentifier; import com.google.devtools.build.lib.packages.TargetUtils; -import com.google.devtools.build.lib.packages.TestSize; import com.google.devtools.build.lib.packages.Type; import com.google.devtools.build.lib.packages.Type.ConversionException; -import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; -import com.google.devtools.build.lib.skylarkbuildapi.StarlarkRuleFunctionsApi; -import com.google.devtools.build.lib.syntax.Dict; -import com.google.devtools.build.lib.syntax.EvalException; -import com.google.devtools.build.lib.syntax.Identifier; -import com.google.devtools.build.lib.syntax.Location; -import com.google.devtools.build.lib.syntax.Module; -import com.google.devtools.build.lib.syntax.Printer; -import com.google.devtools.build.lib.syntax.Sequence; -import com.google.devtools.build.lib.syntax.Starlark; -import com.google.devtools.build.lib.syntax.StarlarkCallable; -import com.google.devtools.build.lib.syntax.StarlarkFunction; -import com.google.devtools.build.lib.syntax.StarlarkThread; -import com.google.devtools.build.lib.syntax.Tuple; +import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant; +import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleFunctionsApi; +import com.google.devtools.build.lib.util.FileType; import com.google.devtools.build.lib.util.FileTypeSet; import com.google.devtools.build.lib.util.Pair; +import com.google.errorprone.annotations.FormatMethod; import java.util.Collection; import java.util.Map; -import java.util.concurrent.ExecutionException; +import javax.annotation.Nullable; +import net.starlark.java.eval.Debug; +import net.starlark.java.eval.Dict; +import net.starlark.java.eval.EvalException; +import net.starlark.java.eval.Module; +import net.starlark.java.eval.Printer; +import net.starlark.java.eval.Sequence; +import net.starlark.java.eval.Starlark; +import net.starlark.java.eval.StarlarkCallable; +import net.starlark.java.eval.StarlarkFunction; +import net.starlark.java.eval.StarlarkInt; +import net.starlark.java.eval.StarlarkThread; +import net.starlark.java.eval.Tuple; +import net.starlark.java.syntax.Identifier; +import net.starlark.java.syntax.Location; /** A helper class to provide an easier API for Starlark rule definitions. */ public class StarlarkRuleClassFunctions implements StarlarkRuleFunctionsApi { - // TODO(bazel-team): Copied from ConfiguredRuleClassProvider for the transition from built-in // rules to Starlark extensions. Using the same instance would require a large refactoring. // If we don't want to support old built-in rules and Starlark simultaneously // (except for transition phase) it's probably OK. private static final LoadingCache labelCache = - CacheBuilder.newBuilder() + Caffeine.newBuilder() .build( new CacheLoader() { @Override @@ -127,8 +139,7 @@ public Label load(String from) throws Exception { /** Parent rule class for non-executable non-test Starlark rules. */ public static final RuleClass baseRule = BaseRuleClasses.commonCoreAndStarlarkAttributes( - BaseRuleClasses.nameAttribute( - new RuleClass.Builder("$base_rule", RuleClassType.ABSTRACT, true)) + new RuleClass.Builder("$base_rule", RuleClassType.ABSTRACT, true) .add(attr("expect_failure", STRING))) // TODO(skylark-team): Allow Starlark rules to extend native rules and remove duplication. .add( @@ -136,12 +147,23 @@ public Label load(String from) throws Exception { .allowedFileTypes(FileTypeSet.NO_FILE) .mandatoryProviders(ImmutableList.of(TemplateVariableInfo.PROVIDER.id())) .dontCheckConstraints()) - .add(attr(RuleClass.EXEC_PROPERTIES, Type.STRING_DICT).value(ImmutableMap.of())) + .add(attr(RuleClass.EXEC_PROPERTIES_ATTR, Type.STRING_DICT).value(ImmutableMap.of())) .add( attr(RuleClass.EXEC_COMPATIBLE_WITH_ATTR, BuildType.LABEL_LIST) .allowedFileTypes() .nonconfigurable("Used in toolchain resolution") + .tool( + "exec_compatible_with exists for constraint checking, not to create an" + + " actual dependency") .value(ImmutableList.of())) + .add( + attr(RuleClass.TARGET_COMPATIBLE_WITH_ATTR, LABEL_LIST) + .mandatoryProviders(ConstraintValueInfo.PROVIDER.id()) + // This should be configurable to allow for complex types of restrictions. + .tool( + "target_compatible_with exists for constraint checking, not to create an" + + " actual dependency") + .allowedFileTypes(FileTypeSet.NO_FILE)) .build(); /** Parent rule class for executable non-test Starlark rules. */ @@ -152,103 +174,97 @@ public Label load(String from) throws Exception { .build(); /** Parent rule class for test Starlark rules. */ - public static final RuleClass getTestBaseRule(RuleDefinitionContext env) { + public static RuleClass getTestBaseRule(RuleDefinitionEnvironment env) { String toolsRepository = env.getToolsRepository(); - return new RuleClass.Builder("$test_base_rule", RuleClassType.ABSTRACT, true, baseRule) - .requiresConfigurationFragments(TestConfiguration.class) - .add( - attr("size", STRING) - .value("medium") - .taggable() - .nonconfigurable("used in loading phase rule validation logic")) - .add( - attr("timeout", STRING) - .taggable() - .nonconfigurable("used in loading phase rule validation logic") - .value(timeoutAttribute)) - .add( - attr("flaky", BOOLEAN) - .value(false) - .taggable() - .nonconfigurable("taggable - called in Rule.getRuleTags")) - .add(attr("shard_count", INTEGER).value(-1)) - .add( - attr("local", BOOLEAN) - .value(false) - .taggable() - .nonconfigurable( - "policy decision: this should be consistent across configurations")) - .add(attr("args", STRING_LIST)) - // Input files for every test action - .add( - attr("$test_wrapper", LABEL) - .cfg(HostTransition.createFactory()) - .singleArtifact() - .value(labelCache.getUnchecked(toolsRepository + "//tools/test:test_wrapper"))) - .add( - attr("$xml_writer", LABEL) - .cfg(HostTransition.createFactory()) - .singleArtifact() - .value(labelCache.getUnchecked(toolsRepository + "//tools/test:xml_writer"))) - .add( - attr("$test_runtime", LABEL_LIST) - .cfg(HostTransition.createFactory()) - // Getting this default value through the getTestRuntimeLabelList helper ensures we - // reuse the same ImmutableList