Skip to content

Commit

Permalink
Allow --override_repository to add repos with --noenable_workspace
Browse files Browse the repository at this point in the history
RELNOTES: `--override_repository` can now be used to add new repos even with `--noenable_workspace`. The new repo will be visible to the main repo and sees the same repos as the main repo.
  • Loading branch information
fmeum committed Sep 27, 2024
1 parent 56882d5 commit 711ef17
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -603,7 +604,7 @@ public ImmutableList<Injected> getPrecomputedValues() {
lastRegistryInvalidation = now;
}
return ImmutableList.of(
PrecomputedValue.injected(RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, overrides),
PrecomputedValue.injected(RepositoryMappingFunction.REPOSITORY_OVERRIDES, overrides),
PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, moduleOverrides),
PrecomputedValue.injected(
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE,
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.devtools.build.lib.skyframe.AlreadyReportedException;
import com.google.devtools.build.lib.skyframe.PrecomputedValue;
import com.google.devtools.build.lib.skyframe.PrecomputedValue.Precomputed;
import com.google.devtools.build.lib.skyframe.RepositoryMappingFunction;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
Expand Down Expand Up @@ -71,8 +72,6 @@
* this function.
*/
public final class RepositoryDelegatorFunction implements SkyFunction {
public static final Precomputed<Map<RepositoryName, PathFragment>> REPOSITORY_OVERRIDES =
new Precomputed<>("repository_overrides");

public static final String FORCE_FETCH_DISABLED = "";

Expand Down Expand Up @@ -156,7 +155,8 @@ public SkyValue compute(SkyKey skyKey, Environment env)
Path repoRoot =
RepositoryFunction.getExternalRepositoryDirectory(directories)
.getRelative(repositoryName.getName());
Map<RepositoryName, PathFragment> overrides = REPOSITORY_OVERRIDES.get(env);
Map<RepositoryName, PathFragment> overrides =
RepositoryMappingFunction.REPOSITORY_OVERRIDES.get(env);
if (Preconditions.checkNotNull(overrides).containsKey(repositoryName)) {
return setupOverride(overrides.get(repositoryName), env, repoRoot, repositoryName);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package com.google.devtools.build.lib.skyframe;

import static java.util.stream.Collectors.toMap;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.bzlmod.BazelDepGraphValue;
import com.google.devtools.build.lib.bazel.bzlmod.Module;
Expand All @@ -28,19 +30,22 @@
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;
import com.google.devtools.build.skyframe.SkyValue;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.Set;
import javax.annotation.Nullable;
import net.starlark.java.eval.StarlarkSemantics;

/** {@link SkyFunction} for {@link RepositoryMappingValue}s. */
public class RepositoryMappingFunction implements SkyFunction {
public static final PrecomputedValue.Precomputed<Map<RepositoryName, PathFragment>>
REPOSITORY_OVERRIDES = new PrecomputedValue.Precomputed<>("repository_overrides");
private final RuleClassProvider ruleClassProvider;

public RepositoryMappingFunction(RuleClassProvider ruleClassProvider) {
Expand All @@ -58,6 +63,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
RepositoryName repositoryName = ((RepositoryMappingValue.Key) skyKey).repoName();
boolean enableBzlmod = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD);
boolean enableWorkspace = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_WORKSPACE);
Set<RepositoryName> repositoryOverrides = REPOSITORY_OVERRIDES.get(env).keySet();

if (!enableBzlmod && !enableWorkspace) {
throw new RepositoryMappingFunctionException(
Expand Down Expand Up @@ -126,9 +132,9 @@ public SkyValue compute(SkyKey skyKey, Environment env)
entry.getValue().getAssociatedRule() != null
&& !entry.getValue().getAssociatedRule().getRuleClass().equals("bind"))
.collect(
Collectors.toMap(
toMap(
Entry::getKey, entry -> RepositoryName.createUnvalidated(entry.getKey())));
return computeForBazelModuleRepo(repositoryName, bazelDepGraphValue)
return computeForBazelModuleRepo(repositoryName, bazelDepGraphValue, repositoryOverrides)
.get()
// For the transitional period, we need to map the workspace name to the main repo.
.withAdditionalMappings(
Expand All @@ -140,7 +146,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)

// Try and see if this is a repo generated from a Bazel module.
Optional<RepositoryMappingValue> mappingValue =
computeForBazelModuleRepo(repositoryName, bazelDepGraphValue);
computeForBazelModuleRepo(repositoryName, bazelDepGraphValue, repositoryOverrides);
if (mappingValue.isPresent()) {
return repositoryName.isMain()
? mappingValue.get().withCachedInverseMap()
Expand All @@ -163,6 +169,15 @@ public SkyValue compute(SkyKey skyKey, Environment env)
repoMappingEntriesValue.getModuleKey().name(),
repoMappingEntriesValue.getModuleKey().version());
}

if (!enableWorkspace) {
// If a repo is overridden without a previous definition, it uses the main repo's mapping.
if (REPOSITORY_OVERRIDES.get(env).containsKey(repositoryName)) {
return computeForBazelModuleRepo(
RepositoryName.MAIN, bazelDepGraphValue, repositoryOverrides)
.get();
}
}
}

if (enableWorkspace) {
Expand Down Expand Up @@ -196,17 +211,23 @@ public SkyValue compute(SkyKey skyKey, Environment env)
* Optional.empty().
*/
private Optional<RepositoryMappingValue> computeForBazelModuleRepo(
RepositoryName repositoryName, BazelDepGraphValue bazelDepGraphValue) {
RepositoryName repositoryName,
BazelDepGraphValue bazelDepGraphValue,
Set<RepositoryName> repositoryOverrides) {
ModuleKey moduleKey = bazelDepGraphValue.getCanonicalRepoNameLookup().get(repositoryName);
if (moduleKey == null) {
return Optional.empty();
}
RepositoryMapping repoMapping = bazelDepGraphValue.getFullRepoMapping(moduleKey);
if (repositoryName.isMain()) {
repoMapping =
repoMapping.withAdditionalMappings(
repositoryOverrides.stream().collect(toMap(RepositoryName::getName, name -> name)));
}
Module module = bazelDepGraphValue.getDepGraph().get(moduleKey);
return Optional.of(
RepositoryMappingValue.createForBzlmodRepo(
bazelDepGraphValue.getFullRepoMapping(moduleKey),
module.getName(),
module.getVersion()));
repoMapping, module.getName(), module.getVersion()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -194,7 +195,7 @@ public ImmutableList<PrecomputedValue.Injected> 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -244,7 +245,7 @@ protected void useRuleClassProvider(ConfiguredRuleClassProvider ruleClassProvide
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),
Expand Down Expand Up @@ -289,7 +290,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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -222,7 +223,7 @@ private void setUpWithBuiltinModules(ImmutableMap<String, NonRegistryOverride> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void setup() throws Exception {
PrecomputedValue.STARLARK_SEMANTICS.set(differencer, semantics);
PrecomputedValue.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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,7 +202,7 @@ private void setUpWithBuiltinModules(ImmutableMap<String, NonRegistryOverride> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 711ef17

Please sign in to comment.