diff --git a/src/main/java/com/google/devtools/build/lib/remote/BazelOutputService.java b/src/main/java/com/google/devtools/build/lib/remote/BazelOutputService.java index 412d9951002d9f..ab3642bdbd38d7 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/BazelOutputService.java +++ b/src/main/java/com/google/devtools/build/lib/remote/BazelOutputService.java @@ -86,6 +86,7 @@ public class BazelOutputService implements OutputService { private final boolean verboseFailures; private final RemoteRetrier retrier; private final ReferenceCountedChannel channel; + @Nullable private final String lastBuildId; @Nullable private String buildId; @Nullable private PathFragment outputPathTarget; @@ -100,7 +101,8 @@ public BazelOutputService( String remoteOutputServiceOutputPathPrefix, boolean verboseFailures, RemoteRetrier retrier, - ReferenceCountedChannel channel) { + ReferenceCountedChannel channel, + @Nullable String lastBuildId) { this.outputBaseId = DigestUtil.hashCodeToString(md5().hashString(outputBase.toString(), UTF_8)); this.execRootSupplier = execRootSupplier; this.outputPathSupplier = outputPathSupplier; @@ -111,6 +113,7 @@ public BazelOutputService( this.verboseFailures = verboseFailures; this.retrier = retrier; this.channel = channel; + this.lastBuildId = lastBuildId; } public void shutdown() { @@ -244,7 +247,12 @@ public ModifiedFileSet startBuild( outputPathTarget = constructOutputPathTarget(outputPathPrefix, response); prepareOutputPath(outputPath, outputPathTarget); - if (finalizeActions) { + if (finalizeActions && response.hasInitialOutputPathContents()) { + var initialOutputPathContents = response.getInitialOutputPathContents(); + if (!initialOutputPathContents.getBuildId().equals(lastBuildId)) { + return ModifiedFileSet.EVERYTHING_DELETED; + } + // TODO(chiwang): Handle StartBuildResponse.initial_output_path_contents } diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java index c1bee6d374ad53..2157b2ac576fad 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java @@ -138,6 +138,7 @@ public final class RemoteModule extends BlazeModule { @Nullable private TempPathGenerator tempPathGenerator; @Nullable private BlockWaitingModule blockWaitingModule; @Nullable private RemoteOutputChecker remoteOutputChecker; + @Nullable private String lastBuildId; private ChannelFactory channelFactory = new ChannelFactory() { @@ -473,7 +474,8 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException { remoteOptions.remoteOutputServiceOutputPathPrefix, verboseFailures, retrier, - bazelOutputServiceChannel); + bazelOutputServiceChannel, + lastBuildId); } else { outputService = new RemoteOutputService(env); } @@ -919,6 +921,8 @@ public void afterCommand() { () -> afterCommandTask(actionContextProviderRef, tempPathGeneratorRef, rpcLogFileRef)); } + lastBuildId = Preconditions.checkNotNull(env).getCommandId().toString(); + buildEventArtifactUploaderFactoryDelegate.reset(); repositoryRemoteExecutorFactoryDelegate.reset(); remoteDownloaderSupplier.set(null);