diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java index ecab25ebe61bfa..f152ec74cd9a75 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelLockFileModule.java @@ -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( @@ -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 @@ -114,14 +107,16 @@ public void afterCommand() throws AbruptExitException { private ImmutableMap< ModuleExtensionId, ImmutableMap> combineModuleExtensions( - BazelLockFileValue oldLockfile, - Map extensionLockFileInfos) - throws ExternalDepsException { + ImmutableMap< + ModuleExtensionId, + ImmutableMap> + oldExtensionInfos, + Map newExtensionInfos) { Map> 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 = @@ -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; @@ -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; }