Skip to content

Commit

Permalink
Fix file hashes being deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 21, 2024
1 parent 03a42ab commit 32e04db
Showing 1 changed file with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Comparator.comparing;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
Expand Down Expand Up @@ -83,6 +82,27 @@ public void doAfterCommand() {
return;
}

BazelLockFileValue oldLockfile = moduleResolutionEvent.getOnDiskLockfileValue();

ImmutableMap<String, Optional<Checksum>> fileHashes;
if (downloadEvents.isEmpty()) {
// BazelDepGraphFunction took the dep graph from the lockfile and didn't cause evaluation of
// BazelModuleResolutionFunction. The file hashes in the lockfile are still up-to-date.
fileHashes = oldLockfile.getFileHashes();
} else {
// BazelModuleResolutionFunction ran and emitted events with the up-to-date list of accessed
// registry files.
fileHashes =
downloadEvents.stream()
.collect(
toImmutableMap(
RegistryFileDownloadEvent::getUri,
RegistryFileDownloadEvent::getChecksum,
// This should never be called since we take care to download the same file
// only once per build.
BazelLockFileModule::failOnDifferentChecksums));
}

// All nodes corresponding to module extensions that have been evaluated in the current build
// are done at this point. Look up entries by unvalidated keys to record results even if
// validation later fails due to invalid imports.
Expand All @@ -94,26 +114,15 @@ public void doAfterCommand() {
entry -> ((SingleExtensionEvalValue.UnvalidatedKey) entry.getKey()).argument(),
entry ->
((SingleExtensionEvalValue) entry.getValue()).getLockFileInfo().get()));
var combinedExtensionInfos =
combineModuleExtensions(oldLockfile.getModuleExtensions(), newExtensionInfos);

BazelLockFileValue oldLockfile = moduleResolutionEvent.getOnDiskLockfileValue();
// Create an updated version of the lockfile, keeping only the extension results from the old
// lockfile that are still up-to-date and adding the newly resolved extension results.
BazelLockFileValue newLockfile =
moduleResolutionEvent.getResolutionOnlyLockfileValue().toBuilder()
.setModuleExtensions(
combineModuleExtensions(oldLockfile.getModuleExtensions(), newExtensionInfos))
.setFileHashes(
// We ensure that the same file is not downloaded multiple times and can thus
// collect without worrying about duplicates.
downloadEvents.stream()
.sorted(comparing(RegistryFileDownloadEvent::getUri))
.collect(
toImmutableMap(
RegistryFileDownloadEvent::getUri,
RegistryFileDownloadEvent::getChecksum,
// This should never be called since we take care to download the same
// file only once per build.
BazelLockFileModule::failOnDifferentChecksums)))
.setModuleExtensions(combinedExtensionInfos)
.setFileHashes(fileHashes)
.build();

// Write the new value to the file, but only if needed. This is not just a performance
Expand Down

0 comments on commit 32e04db

Please sign in to comment.