From 1d97bcb0cae9c4fef06d4a1f48b13d9919492440 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Wed, 6 Nov 2024 10:28:47 +0100 Subject: [PATCH] Add `--inject_repository` and fix crash on overridden non-existent repo RELNOTES: The new `--inject_repository` flag can be used to add new repositories via the CLI with `--noenable_workspace`. Such repositories behave as if they were declared by `local_repository` via `use_repo_rule` in the root module. --- .../com/google/devtools/build/lib/bazel/BUILD | 1 + .../lib/bazel/BazelRepositoryModule.java | 23 +++- .../devtools/build/lib/bazel/bzlmod/BUILD | 1 + .../lib/bazel/bzlmod/ModuleFileFunction.java | 50 +++++++++ .../lib/bazel/bzlmod/ModuleFileGlobals.java | 3 +- .../lib/bazel/bzlmod/ModuleThreadContext.java | 6 + .../devtools/build/lib/bazel/repository/BUILD | 1 + .../bazel/repository/RepositoryOptions.java | 79 ++++++++----- .../com/google/devtools/build/lib/rules/BUILD | 1 + .../RepositoryDelegatorFunction.java | 4 +- .../google/devtools/build/lib/skyframe/BUILD | 1 + .../skyframe/RepositoryMappingFunction.java | 24 +++- .../build/lib/skyframe/packages/BUILD | 1 + .../skyframe/packages/BazelPackageLoader.java | 3 +- .../build/lib/analysis/util/AnalysisMock.java | 4 +- .../lib/analysis/util/AnalysisTestCase.java | 8 +- .../devtools/build/lib/analysis/util/BUILD | 1 + .../bzlmod/BazelDepGraphFunctionTest.java | 1 + .../BazelModuleResolutionFunctionTest.java | 4 +- .../bzlmod/BzlmodRepoRuleFunctionTest.java | 1 + .../build/lib/bazel/bzlmod/DiscoveryTest.java | 5 +- .../bzlmod/ModuleExtensionResolutionTest.java | 4 +- .../bazel/bzlmod/ModuleFileFunctionTest.java | 5 +- .../devtools/build/lib/query2/testutil/BUILD | 1 + .../query2/testutil/SkyframeQueryHelper.java | 5 +- .../repository/RepositoryDelegatorTest.java | 5 +- ...ractCollectPackagesUnderDirectoryTest.java | 2 +- .../google/devtools/build/lib/skyframe/BUILD | 4 + .../ContainingPackageLookupFunctionTest.java | 2 +- .../build/lib/skyframe/FileFunctionTest.java | 2 +- .../skyframe/PackageLookupFunctionTest.java | 2 +- .../PrepareDepsOfPatternsFunctionTest.java | 2 + .../py/bazel/bzlmod/bazel_overrides_test.py | 105 ++++++++++++++++++ src/test/shell/integration/aspect_test.sh | 31 ++++++ 34 files changed, 344 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/BUILD index f8356c97339e58..b829fb6cccf8b4 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/BUILD @@ -52,6 +52,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function", "//src/main/java/com/google/devtools/build/lib/skyframe:mutable_supplier", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_executor_repository_helpers_holder", diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java index 1cf6c5e5d2a06e..a803bfd4836916 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java @@ -104,6 +104,7 @@ import com.google.devtools.build.lib.skyframe.MutableSupplier; import com.google.devtools.build.lib.skyframe.PrecomputedValue; import com.google.devtools.build.lib.skyframe.PrecomputedValue.Injected; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.skyframe.SkyframeExecutorRepositoryHelpersHolder; import com.google.devtools.build.lib.starlarkbuildapi.repository.RepositoryBootstrap; @@ -146,6 +147,7 @@ public class BazelRepositoryModule extends BlazeModule { private final MutableSupplier> clientEnvironmentSupplier = new MutableSupplier<>(); private ImmutableMap overrides = ImmutableMap.of(); + private ImmutableMap injections = ImmutableMap.of(); private ImmutableMap moduleOverrides = ImmutableMap.of(); private Optional resolvedFileReplacingWorkspace = Optional.empty(); private FileSystem filesystem; @@ -469,6 +471,24 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException { overrides = ImmutableMap.of(); } + if (repoOptions.repositoryInjections != null) { + Map injectionMap = new LinkedHashMap<>(); + for (RepositoryOptions.RepositoryInjection injection : repoOptions.repositoryInjections) { + if (injection.path().isEmpty()) { + injectionMap.remove(injection.apparentName()); + continue; + } + String repoPath = getAbsolutePath(injection.path(), env); + injectionMap.put(injection.apparentName(), PathFragment.create(repoPath)); + } + ImmutableMap newInjections = ImmutableMap.copyOf(injectionMap); + if (!Maps.difference(injections, newInjections).areEqual()) { + injections = newInjections; + } + } else { + injections = ImmutableMap.of(); + } + if (repoOptions.moduleOverrides != null) { Map moduleOverrideMap = new LinkedHashMap<>(); for (RepositoryOptions.ModuleOverride override : repoOptions.moduleOverrides) { @@ -603,7 +623,8 @@ public ImmutableList getPrecomputedValues() { lastRegistryInvalidation = now; } return ImmutableList.of( - PrecomputedValue.injected(RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, overrides), + PrecomputedValue.injected(RepositoryMappingFunction.REPOSITORY_OVERRIDES, overrides), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, injections), PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, moduleOverrides), PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD index 8edaa2957afcc6..76e74e17d63775 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD @@ -259,6 +259,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_function", "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_value", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", "//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs", diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunction.java index 0c7bd44a509a71..1a27cec8cd3071 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunction.java @@ -76,11 +76,13 @@ import java.util.Optional; import java.util.stream.Stream; import javax.annotation.Nullable; +import net.starlark.java.eval.Dict; import net.starlark.java.eval.EvalException; import net.starlark.java.eval.Mutability; import net.starlark.java.eval.StarlarkSemantics; import net.starlark.java.eval.StarlarkThread; import net.starlark.java.eval.SymbolGenerator; +import net.starlark.java.syntax.Location; /** * Takes a {@link ModuleKey} and its override (if any), retrieves the module file from a registry or @@ -95,6 +97,8 @@ public class ModuleFileFunction implements SkyFunction { public static final Precomputed> MODULE_OVERRIDES = new Precomputed<>("module_overrides"); + public static final Precomputed> INJECTED_REPOSITORIES = + new Precomputed<>("repository_injections"); private final BazelStarlarkEnvironment starlarkEnv; private final Path workspaceRoot; @@ -194,6 +198,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) // Dev dependencies should always be ignored if the current module isn't the root module /* ignoreDevDeps= */ true, builtinModules, + /* injectedRepositories= */ ImmutableMap.of(), // Disable printing for modules from registries. We don't want them to be able to spam // the console during resolution, but module files potentially edited by the user as // part of a non-registry override should permit printing to aid debugging. @@ -287,6 +292,7 @@ private SkyValue computeForRootModule( ImmutableMap.copyOf(state.includeLabelToCompiledModuleFile), builtinModules, MODULE_OVERRIDES.get(env), + INJECTED_REPOSITORIES.get(env), IGNORE_DEV_DEPS.get(env), starlarkSemantics, env.getListener(), @@ -420,6 +426,7 @@ public static RootModuleFileValue evaluateRootModuleFile( ImmutableMap includeLabelToCompiledModuleFile, ImmutableMap builtinModules, Map commandOverrides, + Map injectedRepositories, boolean ignoreDevDeps, StarlarkSemantics starlarkSemantics, ExtendedEventHandler eventHandler, @@ -432,6 +439,7 @@ public static RootModuleFileValue evaluateRootModuleFile( ModuleKey.ROOT, ignoreDevDeps, builtinModules, + injectedRepositories, /* printIsNoop= */ false, starlarkSemantics, eventHandler, @@ -495,6 +503,7 @@ private static ModuleThreadContext execModuleFile( ModuleKey moduleKey, boolean ignoreDevDeps, ImmutableMap builtinModules, + Map injectedRepositories, boolean printIsNoop, StarlarkSemantics starlarkSemantics, ExtendedEventHandler eventHandler, @@ -524,6 +533,8 @@ private static ModuleThreadContext execModuleFile( } } }); + + injectRepos(injectedRepositories, context, thread); compiledRootModuleFile.runOnThread(thread); } catch (EvalException e) { eventHandler.handle(Event.error(e.getInnermostLocation(), e.getMessageWithStack())); @@ -532,6 +543,45 @@ private static ModuleThreadContext execModuleFile( return context; } + // Adds a local_repository for each repository injected via --injected_repositories. + private static void injectRepos( + Map injectedRepositories, + ModuleThreadContext context, + StarlarkThread thread) + throws EvalException { + // Use the innate extension backing use_repo_rule. + ModuleThreadContext.ModuleExtensionUsageBuilder usageBuilder = + new ModuleThreadContext.ModuleExtensionUsageBuilder( + context, + "//:MODULE.bazel", + "@bazel_tools//tools/build_defs/repo:local.bzl%local_repository", + /* isolate= */ false); + ModuleFileGlobals.ModuleExtensionProxy extensionProxy = + new ModuleFileGlobals.ModuleExtensionProxy( + usageBuilder, + ModuleExtensionUsage.Proxy.builder() + .setDevDependency(true) + .setLocation(Location.BUILTIN) + .setContainingModuleFilePath(context.getCurrentModuleFilePath())); + for (var injectedRepository : injectedRepositories.entrySet()) { + extensionProxy + .getValue("repo") + .call( + Dict.copyOf( + thread.mutability(), + ImmutableMap.of( + "name", injectedRepository.getKey(), + "path", injectedRepository.getValue().getPathString())), + thread); + extensionProxy.addImport( + injectedRepository.getKey(), + injectedRepository.getKey(), + "by --inject_repository", + thread.getCallStack()); + } + context.getExtensionUsageBuilders().add(usageBuilder); + } + /** * Result of a {@link #getModuleFile} call. * diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java index 9e17ab282482db..fd63d31395a906 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java @@ -780,7 +780,8 @@ public RepoRuleProxy useRepoRule(String bzlFile, String ruleName, StarlarkThread context.setNonModuleCalled(); // Not a valid Starlark identifier so that it can't collide with a real extension. String extensionName = bzlFile + '%' + ruleName; - // Find or create the builder for the singular "innate" extension of this module. + // Find or create the builder for the singular "innate" extension of this repo rule for this + // module. for (ModuleExtensionUsageBuilder usageBuilder : context.getExtensionUsageBuilders()) { if (usageBuilder.isForExtension("//:MODULE.bazel", extensionName)) { return new RepoRuleProxy(usageBuilder); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleThreadContext.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleThreadContext.java index 98067f065abb64..99466525bb5434 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleThreadContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleThreadContext.java @@ -87,6 +87,9 @@ record RepoOverride( String extensionName, ImmutableList stack) { Location location() { + if (stack.size() < 2) { + return Location.BUILTIN; + } // Skip over the override_repo builtin frame. return stack.reverse().get(1).location; } @@ -94,6 +97,9 @@ Location location() { record RepoNameUsage(String how, ImmutableList stack) { Location location() { + if (stack.size() < 2) { + return Location.BUILTIN; + } // Skip over the override_repo builtin frame. return stack.reverse().get(1).location; } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD b/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD index 30b5d051d1d556..d01d8826c1c1d5 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/BUILD @@ -72,6 +72,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/util", "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", "//src/main/java/com/google/devtools/common/options", + "//src/main/java/net/starlark/java/eval", "//third_party:auto_value", "//third_party:guava", ], diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryOptions.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryOptions.java index f64dc4a722714a..527f18843478ea 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryOptions.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryOptions.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.bazel.repository; -import com.google.auto.value.AutoValue; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; import com.google.devtools.build.lib.cmdline.RepositoryName; import com.google.devtools.build.lib.util.OptionsUtils; @@ -28,6 +27,7 @@ import com.google.devtools.common.options.OptionsBase; import com.google.devtools.common.options.OptionsParsingException; import java.util.List; +import net.starlark.java.eval.EvalException; /** Command-line options for repositories. */ public class RepositoryOptions extends OptionsBase { @@ -131,8 +131,6 @@ public class RepositoryOptions extends OptionsBase { + "to download them.") public List experimentalDistdir; - - @Option( name = "override_repository", defaultValue = "null", @@ -149,6 +147,24 @@ public class RepositoryOptions extends OptionsBase { + " previous overrides.") public List repositoryOverrides; + @Option( + name = "inject_repository", + defaultValue = "null", + allowMultiple = true, + converter = RepositoryInjectionConverter.class, + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = + "Adds a new repository with a local path in the form of =. This" + + " only takes effect with --enable_bzlmod and is equivalent to adding a" + + " corresponding `local_repository` to the root module's MODULE.bazel file via" + + " `use_repo_rule`. If the given path is an absolute path, it will be used as it is." + + " If the given path is a relative path, it is relative to the current working" + + " directory. If the given path starts with '%workspace%, it is relative to the" + + " workspace root, which is the output of `bazel info workspace`. If the given path" + + " is empty, then remove any previous overrides.") + public List repositoryInjections; + @Option( name = "override_module", defaultValue = "null", @@ -362,7 +378,7 @@ public RepositoryOverride convert(String input) throws OptionsParsingException { OptionsUtils.PathFragmentConverter pathConverter = new OptionsUtils.PathFragmentConverter(); String pathString = pathConverter.convert(pieces[1]).getPathString(); try { - return RepositoryOverride.create(RepositoryName.create(pieces[0]), pathString); + return new RepositoryOverride(RepositoryName.create(pieces[0]), pathString); } catch (LabelSyntaxException e) { throw new OptionsParsingException("Invalid repository name given to override", input, e); } @@ -374,6 +390,35 @@ public String getTypeDescription() { } } + /** + * Converts from an equals-separated pair of strings into RepositoryName->PathFragment mapping. + */ + public static class RepositoryInjectionConverter + extends Converter.Contextless { + + @Override + public RepositoryInjection convert(String input) throws OptionsParsingException { + String[] pieces = input.split("=", 2); + if (pieces.length != 2) { + throw new OptionsParsingException( + "Repository injections must be of the form 'repository-name=path'", input); + } + OptionsUtils.PathFragmentConverter pathConverter = new OptionsUtils.PathFragmentConverter(); + String pathString = pathConverter.convert(pieces[1]).getPathString(); + try { + RepositoryName.validateUserProvidedRepoName(pieces[0]); + return new RepositoryInjection(pieces[0], pathString); + } catch (EvalException e) { + throw new OptionsParsingException("Invalid repository name given to inject", input, e); + } + } + + @Override + public String getTypeDescription() { + return "an equals-separated mapping of repository name to path"; + } + } + /** Converts from an equals-separated pair of strings into ModuleName->PathFragment mapping. */ public static class ModuleOverrideConverter extends Converter.Contextless { @@ -396,7 +441,7 @@ public ModuleOverride convert(String input) throws OptionsParsingException { OptionsUtils.PathFragmentConverter pathConverter = new OptionsUtils.PathFragmentConverter(); String pathString = pathConverter.convert(pieces[1]).getPathString(); - return ModuleOverride.create(pieces[0], pathString); + return new ModuleOverride(pieces[0], pathString); } @Override @@ -406,28 +451,10 @@ public String getTypeDescription() { } /** A repository override, represented by a name and an absolute path to a repository. */ - @AutoValue - public abstract static class RepositoryOverride { + public record RepositoryOverride(RepositoryName repositoryName, String path) {} - private static RepositoryOverride create(RepositoryName repositoryName, String path) { - return new AutoValue_RepositoryOptions_RepositoryOverride(repositoryName, path); - } - - public abstract RepositoryName repositoryName(); - - public abstract String path(); - } + public record RepositoryInjection(String apparentName, String path) {} /** A module override, represented by a name and an absolute path to a module. */ - @AutoValue - public abstract static class ModuleOverride { - - private static ModuleOverride create(String moduleName, String path) { - return new AutoValue_RepositoryOptions_ModuleOverride(moduleName, path); - } - - public abstract String moduleName(); - - public abstract String path(); - } + public record ModuleOverride(String moduleName, String path) {} } diff --git a/src/main/java/com/google/devtools/build/lib/rules/BUILD b/src/main/java/com/google/devtools/build/lib/rules/BUILD index 17324757b197d6..14aced9bafb08e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/BUILD +++ b/src/main/java/com/google/devtools/build/lib/rules/BUILD @@ -413,6 +413,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_function", "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_value", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_value_dirtiness_checker", "//src/main/java/com/google/devtools/build/lib/util", diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java index 25f429c8299370..5fbe831fc76db6 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.rules.repository; +import static com.google.devtools.build.lib.skyframe.RepositoryMappingFunction.REPOSITORY_OVERRIDES; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.annotations.VisibleForTesting; @@ -71,9 +72,6 @@ * this function. */ public final class RepositoryDelegatorFunction implements SkyFunction { - public static final Precomputed> REPOSITORY_OVERRIDES = - new Precomputed<>("repository_overrides"); - public static final String FORCE_FETCH_DISABLED = ""; public static final Precomputed FORCE_FETCH = diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/BUILD b/src/main/java/com/google/devtools/build/lib/skyframe/BUILD index 62307d47ec0f0d..0a6731bb1061d6 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/BUILD +++ b/src/main/java/com/google/devtools/build/lib/skyframe/BUILD @@ -2396,6 +2396,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/cmdline", "//src/main/java/com/google/devtools/build/lib/packages", "//src/main/java/com/google/devtools/build/lib/packages/semantics", + "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects", "//src/main/java/net/starlark/java/eval", "//third_party:guava", diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java index 8457f54f8ce22c..64db6c42f921b2 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryMappingFunction.java @@ -28,6 +28,7 @@ import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.RuleClassProvider; import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; +import com.google.devtools.build.lib.vfs.PathFragment; import com.google.devtools.build.skyframe.SkyFunction; import com.google.devtools.build.skyframe.SkyFunctionException; import com.google.devtools.build.skyframe.SkyKey; @@ -41,6 +42,8 @@ /** {@link SkyFunction} for {@link RepositoryMappingValue}s. */ public class RepositoryMappingFunction implements SkyFunction { + public static final PrecomputedValue.Precomputed> + REPOSITORY_OVERRIDES = new PrecomputedValue.Precomputed<>("repository_overrides"); private final RuleClassProvider ruleClassProvider; public RepositoryMappingFunction(RuleClassProvider ruleClassProvider) { @@ -51,11 +54,28 @@ public RepositoryMappingFunction(RuleClassProvider ruleClassProvider) { @Override public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException { + RepositoryMappingValue.Key key = (RepositoryMappingValue.Key) skyKey; + RepositoryMappingValue repositoryMappingValue = computeInternal(key, env); + if (repositoryMappingValue == null) { + return null; + } + if (repositoryMappingValue == RepositoryMappingValue.NOT_FOUND_VALUE + && REPOSITORY_OVERRIDES.get(env).containsKey(key.repoName())) { + throw new RepositoryMappingFunctionException( + String.format( + "the repository %s does not exist, but has been specified as overridden with --override_repository. Use --inject_repository instead to add a new repository.", + key.repoName())); + } + return repositoryMappingValue; + } + + private RepositoryMappingValue computeInternal(RepositoryMappingValue.Key skyKey, Environment env) + throws SkyFunctionException, InterruptedException { StarlarkSemantics starlarkSemantics = PrecomputedValue.STARLARK_SEMANTICS.get(env); if (starlarkSemantics == null) { return null; } - RepositoryName repositoryName = ((RepositoryMappingValue.Key) skyKey).repoName(); + RepositoryName repositoryName = skyKey.repoName(); boolean enableBzlmod = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD); boolean enableWorkspace = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE); @@ -109,7 +129,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) } if (repositoryName.isMain() - && ((RepositoryMappingValue.Key) skyKey).rootModuleShouldSeeWorkspaceRepos() + && skyKey.rootModuleShouldSeeWorkspaceRepos() && enableWorkspace) { // The root module should be able to see repos defined in WORKSPACE. Therefore, we find all // workspace repos and add them as extra visible repos in root module's repo mappings. diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/packages/BUILD b/src/main/java/com/google/devtools/build/lib/skyframe/packages/BUILD index 3926065e8e5c01..00c6fdb2602efc 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/packages/BUILD +++ b/src/main/java/com/google/devtools/build/lib/skyframe/packages/BUILD @@ -103,6 +103,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/skyframe:directory_listing_function", "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_function", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", "//src/main/java/com/google/devtools/build/lib/vfs", diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoader.java b/src/main/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoader.java index 067333494e99b2..10267b3488b9f1 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoader.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoader.java @@ -46,6 +46,7 @@ import com.google.devtools.build.lib.skyframe.PackageFunction.ActionOnIOExceptionReadingBuildFile; import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.Root; @@ -103,7 +104,7 @@ private Builder(Root workspaceDir, Path installBase, Path outputBase, AtomicBool PrecomputedValue.injected(PrecomputedValue.ACTION_ENV, ImmutableMap.of()), PrecomputedValue.injected(PrecomputedValue.REPO_ENV, ImmutableMap.of()), PrecomputedValue.injected( - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, + RepositoryMappingFunction.REPOSITORY_OVERRIDES, Suppliers.ofInstance(ImmutableMap.of())), PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisMock.java b/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisMock.java index 5acb64b1de36fb..fdad032997c68d 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisMock.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisMock.java @@ -49,6 +49,7 @@ import com.google.devtools.build.lib.skyframe.BazelSkyframeExecutorConstants; import com.google.devtools.build.lib.skyframe.ClientEnvironmentFunction; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.skyframe.packages.PackageFactoryBuilderWithSkyframeForTesting; import com.google.devtools.build.lib.testutil.TestConstants; @@ -252,7 +253,7 @@ public ImmutableList getPrecomputedValues() { PrecomputedValue.injected(PrecomputedValue.REPO_ENV, ImmutableMap.of()), PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), PrecomputedValue.injected( @@ -262,6 +263,7 @@ public ImmutableList getPrecomputedValues() { PrecomputedValue.injected(RepositoryDelegatorFunction.DISABLE_NATIVE_REPO_RULES, false), PrecomputedValue.injected(ModuleFileFunction.REGISTRIES, ImmutableSet.of()), PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected(YankedVersionsUtil.ALLOWED_YANKED_VERSIONS, ImmutableList.of()), PrecomputedValue.injected( diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java b/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java index 1d62f4cf80efa2..437baa7f347fe5 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisTestCase.java @@ -77,6 +77,7 @@ import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData; import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyframeExecutor; import com.google.devtools.build.lib.skyframe.SkyframeExecutorRepositoryHelpersHolder; import com.google.devtools.build.lib.skyframe.TargetPatternPhaseValue; @@ -239,12 +240,14 @@ protected void useRuleClassProvider(ConfiguredRuleClassProvider ruleClassProvide PrecomputedValue.injected( ModuleFileFunction.REGISTRIES, ImmutableSet.of(registry.getUrl())), PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected( + ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.DISABLE_NATIVE_REPO_RULES, false), PrecomputedValue.injected( ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.FORCE_FETCH, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED), @@ -289,7 +292,7 @@ private void reinitializeSkyframeExecutor() { PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), PrecomputedValue.injected( - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.FORCE_FETCH, @@ -299,6 +302,7 @@ private void reinitializeSkyframeExecutor() { PrecomputedValue.injected( ModuleFileFunction.REGISTRIES, ImmutableSet.of(registry.getUrl())), PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected(RepositoryDelegatorFunction.DISABLE_NATIVE_REPO_RULES, false), PrecomputedValue.injected( BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES, diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/util/BUILD index 4c8df2c63e268e..8d07da0f71b9f4 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/BUILD +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/BUILD @@ -109,6 +109,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/skyframe:diff_awareness", "//src/main/java/com/google/devtools/build/lib/skyframe:package_roots_no_symlink_creation", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_executor_repository_helpers_holder", diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunctionTest.java index 0997f03f20252f..dd669ae718fdd9 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunctionTest.java @@ -148,6 +148,7 @@ public void setup() throws Exception { differencer, StarlarkSemantics.builder().setBool(BuildLanguageOptions.ENABLE_BZLMOD, true).build()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES.set( diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionFunctionTest.java index fdf508a71609b7..ef59c79260d547 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleResolutionFunctionTest.java @@ -48,6 +48,7 @@ import com.google.devtools.build.lib.skyframe.FileStateFunction; import com.google.devtools.build.lib.skyframe.PrecomputedFunction; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.starlarkbuildapi.repository.RepositoryBootstrap; import com.google.devtools.build.lib.testutil.FoundationTestCase; @@ -171,6 +172,7 @@ public void setup() throws Exception { differencer, StarlarkSemantics.builder().setBool(BuildLanguageOptions.ENABLE_BZLMOD, true).build()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES.set( differencer, CheckDirectDepsMode.OFF); @@ -180,7 +182,7 @@ public void setup() throws Exception { YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.set(differencer, ImmutableList.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.VENDOR_DIRECTORY.set(differencer, Optional.empty()); } diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodRepoRuleFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodRepoRuleFunctionTest.java index efea56a6c5a213..e799387928da63 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodRepoRuleFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodRepoRuleFunctionTest.java @@ -152,6 +152,7 @@ public void setup() throws Exception { PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT); ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.set(differencer, ImmutableList.of()); BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES.set( diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/DiscoveryTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/DiscoveryTest.java index 3f53d182869bfa..c65a870724f2b9 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/DiscoveryTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/DiscoveryTest.java @@ -50,6 +50,7 @@ import com.google.devtools.build.lib.skyframe.FileStateFunction; import com.google.devtools.build.lib.skyframe.PrecomputedFunction; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.starlarkbuildapi.repository.RepositoryBootstrap; import com.google.devtools.build.lib.testutil.FoundationTestCase; @@ -221,7 +222,7 @@ private void setUpWithBuiltinModules(ImmutableMap b PrecomputedValue.STARLARK_SEMANTICS.set( differencer, StarlarkSemantics.builder().setBool(BuildLanguageOptions.ENABLE_BZLMOD, true).build()); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); RepositoryDelegatorFunction.VENDOR_DIRECTORY.set(differencer, Optional.empty()); @@ -231,6 +232,7 @@ private void setUpWithBuiltinModules(ImmutableMap b differencer, Optional.empty()); PrecomputedValue.REPO_ENV.set(differencer, ImmutableMap.of()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.set(differencer, ImmutableList.of()); BazelLockFileFunction.LOCKFILE_MODE.set(differencer, LockfileMode.UPDATE); @@ -344,6 +346,7 @@ public void testIgnoreDevDependency() throws Exception { .addModule(createModuleKey("ccc", "2.0"), "module(name='ccc', version='2.0')"); ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of(registry.getUrl())); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, true); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); EvaluationResult result = evaluator.evaluate(ImmutableList.of(DiscoveryValue.KEY), evaluationContext); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java index e73ebaa55a5c9e..6a5bf05a03ce82 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java @@ -266,7 +266,7 @@ public void setup() throws Exception { PrecomputedValue.STARLARK_SEMANTICS.set(differencer, semantics); AutoloadSymbols.AUTOLOAD_SYMBOLS.set( differencer, new AutoloadSymbols(ruleClassProvider, semantics)); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, packageLocator.get()); @@ -274,6 +274,7 @@ public void setup() throws Exception { differencer, Optional.empty()); PrecomputedValue.REPO_ENV.set(differencer, ImmutableMap.of()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.set(differencer, ImmutableList.of()); ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of(registry.getUrl())); @@ -647,6 +648,7 @@ public void multipleModules_ignoreDevDependency() throws Exception { "ext=module_extension(implementation=_ext_impl,tag_classes={'tag':tag})"); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, true); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); SkyKey skyKey = BzlLoadValue.keyForBuild(Label.parseCanonical("@@ext++ext+ext_repo//:data.bzl")); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java index 9cd566b84b88a3..c01fc4966d440b 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunctionTest.java @@ -57,6 +57,7 @@ import com.google.devtools.build.lib.skyframe.PackageLookupFunction.CrossRepositoryLabelViolationStrategy; import com.google.devtools.build.lib.skyframe.PrecomputedFunction; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.starlarkbuildapi.repository.RepositoryBootstrap; import com.google.devtools.build.lib.testutil.FoundationTestCase; @@ -196,7 +197,7 @@ private void setUpWithBuiltinModules(ImmutableMap b PrecomputedValue.STARLARK_SEMANTICS.set( differencer, StarlarkSemantics.builder().setBool(BuildLanguageOptions.ENABLE_BZLMOD, true).build()); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); RepositoryDelegatorFunction.VENDOR_DIRECTORY.set(differencer, Optional.empty()); @@ -206,6 +207,7 @@ private void setUpWithBuiltinModules(ImmutableMap b differencer, Optional.empty()); PrecomputedValue.REPO_ENV.set(differencer, ImmutableMap.of()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.set(differencer, ImmutableList.of()); BazelLockFileFunction.LOCKFILE_MODE.set(differencer, LockfileMode.UPDATE); @@ -347,6 +349,7 @@ public void testRootModule_overridesIgnoredWithIgnoreDevDependency() throws Exce FakeRegistry registry = registryFactory.newFakeRegistry("/foo"); ModuleFileFunction.REGISTRIES.set(differencer, ImmutableSet.of(registry.getUrl())); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, true); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); EvaluationResult result = evaluator.evaluate( diff --git a/src/test/java/com/google/devtools/build/lib/query2/testutil/BUILD b/src/test/java/com/google/devtools/build/lib/query2/testutil/BUILD index 86f245da1ff3c4..3cbe3ce384fbc9 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/testutil/BUILD +++ b/src/test/java/com/google/devtools/build/lib/query2/testutil/BUILD @@ -48,6 +48,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function", "//src/main/java/com/google/devtools/build/lib/skyframe:ignored_package_prefixes_function", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", "//src/main/java/com/google/devtools/build/lib/skyframe/packages:PackageFactoryBuilderWithSkyframeForTesting", "//src/main/java/com/google/devtools/build/lib/testing/common:fake-options", diff --git a/src/test/java/com/google/devtools/build/lib/query2/testutil/SkyframeQueryHelper.java b/src/test/java/com/google/devtools/build/lib/query2/testutil/SkyframeQueryHelper.java index 362644b2d1c4fb..29bcc387f6c809 100644 --- a/src/test/java/com/google/devtools/build/lib/query2/testutil/SkyframeQueryHelper.java +++ b/src/test/java/com/google/devtools/build/lib/query2/testutil/SkyframeQueryHelper.java @@ -64,6 +64,7 @@ import com.google.devtools.build.lib.runtime.QuiescingExecutorsImpl; import com.google.devtools.build.lib.skyframe.BazelSkyframeExecutorConstants; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SkyframeExecutor; import com.google.devtools.build.lib.skyframe.SkyframeTargetPatternEvaluator; import com.google.devtools.build.lib.skyframe.packages.PackageFactoryBuilderWithSkyframeForTesting; @@ -404,6 +405,7 @@ protected SkyframeExecutor createSkyframeExecutor(ConfiguredRuleClassProvider ru PrecomputedValue.injected( ModuleFileFunction.REGISTRIES, ImmutableSet.of(registry.getUrl())), PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected( BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES, CheckDirectDepsMode.WARNING), @@ -431,7 +433,7 @@ protected SkyframeExecutor createSkyframeExecutor(ConfiguredRuleClassProvider ru PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), PrecomputedValue.injected( - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.FORCE_FETCH, @@ -441,6 +443,7 @@ protected SkyframeExecutor createSkyframeExecutor(ConfiguredRuleClassProvider ru PrecomputedValue.injected( ModuleFileFunction.REGISTRIES, ImmutableSet.of(registry.getUrl())), PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected(RepositoryDelegatorFunction.DISABLE_NATIVE_REPO_RULES, false), PrecomputedValue.injected( BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES, diff --git a/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorTest.java b/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorTest.java index 908a1fc0ef8190..35a3be72512768 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorTest.java @@ -265,7 +265,7 @@ public void setupDelegator() throws Exception { differencer); overrideDirectory = scratch.dir("/foo"); scratch.file("/foo/WORKSPACE"); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.IS_VENDOR_COMMAND.set(differencer, false); RepositoryDelegatorFunction.VENDOR_DIRECTORY.set(differencer, Optional.empty()); RepositoryDelegatorFunction.FORCE_FETCH.set( @@ -280,6 +280,7 @@ public void setupDelegator() throws Exception { differencer, Optional.empty()); PrecomputedValue.REPO_ENV.set(differencer, ImmutableMap.of()); ModuleFileFunction.IGNORE_DEV_DEPS.set(differencer, false); + ModuleFileFunction.INJECTED_REPOSITORIES.set(differencer, ImmutableMap.of()); ModuleFileFunction.MODULE_OVERRIDES.set(differencer, ImmutableMap.of()); YankedVersionsUtil.ALLOWED_YANKED_VERSIONS.set(differencer, ImmutableList.of()); BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES.set( @@ -291,7 +292,7 @@ public void setupDelegator() throws Exception { @Test public void testOverride() throws Exception { - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set( + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set( differencer, ImmutableMap.of(RepositoryName.createUnvalidated("foo"), overrideDirectory.asFragment())); diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/AbstractCollectPackagesUnderDirectoryTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/AbstractCollectPackagesUnderDirectoryTest.java index d15d9d898530d3..e570d188df408f 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/AbstractCollectPackagesUnderDirectoryTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/AbstractCollectPackagesUnderDirectoryTest.java @@ -320,7 +320,7 @@ private void initEvaluator() PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), PrecomputedValue.injected( - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.FORCE_FETCH, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED), diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/BUILD b/src/test/java/com/google/devtools/build/lib/skyframe/BUILD index d22e62f4aeb6e7..f9d61d0a44516b 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/BUILD +++ b/src/test/java/com/google/devtools/build/lib/skyframe/BUILD @@ -127,6 +127,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function", "//src/main/java/com/google/devtools/build/lib/skyframe:collect_packages_under_directory_value", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", "//src/main/java/com/google/devtools/build/lib/skyframe/packages:PackageFactoryBuilderWithSkyframeForTesting", "//src/main/java/com/google/devtools/build/lib/testing/common:fake-options", @@ -1084,6 +1085,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib/skyframe:file_function", "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_function", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_value", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", @@ -1175,6 +1177,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_function", "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_value", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_value", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", @@ -1442,6 +1445,7 @@ java_test( "//src/main/java/com/google/devtools/build/lib/skyframe:package_lookup_value", "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", "//src/main/java/com/google/devtools/build/lib/skyframe:repo_file_function", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_function", "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_value", "//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions", "//src/main/java/com/google/devtools/build/lib/skyframe:skyframe_cluster", diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunctionTest.java index 5c14ff3a362353..d41876bbbf0dcd 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/ContainingPackageLookupFunctionTest.java @@ -176,7 +176,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) { // https://github.com/bazelbuild/bazel/issues/22208 PrecomputedValue.STARLARK_SEMANTICS.set( differencer, StarlarkSemantics.DEFAULT.toBuilder().setBool(ENABLE_WORKSPACE, true).build()); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); RepositoryDelegatorFunction.DISABLE_NATIVE_REPO_RULES.set(differencer, false); diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java index bb54612df6b605..5b0075d07e4b7d 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/FileFunctionTest.java @@ -237,7 +237,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) { differencer); PrecomputedValue.BUILD_ID.set(differencer, UUID.randomUUID()); PrecomputedValue.PATH_PACKAGE_LOCATOR.set(differencer, pkgLocator); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); RepositoryDelegatorFunction.VENDOR_DIRECTORY.set(differencer, Optional.empty()); diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/PackageLookupFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/PackageLookupFunctionTest.java index 801f50946c7045..293aaead3025f3 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/PackageLookupFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/PackageLookupFunctionTest.java @@ -192,7 +192,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) { // https://github.com/bazelbuild/bazel/issues/22208 PrecomputedValue.STARLARK_SEMANTICS.set( differencer, StarlarkSemantics.DEFAULT.toBuilder().setBool(ENABLE_WORKSPACE, true).build()); - RepositoryDelegatorFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); + RepositoryMappingFunction.REPOSITORY_OVERRIDES.set(differencer, ImmutableMap.of()); RepositoryDelegatorFunction.FORCE_FETCH.set( differencer, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED); RepositoryDelegatorFunction.DISABLE_NATIVE_REPO_RULES.set(differencer, false); diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsFunctionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsFunctionTest.java index 7dd03f7edf25aa..8e59db2870e749 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsFunctionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternsFunctionTest.java @@ -19,6 +19,7 @@ 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.common.eventbus.EventBus; import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; @@ -272,6 +273,7 @@ protected ImmutableList extraPrecomputedValues() { PrecomputedValue.injected( ModuleFileFunction.REGISTRIES, ImmutableSet.of(registry.getUrl())), PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected( BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES, CheckDirectDepsMode.WARNING), PrecomputedValue.injected(YankedVersionsUtil.ALLOWED_YANKED_VERSIONS, ImmutableList.of()), diff --git a/src/test/py/bazel/bzlmod/bazel_overrides_test.py b/src/test/py/bazel/bzlmod/bazel_overrides_test.py index 0f6ebf8d728472..79bd0d666b526e 100644 --- a/src/test/py/bazel/bzlmod/bazel_overrides_test.py +++ b/src/test/py/bazel/bzlmod/bazel_overrides_test.py @@ -14,6 +14,7 @@ # limitations under the License. # pylint: disable=g-long-ternary +import json import os import shutil import tempfile @@ -556,6 +557,110 @@ def testLocalPathOverrideErrorResolved(self): self.ScratchFile('module/MODULE.bazel', ["module(name = 'module')"]) _, _, _ = self.RunBazel(['build', '@module//:all']) + def testInjectRepository(self): + self.ScratchFile( + 'MODULE.bazel', + [ + 'bazel_dep(name="other_module", version="1.0")', + ( + 'local_path_override(module_name="other_module",' + ' path="other_module")' + ), + 'ext = use_extension("//:defs.bzl", "my_ext")', + 'use_repo(ext, "repo")', + ], + ) + + self.ScratchFile( + 'other_module/MODULE.bazel', + ['module(name="other_module", version="1.0")'], + ) + self.ScratchFile('other_repo/REPO.bazel') + self.ScratchFile('other_repo/BUILD', ['filegroup(name="target")']) + + self.ScratchFile( + 'defs.bzl', + [ + 'def _repo_impl(ctx):', + ' ctx.file("BUILD")', + 'my_repo = repository_rule(implementation=_repo_impl)', + 'def _ext_impl(ctx):', + ' my_repo(name = "repo")', + 'my_ext = module_extension(implementation=_ext_impl)', + ], + ) + self.ScratchFile('BUILD') + + self.RunBazel([ + 'build', + '--inject_repository=my_repo=%workspace%/other_repo', + '@my_repo//:target', + ]) + + _, stdout, _ = self.RunBazel([ + 'mod', + 'dump_repo_mapping', + '--inject_repository=my_repo=%workspace%/other_repo', + '', + ]) + main_repo_mapping = json.loads('\n'.join(stdout)) + self.assertEqual(main_repo_mapping["my_repo"], "+_repo_rules+my_repo") + + _, stdout, _ = self.RunBazel([ + 'mod', + 'dump_repo_mapping', + '--inject_repository=my_repo=%workspace%/other_repo', + '+_repo_rules+my_repo', + ]) + my_repo_mapping = json.loads('\n'.join(stdout)) + self.assertEqual(main_repo_mapping, my_repo_mapping) + + def testInjectRepositoryOnExistingRepo(self): + self.ScratchFile( + 'MODULE.bazel', + [ + 'ext = use_extension("//:defs.bzl", "my_ext")', + 'use_repo(ext, my_repo = "repo")', + ], + ) + + self.ScratchFile('other_repo/REPO.bazel') + self.ScratchFile('other_repo/BUILD', ['filegroup(name="target")']) + + self.ScratchFile( + 'defs.bzl', + [ + 'def _repo_impl(ctx):', + ' ctx.file("BUILD")', + 'my_repo = repository_rule(implementation=_repo_impl)', + 'def _ext_impl(ctx):', + ' my_repo(name = "repo")', + 'my_ext = module_extension(implementation=_ext_impl)', + ], + ) + self.ScratchFile('BUILD') + + exit_code, _, stderr = self.RunBazel([ + 'build', + '--inject_repository=my_repo=%workspace%/other_repo', + '//:all', + ], allow_failure=True) + self.AssertNotExitCode(exit_code, 0, stderr) + self.assertIn( + "Error in use_repo: The repo name 'my_repo' is already being used by --inject_repository at ", stderr) + + def testOverrideRepositoryOnNonExistentRepo(self): + self.ScratchFile('other_repo/REPO.bazel') + self.ScratchFile('other_repo/BUILD', ['filegroup(name="target")']) + + exit_code, _, stderr = self.RunBazel([ + 'build', + '--override_repository=my_repo=%workspace%/other_repo', + '@my_repo//:target', + ], allow_failure=True) + self.AssertNotExitCode(exit_code, 0, stderr) + self.assertIn("ERROR: No repository visible as '@my_repo' from main repository", stderr) + if __name__ == '__main__': absltest.main() diff --git a/src/test/shell/integration/aspect_test.sh b/src/test/shell/integration/aspect_test.sh index 7da68e0b86190a..9925bbd1ea3808 100755 --- a/src/test/shell/integration/aspect_test.sh +++ b/src/test/shell/integration/aspect_test.sh @@ -1964,4 +1964,35 @@ EOF assert_nonempty_file 'bazel-bin/test/from_rule_t2' } +# Regression test for https://github.com/bazelbuild/bazel/issues/22691. +function test_aspect_in_non_existent_overridden_repo() { + cat > BUILD.bazel <<'EOF' +genrule( + name = "gen", + outs = ["out.txt"], + cmd = "touch $@", +) +EOF + + mkdir -p foo + touch foo/REPO.bazel + touch foo/BUILD + cat > foo/foo.bzl <<'EOF' +def _foo_aspect_impl(target, ctx): + return [] + +foo_aspect = aspect( + implementation = _foo_aspect_impl, +) +EOF + + # Run the Bazel build command + bazel build --aspects=@@foo//:foo.bzl%foo_aspect \ + --override_repository=foo=%workspace%/foo \ + //:gen &> $TEST_log && fail "Build succeeded" + expect_not_log "FATAL" + expect_log "--override_repository" + expect_log "--inject_repository" +} + run_suite "Tests for aspects"