Skip to content

Commit 2e51bc3

Browse files
committed
Simplify RepositoryDirectoryValue and rename RepositoryCache
As the title suggests, this PR does two things: 1. It refactors `RepositoryDirectoryValue`. - This class used to have a `repositoryExists()` boolean method, and various other methods that throw at runtime depending on whether `repositoryExists()` is true or false. This PR changes it so that `RepositoryDirectoryValue` is a sealed interface with two impls -- `Success` and `Failure`. - The "success" case used to hold on to a digest. This digest is effectively a hash of the marker file, and it's not actually used anywhere. However, removing it causes tests to mysteriously start failing. Upon closer inspection, this is actually because it affects change pruning -- the marking file changing is almost 100% the same as the fetched contents changing. I can't think of a counterexample, but this seems slightly wrong, so I changed `RepositoryDirectoryValue` to never use change pruning instead (by implementing `NotComparableSkyValue`). - Note that this is potentially a subtle behavior change, but should improve correctness, if anything. 2. It renames `RepositoryCache` to `DownloadCache`. This is admittedly a mostly unrelated change... If any reviewer requests it, I'm happy to spend the time splitting this part into a separate PR. - It also adds a new `RepositoryCache` that holds onto the `DownloadCache` and the soon-to-be-introduced `RepoContentsCache`. The latter is just a "readonly" trivial implementation for now. Work towards #12227. Closes #25919. PiperOrigin-RevId: 750740171 Change-Id: Id87180b75b4a856fa0734952105c178b7d41b74e
1 parent b63b6ee commit 2e51bc3

File tree

52 files changed

+841
-960
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+841
-960
lines changed

src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private static class RepositoryCacheInfoItem extends InfoItem {
214214
public byte[] get(
215215
Supplier<BuildConfigurationValue> configurationSupplier, CommandEnvironment env)
216216
throws AbruptExitException, InterruptedException {
217-
return print(repositoryCache.getRootPath());
217+
return print(repositoryCache.getPath());
218218
}
219219
}
220220

@@ -245,7 +245,8 @@ public void workspaceInit(
245245
isFetch,
246246
clientEnvironmentSupplier,
247247
directories,
248-
BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER);
248+
BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER,
249+
repositoryCache.getRepoContentsCache());
249250
singleExtensionEvalFunction =
250251
new SingleExtensionEvalFunction(directories, clientEnvironmentSupplier);
251252

@@ -311,7 +312,10 @@ public void initializeRuleClasses(ConfiguredRuleClassProvider.Builder builder) {
311312
@Override
312313
public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
313314
DownloadManager downloadManager =
314-
new DownloadManager(repositoryCache, env.getDownloaderDelegate(), env.getHttpDownloader());
315+
new DownloadManager(
316+
repositoryCache.getDownloadCache(),
317+
env.getDownloaderDelegate(),
318+
env.getHttpDownloader());
315319
this.starlarkRepositoryFunction.setDownloadManager(downloadManager);
316320
this.moduleFileFunction.setDownloadManager(downloadManager);
317321
this.repoSpecFunction.setDownloadManager(downloadManager);
@@ -339,7 +343,7 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
339343
}
340344
disableNativeRepoRules = repoOptions.disableNativeRepoRules;
341345

342-
repositoryCache.setHardlink(repoOptions.useHardlinks);
346+
repositoryCache.getDownloadCache().setHardlink(repoOptions.useHardlinks);
343347
if (repoOptions.experimentalScaleTimeouts > 0.0) {
344348
starlarkRepositoryFunction.setTimeoutScaling(repoOptions.experimentalScaleTimeouts);
345349
singleExtensionEvalFunction.setTimeoutScaling(repoOptions.experimentalScaleTimeouts);
@@ -365,7 +369,7 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
365369
.getWorkspace()
366370
.getRelative(repoOptions.experimentalRepositoryCache);
367371
}
368-
repositoryCache.setRepositoryCachePath(repositoryCachePath);
372+
repositoryCache.setPath(repositoryCachePath);
369373
} else {
370374
Path repositoryCachePath =
371375
env.getDirectories()
@@ -374,7 +378,7 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
374378
.getRelative(DEFAULT_CACHE_LOCATION);
375379
try {
376380
repositoryCachePath.createDirectoryAndParents();
377-
repositoryCache.setRepositoryCachePath(repositoryCachePath);
381+
repositoryCache.setPath(repositoryCachePath);
378382
} catch (IOException e) {
379383
env.getReporter()
380384
.handle(

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelFetchAllFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedExcept
9898
if (repoDirValue == null) {
9999
return null;
100100
}
101-
if (!repoDirValue.excludeFromVendoring()) {
101+
if (repoDirValue instanceof RepositoryDirectoryValue.Success s && !s.excludeFromVendoring()) {
102102
shouldVendor.add((RepositoryName) repoDelegatorKey.argument());
103103
}
104104
}

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/GsonTypeAdapterUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import com.google.common.collect.ImmutableTable;
2626
import com.google.common.collect.Table;
2727
import com.google.devtools.build.lib.bazel.bzlmod.Version.ParseException;
28-
import com.google.devtools.build.lib.bazel.repository.cache.RepositoryCache;
28+
import com.google.devtools.build.lib.bazel.repository.cache.DownloadCache;
2929
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
3030
import com.google.devtools.build.lib.cmdline.Label;
3131
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
@@ -445,7 +445,7 @@ public Optional<Checksum> read(JsonReader jsonReader) throws IOException {
445445
return Optional.empty();
446446
}
447447
try {
448-
return Optional.of(Checksum.fromString(RepositoryCache.KeyType.SHA256, checksumString));
448+
return Optional.of(Checksum.fromString(DownloadCache.KeyType.SHA256, checksumString));
449449
} catch (Checksum.InvalidChecksumException e) {
450450
throw new JsonParseException(String.format("Invalid checksum: %s", checksumString), e);
451451
}

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileFunction.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@
2828
import com.google.common.collect.Maps;
2929
import com.google.devtools.build.lib.actions.FileValue;
3030
import com.google.devtools.build.lib.bazel.bzlmod.CompiledModuleFile.IncludeStatement;
31+
import com.google.devtools.build.lib.bazel.bzlmod.ModuleExtensionUsage.Proxy;
32+
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileGlobals.ModuleExtensionProxy;
3133
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue.NonRootModuleFileValue;
3234
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue.RootModuleFileValue;
35+
import com.google.devtools.build.lib.bazel.bzlmod.ModuleThreadContext.ModuleExtensionUsageBuilder;
36+
import com.google.devtools.build.lib.bazel.bzlmod.Registry.NotFoundException;
3337
import com.google.devtools.build.lib.bazel.repository.PatchUtil;
3438
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum.MissingChecksumException;
3539
import com.google.devtools.build.lib.bazel.repository.downloader.DownloadManager;
@@ -47,6 +51,7 @@
4751
import com.google.devtools.build.lib.profiler.ProfilerTask;
4852
import com.google.devtools.build.lib.profiler.SilentCloseable;
4953
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
54+
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue.Success;
5055
import com.google.devtools.build.lib.server.FailureDetails.ExternalDeps.Code;
5156
import com.google.devtools.build.lib.skyframe.ClientEnvironmentFunction;
5257
import com.google.devtools.build.lib.skyframe.ClientEnvironmentValue;
@@ -62,6 +67,7 @@
6267
import com.google.devtools.build.lib.vfs.RootedPath;
6368
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
6469
import com.google.devtools.build.skyframe.SkyFunction;
70+
import com.google.devtools.build.skyframe.SkyFunction.Environment.SkyKeyComputeState;
6571
import com.google.devtools.build.skyframe.SkyFunctionException;
6672
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
6773
import com.google.devtools.build.skyframe.SkyKey;
@@ -132,7 +138,7 @@ public ModuleFileFunction(
132138
this.builtinModules = builtinModules;
133139
}
134140

135-
private static class State implements Environment.SkyKeyComputeState {
141+
private static class State implements SkyKeyComputeState {
136142
// The following fields are used during root module file evaluation. We try to compile the root
137143
// module file itself first, and then read, parse, and compile any included module files layer
138144
// by layer, in a BFS fashion (hence the `horizon` field). Finally, everything is collected into
@@ -480,7 +486,7 @@ public static RootModuleFileValue evaluateRootModuleFile(
480486
throw errorf(Code.BAD_MODULE, "error executing MODULE.bazel file for the root module");
481487
}
482488
for (ModuleExtensionUsage usage : module.getExtensionUsages()) {
483-
ModuleExtensionUsage.Proxy firstProxy = usage.getProxies().getFirst();
489+
Proxy firstProxy = usage.getProxies().getFirst();
484490
if (usage.getIsolationKey().isPresent() && firstProxy.getImports().isEmpty()) {
485491
throw errorf(
486492
Code.BAD_MODULE,
@@ -581,16 +587,16 @@ private static void injectRepos(
581587
return;
582588
}
583589
// Use the innate extension backing use_repo_rule.
584-
ModuleThreadContext.ModuleExtensionUsageBuilder usageBuilder =
585-
new ModuleThreadContext.ModuleExtensionUsageBuilder(
590+
ModuleExtensionUsageBuilder usageBuilder =
591+
new ModuleExtensionUsageBuilder(
586592
context,
587593
"//:MODULE.bazel",
588594
"@bazel_tools//tools/build_defs/repo:local.bzl local_repository",
589595
/* isolate= */ false);
590-
ModuleFileGlobals.ModuleExtensionProxy extensionProxy =
591-
new ModuleFileGlobals.ModuleExtensionProxy(
596+
ModuleExtensionProxy extensionProxy =
597+
new ModuleExtensionProxy(
592598
usageBuilder,
593-
ModuleExtensionUsage.Proxy.builder()
599+
Proxy.builder()
594600
.setDevDependency(true)
595601
.setLocation(Location.BUILTIN)
596602
.setContainingModuleFilePath(context.getCurrentModuleFilePath()));
@@ -638,9 +644,12 @@ private GetModuleFileResult getModuleFile(
638644
if (repoDir == null) {
639645
return null;
640646
}
647+
// This repo _definitely_ exists, since it has a non-registry override, which directly gets
648+
// "translated" into a repo spec. So we can cast `repoDir` to `Success`.
649+
Path repoDirPath = ((Success) repoDir).getPath();
641650
RootedPath moduleFilePath =
642651
RootedPath.toRootedPath(
643-
Root.fromPath(repoDir.getPath()), LabelConstants.MODULE_DOT_BAZEL_FILE_NAME);
652+
Root.fromPath(repoDirPath), LabelConstants.MODULE_DOT_BAZEL_FILE_NAME);
644653
if (env.getValue(FileValue.key(moduleFilePath)) == null) {
645654
return null;
646655
}
@@ -706,7 +715,7 @@ private GetModuleFileResult getModuleFile(
706715
try {
707716
originalModuleFile =
708717
registry.getModuleFile(key, downloadEventHandler, this.downloadManager);
709-
} catch (Registry.NotFoundException e) {
718+
} catch (NotFoundException e) {
710719
if (notFoundTrace == null) {
711720
notFoundTrace = new ArrayList<>();
712721
}

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/RegistryFileDownloadEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import com.google.common.collect.ImmutableMap;
1919
import com.google.common.hash.Hashing;
20-
import com.google.devtools.build.lib.bazel.repository.cache.RepositoryCache;
20+
import com.google.devtools.build.lib.bazel.repository.cache.DownloadCache;
2121
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
2222
import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable;
2323
import java.util.Collection;
@@ -44,7 +44,7 @@ static ImmutableMap<String, Optional<Checksum>> collectToMap(Collection<Postable
4444
private static Checksum computeHash(byte[] bytes) {
4545
try {
4646
return Checksum.fromString(
47-
RepositoryCache.KeyType.SHA256, Hashing.sha256().hashBytes(bytes).toString());
47+
DownloadCache.KeyType.SHA256, Hashing.sha256().hashBytes(bytes).toString());
4848
} catch (Checksum.InvalidChecksumException e) {
4949
// This can't happen since HashCode.toString() always returns a valid hash.
5050
throw new IllegalStateException(e);

src/main/java/com/google/devtools/build/lib/bazel/commands/FetchCommand.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.google.devtools.common.options.OptionsParser;
5555
import com.google.devtools.common.options.OptionsParsingResult;
5656
import java.util.List;
57+
import java.util.stream.Stream;
5758
import javax.annotation.Nullable;
5859

5960
/** Fetches external repositories. Which is so fetch. */
@@ -216,8 +217,11 @@ private BlazeCommandResult fetchRepos(
216217

217218
String notFoundRepos =
218219
repositoryNamesAndValues.values().stream()
219-
.filter(value -> !value.repositoryExists())
220-
.map(value -> value.getErrorMsg())
220+
.flatMap(
221+
value ->
222+
value instanceof RepositoryDirectoryValue.Failure failure
223+
? Stream.of(failure.getErrorMsg())
224+
: Stream.of())
221225
.collect(joining("; "));
222226
if (!notFoundRepos.isEmpty()) {
223227
return createFailedBlazeCommandResult(

src/main/java/com/google/devtools/build/lib/bazel/commands/VendorCommand.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import com.google.devtools.build.lib.pkgcache.PackageOptions;
4040
import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction;
4141
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
42+
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue.Failure;
43+
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue.Success;
4244
import com.google.devtools.build.lib.runtime.BlazeCommand;
4345
import com.google.devtools.build.lib.runtime.BlazeCommandResult;
4446
import com.google.devtools.build.lib.runtime.Command;
@@ -257,12 +259,13 @@ private BlazeCommandResult vendorRepos(
257259
List<String> notFoundRepoErrors = new ArrayList<>();
258260
for (Entry<RepositoryName, RepositoryDirectoryValue> entry :
259261
repositoryNamesAndValues.entrySet()) {
260-
if (entry.getValue().repositoryExists()) {
261-
if (!entry.getValue().excludeFromVendoring()) {
262-
reposToVendor.add(entry.getKey());
262+
switch (entry.getValue()) {
263+
case Success s -> {
264+
if (!s.excludeFromVendoring()) {
265+
reposToVendor.add(entry.getKey());
266+
}
263267
}
264-
} else {
265-
notFoundRepoErrors.add(entry.getValue().getErrorMsg());
268+
case Failure f -> notFoundRepoErrors.add(f.getErrorMsg());
266269
}
267270
}
268271

@@ -314,8 +317,7 @@ private ImmutableSet<RepositoryName> collectReposFromTargets(
314317
SkyKey key = nodes.remove();
315318
visited.add(key);
316319
NodeEntry nodeEntry = inMemoryGraph.get(null, Reason.VENDOR_EXTERNAL_REPOS, key);
317-
if (nodeEntry.getValue() instanceof RepositoryDirectoryValue repoDirValue
318-
&& repoDirValue.repositoryExists()
320+
if (nodeEntry.getValue() instanceof RepositoryDirectoryValue.Success repoDirValue
319321
&& !repoDirValue.excludeFromVendoring()) {
320322
repos.add((RepositoryName) key.argument());
321323
}

src/main/java/com/google/devtools/build/lib/bazel/repository/CacheHitReportingModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package com.google.devtools.build.lib.bazel.repository;
1515

1616
import com.google.common.eventbus.Subscribe;
17-
import com.google.devtools.build.lib.bazel.repository.cache.RepositoryCacheHitEvent;
17+
import com.google.devtools.build.lib.bazel.repository.cache.DownloadCacheHitEvent;
1818
import com.google.devtools.build.lib.events.Event;
1919
import com.google.devtools.build.lib.events.Reporter;
2020
import com.google.devtools.build.lib.repository.RepositoryFailedEvent;
@@ -47,7 +47,7 @@ public void afterCommand() {
4747
}
4848

4949
@Subscribe
50-
public synchronized void cacheHit(RepositoryCacheHitEvent event) {
50+
public synchronized void cacheHit(DownloadCacheHitEvent event) {
5151
cacheHitsByContext
5252
.computeIfAbsent(event.getContext(), k -> new HashSet<>())
5353
.add(Pair.of(event.getFileHash(), event.getUrl()));

src/main/java/com/google/devtools/build/lib/bazel/repository/LocalConfigPlatformFunction.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.devtools.build.lib.bazel.ResolvedEvent;
2121
import com.google.devtools.build.lib.packages.Rule;
2222
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
23-
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
2423
import com.google.devtools.build.lib.rules.repository.RepositoryFunction;
2524
import com.google.devtools.build.lib.rules.repository.ResolvedFileValue;
2625
import com.google.devtools.build.lib.skyframe.PrecomputedValue;
@@ -97,8 +96,7 @@ public Object getResolvedInformation(XattrProvider xattrProvider) {
9796
});
9897

9998
// Return the needed info.
100-
return new FetchResult(
101-
RepositoryDirectoryValue.builder().setPath(outputDirectory), ImmutableMap.of());
99+
return new FetchResult(ImmutableMap.of());
102100
}
103101

104102
private static String workspaceFileContent(String repositoryName) {

src/main/java/com/google/devtools/build/lib/bazel/repository/cache/BUILD

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ filegroup(
1515
# Main Java code for Bazel
1616
java_library(
1717
name = "cache",
18-
srcs = ["RepositoryCache.java"],
18+
srcs = [
19+
"DownloadCache.java",
20+
"RepoContentsCache.java",
21+
"RepositoryCache.java",
22+
],
1923
deps = [
2024
"//src/main/java/com/google/devtools/build/lib/vfs",
2125
"//src/main/java/com/google/devtools/build/lib/vfs/bazel",
@@ -26,9 +30,6 @@ java_library(
2630

2731
java_library(
2832
name = "events",
29-
srcs = ["RepositoryCacheHitEvent.java"],
30-
deps = [
31-
"//src/main/java/com/google/devtools/build/lib/cmdline",
32-
"//src/main/java/com/google/devtools/build/lib/events",
33-
],
33+
srcs = ["DownloadCacheHitEvent.java"],
34+
deps = ["//src/main/java/com/google/devtools/build/lib/events"],
3435
)

0 commit comments

Comments
 (0)