Skip to content

Commit

Permalink
Rename variables and clean up BazelLockFileModule
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Apr 21, 2024
1 parent 090beef commit 928c399
Showing 1 changed file with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ public void afterCommand() throws AbruptExitException {
return;
}

BazelLockFileValue oldLockfile = moduleResolutionEvent.getOnDiskLockfileValue();

// 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.
var extensionLockFileInfos =
var newExtensionInfos =
evaluator.getDoneValues().entrySet().stream()
.filter(entry -> entry.getKey() instanceof SingleExtensionEvalValue.UnvalidatedKey)
.collect(
Expand All @@ -80,22 +82,13 @@ public void afterCommand() throws AbruptExitException {
entry ->
((SingleExtensionEvalValue) entry.getValue()).getLockFileInfo().get()));

BazelLockFileValue oldLockfile = moduleResolutionEvent.getOnDiskLockfileValue();
BazelLockFileValue newLockfile;
try {
// 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.
newLockfile =
moduleResolutionEvent.getResolutionOnlyLockfileValue().toBuilder()
.setModuleExtensions(combineModuleExtensions(oldLockfile, extensionLockFileInfos))
.build();
} catch (ExternalDepsException e) {
logger.atSevere().withCause(e).log(
"Failed to read and parse the MODULE.bazel.lock file with error: %s."
+ " Try deleting it and rerun the build.",
e.getMessage());
return;
}
// 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))
.build();

// Write the new value to the file, but only if needed. This is not just a performance
// optimization: whenever the lockfile is updated, most Skyframe nodes will be marked as dirty
Expand All @@ -114,14 +107,16 @@ public void afterCommand() throws AbruptExitException {
private ImmutableMap<
ModuleExtensionId, ImmutableMap<ModuleExtensionEvalFactors, LockFileModuleExtension>>
combineModuleExtensions(
BazelLockFileValue oldLockfile,
Map<ModuleExtensionId, LockFileModuleExtension.WithFactors> extensionLockFileInfos)
throws ExternalDepsException {
ImmutableMap<
ModuleExtensionId,
ImmutableMap<ModuleExtensionEvalFactors, LockFileModuleExtension>>
oldExtensionInfos,
Map<ModuleExtensionId, LockFileModuleExtension.WithFactors> newExtensionInfos) {
Map<ModuleExtensionId, ImmutableMap<ModuleExtensionEvalFactors, LockFileModuleExtension>>
updatedExtensionMap = new HashMap<>();

// Keep old extensions if they are still valid.
for (var entry : oldLockfile.getModuleExtensions().entrySet()) {
for (var entry : oldExtensionInfos.entrySet()) {
var moduleExtensionId = entry.getKey();
var factorToLockedExtension = entry.getValue();
ModuleExtensionEvalFactors firstEntryFactors =
Expand All @@ -134,13 +129,13 @@ public void afterCommand() throws AbruptExitException {
moduleExtensionId,
firstEntryFactors,
firstEntryExtension.getUsagesDigest(),
extensionLockFileInfos.get(moduleExtensionId))) {
newExtensionInfos.get(moduleExtensionId))) {
updatedExtensionMap.put(moduleExtensionId, factorToLockedExtension);
}
}

// Add the new resolved extensions
for (var extensionIdAndInfo : extensionLockFileInfos.entrySet()) {
for (var extensionIdAndInfo : newExtensionInfos.entrySet()) {
LockFileModuleExtension extension = extensionIdAndInfo.getValue().moduleExtension();
if (!extension.shouldLockExtension()) {
continue;
Expand Down Expand Up @@ -186,17 +181,17 @@ private boolean shouldKeepExtension(
ModuleExtensionId extensionId,
ModuleExtensionEvalFactors lockedExtensionKey,
byte[] oldUsagesDigest,
@Nullable LockFileModuleExtension.WithFactors extensionInfo) {
@Nullable LockFileModuleExtension.WithFactors newExtensionInfo) {

// If there is a new event for this extension, compare it with the existing ones
if (extensionInfo != null) {
boolean doNotLockExtension = !extensionInfo.moduleExtension().shouldLockExtension();
if (newExtensionInfo != null) {
boolean doNotLockExtension = !newExtensionInfo.moduleExtension().shouldLockExtension();
boolean dependencyOnOsChanged =
lockedExtensionKey.getOs().isEmpty()
!= extensionInfo.extensionFactors().getOs().isEmpty();
!= newExtensionInfo.extensionFactors().getOs().isEmpty();
boolean dependencyOnArchChanged =
lockedExtensionKey.getArch().isEmpty()
!= extensionInfo.extensionFactors().getArch().isEmpty();
!= newExtensionInfo.extensionFactors().getArch().isEmpty();
if (doNotLockExtension || dependencyOnOsChanged || dependencyOnArchChanged) {
return false;
}
Expand Down

0 comments on commit 928c399

Please sign in to comment.