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 0bf7783731e00e..f7bab91d859b66 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; @@ -477,6 +479,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) { @@ -611,7 +631,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/ModuleFileFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunction.java index 69d6099f96d17b..048e000acba67e 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 @@ -77,11 +77,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 @@ -96,6 +98,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; @@ -196,6 +200,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. @@ -293,6 +298,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(), @@ -426,6 +432,7 @@ public static RootModuleFileValue evaluateRootModuleFile( ImmutableMap includeLabelToCompiledModuleFile, ImmutableMap builtinModules, Map commandOverrides, + Map injectedRepositories, boolean ignoreDevDeps, StarlarkSemantics starlarkSemantics, ExtendedEventHandler eventHandler, @@ -438,6 +445,7 @@ public static RootModuleFileValue evaluateRootModuleFile( ModuleKey.ROOT, ignoreDevDeps, builtinModules, + injectedRepositories, /* printIsNoop= */ false, starlarkSemantics, eventHandler, @@ -501,6 +509,7 @@ private static ModuleThreadContext execModuleFile( ModuleKey moduleKey, boolean ignoreDevDeps, ImmutableMap builtinModules, + Map injectedRepositories, boolean printIsNoop, StarlarkSemantics starlarkSemantics, ExtendedEventHandler eventHandler, @@ -530,6 +539,8 @@ private static ModuleThreadContext execModuleFile( } } }); + + injectRepos(injectedRepositories, context, thread); compiledRootModuleFile.runOnThread(thread); } catch (EvalException e) { eventHandler.handle(Event.error(e.getInnermostLocation(), e.getMessageWithStack())); @@ -538,6 +549,48 @@ 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 { + if (injectedRepositories.isEmpty()) { + return; + } + // 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..bbda0c4d04a18b 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 @@ -38,7 +38,6 @@ java_library( "//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/repository:repository_events", - "//src/main/java/com/google/devtools/build/lib/rules:repository/repo_recorded_input", "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_directory_value", "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function", "//src/main/java/com/google/devtools/build/lib/rules:repository/resolved_file_value", @@ -48,7 +47,6 @@ java_library( "//src/main/java/com/google/devtools/build/lib/util", "//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/skyframe", "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects", "//src/main/java/com/google/devtools/common/options", "//src/main/java/net/starlark/java/eval", @@ -72,7 +70,6 @@ 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", - "//third_party:auto_value", - "//third_party:guava", + "//src/main/java/net/starlark/java/eval", ], ) 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..65a00c993c86c8 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 injections.") + 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,14 @@ public String getTypeDescription() { } /** A repository override, represented by a name and an absolute path to a repository. */ - @AutoValue - public abstract static class RepositoryOverride { - - private static RepositoryOverride create(RepositoryName repositoryName, String path) { - return new AutoValue_RepositoryOptions_RepositoryOverride(repositoryName, path); - } - - public abstract RepositoryName repositoryName(); + public record RepositoryOverride(RepositoryName repositoryName, String path) {} - public abstract String path(); - } + /** + * A repository injected into the scope of the root module, represented by a name and an absolute + * path to a repository. + */ + 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 820379071b0644..202cae413a4d0a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/BUILD +++ b/src/main/java/com/google/devtools/build/lib/rules/BUILD @@ -412,6 +412,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 b8d012d3f20e06..82708f06b66ae4 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/BUILD +++ b/src/main/java/com/google/devtools/build/lib/skyframe/BUILD @@ -2375,6 +2375,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..798aa181bb4554 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,30 @@ 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 +131,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 a3ebbc70cdab2b..f18bcb146dbb8d 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 @@ -104,10 +104,10 @@ 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", - "//src/main/java/com/google/devtools/build/skyframe", "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects", "//third_party:error_prone_annotations", "//third_party:guava", 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 b29736d1137658..953768ae9a0ffc 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,13 +104,14 @@ 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()), PrecomputedValue.injected( RepositoryDelegatorFunction.FORCE_FETCH, RepositoryDelegatorFunction.FORCE_FETCH_DISABLED), + PrecomputedValue.injected(ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()), PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), PrecomputedValue.injected( RepositoryDelegatorFunction.FORCE_FETCH_CONFIGURE, 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 5b76a5f2b529a8..d3f13c594f80bb 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 @@ -110,6 +110,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/BUILD b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD index 8cc932adc1862d..975296e346515b 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD @@ -42,7 +42,6 @@ java_library( "//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:repo_rule_value", "//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution", "//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution_impl", - "//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:root_module_file_fixup", "//src/main/java/com/google/devtools/build/lib/bazel/repository:repository_options", "//src/main/java/com/google/devtools/build/lib/bazel/repository/cache", "//src/main/java/com/google/devtools/build/lib/bazel/repository/downloader", @@ -90,7 +89,6 @@ java_library( "//third_party:guava", "//third_party:jsr305", "//third_party:junit4", - "//third_party:mockito", "//third_party:truth", ], ) 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 218b298eecaa9f..50d5bf956fa5ba 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 @@ -149,6 +149,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 1724097f80abdd..cb08c6fb554c89 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 @@ -49,6 +49,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; @@ -172,6 +173,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); @@ -181,7 +183,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 01b8e604f76fc8..47a1fb36037fd3 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 @@ -153,6 +153,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 15a5fb4e2b572c..7aaafc60f50afa 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 @@ -51,6 +51,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; @@ -222,7 +223,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()); @@ -232,6 +233,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); @@ -345,6 +347,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 a654793e825797..a3307d835b490c 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 @@ -267,7 +267,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()); @@ -275,6 +275,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())); @@ -648,6 +649,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 f3dbf619d3f080..32b6104e380fcc 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 @@ -58,6 +58,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; @@ -197,7 +198,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()); @@ -207,6 +208,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); @@ -348,6 +350,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/packages/BUILD b/src/test/java/com/google/devtools/build/lib/packages/BUILD index ee1f907c050710..417d225b5da074 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/BUILD +++ b/src/test/java/com/google/devtools/build/lib/packages/BUILD @@ -173,6 +173,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/rules/cpp", "//src/main/java/com/google/devtools/build/lib/rules/proto", "//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/packages:PackageFactoryBuilderWithSkyframeForTesting", diff --git a/src/test/java/com/google/devtools/build/lib/packages/util/PackageLoadingTestCase.java b/src/test/java/com/google/devtools/build/lib/packages/util/PackageLoadingTestCase.java index 7626e8bcc48c8e..8408a75b62e5ff 100644 --- a/src/test/java/com/google/devtools/build/lib/packages/util/PackageLoadingTestCase.java +++ b/src/test/java/com/google/devtools/build/lib/packages/util/PackageLoadingTestCase.java @@ -43,6 +43,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.SkyFunctions; import com.google.devtools.build.lib.skyframe.SkyframeExecutor; import com.google.devtools.build.lib.testutil.FoundationTestCase; @@ -219,6 +220,14 @@ private SkyframeExecutor createSkyframeExecutor() { ImmutableList.of( PrecomputedValue.injected( RepositoryDelegatorFunction.VENDOR_DIRECTORY, Optional.empty()))); + skyframeExecutor.injectExtraPrecomputedValues( + ImmutableList.of( + PrecomputedValue.injected( + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()))); + skyframeExecutor.injectExtraPrecomputedValues( + ImmutableList.of( + PrecomputedValue.injected( + ModuleFileFunction.INJECTED_REPOSITORIES, ImmutableMap.of()))); SkyframeExecutorTestHelper.process(skyframeExecutor); return skyframeExecutor; } diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/BUILD b/src/test/java/com/google/devtools/build/lib/pkgcache/BUILD index 45abef67abcd75..448fee21d91a44 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/BUILD +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/BUILD @@ -114,6 +114,7 @@ java_library( "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function", "//src/main/java/com/google/devtools/build/lib/skyframe:diff_awareness", "//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/util:abrupt_exit_exception", diff --git a/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java b/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java index cb9727c019d20d..c570402c2fc583 100644 --- a/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java +++ b/src/test/java/com/google/devtools/build/lib/pkgcache/IncrementalLoadingTest.java @@ -45,6 +45,7 @@ import com.google.devtools.build.lib.skyframe.BazelSkyframeExecutorConstants; import com.google.devtools.build.lib.skyframe.DiffAwareness; import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction; import com.google.devtools.build.lib.skyframe.SequencedSkyframeExecutor; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.skyframe.SkyframeExecutor; @@ -516,7 +517,9 @@ def _impl(ctx): PrecomputedValue.injected( RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), PrecomputedValue.injected( - RepositoryDelegatorFunction.VENDOR_DIRECTORY, Optional.empty()))); + RepositoryDelegatorFunction.VENDOR_DIRECTORY, Optional.empty()), + PrecomputedValue.injected( + RepositoryMappingFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()))); BuildLanguageOptions buildLanguageOptions = Options.getDefaults(BuildLanguageOptions.class); buildLanguageOptions.incompatibleAutoloadExternally = ImmutableList.of(); skyframeExecutor.preparePackageLoading( 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 e47e3075563d76..cde07637df252e 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 @@ -46,8 +46,8 @@ java_library( "//src/main/java/com/google/devtools/build/lib/query2/common:universe-scope", "//src/main/java/com/google/devtools/build/lib/query2/engine", "//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function", - "//src/main/java/com/google/devtools/build/lib/skyframe:ignored_subdirectories_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..73e08a107d5445 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,8 @@ 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 +434,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 +444,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/BUILD b/src/test/java/com/google/devtools/build/lib/rules/repository/BUILD index 60d60e289df566..114dd31fbc6bc2 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/repository/BUILD +++ b/src/test/java/com/google/devtools/build/lib/rules/repository/BUILD @@ -52,7 +52,6 @@ java_library( "//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/repository", "//src/main/java/com/google/devtools/build/lib/util/io", "//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/skyframe", "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects", "//src/main/java/net/starlark/java/eval", 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 ddd838f69ad328..79573d0b5a2a82 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 @@ -266,7 +266,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( @@ -281,6 +281,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( @@ -292,7 +293,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 cf11b66803e501..0790337578485a 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", @@ -142,6 +143,7 @@ java_library( "//src/test/java/com/google/devtools/build/lib/testutil:JunitUtils", "//src/test/java/com/google/devtools/build/lib/testutil:TestConstants", "//src/test/java/com/google/devtools/build/lib/testutil:TestPackageFactoryBuilderFactory", + "//third_party:error_prone_annotations", "//third_party:guava", "//third_party:junit4", "//third_party:truth", @@ -1084,6 +1086,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", @@ -1176,6 +1179,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", @@ -1444,6 +1448,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 778a7c87b36dda..dd5028fb1998af 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 @@ -177,7 +177,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 68207a52698c77..21a8a4aaff4a27 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 14fe985adec049..d49b1c120237ca 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 df143390df6529..08f29432c503c9 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,122 @@ 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..fa67a878ed0694 100755 --- a/src/test/shell/integration/aspect_test.sh +++ b/src/test/shell/integration/aspect_test.sh @@ -1964,4 +1964,38 @@ 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() { + if [[ "$PRODUCT_NAME" != "bazel" ]]; then + return 0 + fi + 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"