Skip to content

Commit e8b2790

Browse files
lberkicopybara-github
authored andcommitted
Make Bazel not crash if a runfiles tree is in a runfiles tree.
There was a test for this, but it didn't check for the exit code, only the error message and the message printed on the crash coincidentally contained the expected message. My best understanding is that this was broken in unknown commit. The root cause of why that change was imperfect is probably the odd behavior that if one posts an error to the event handler in ActionExecutionContext, the action doesn't automatically fail. RELNOTES: None. PiperOrigin-RevId: 697564109 Change-Id: I0847bd7f090af44ffd37880b4957755bca511ad1
1 parent fba8603 commit e8b2790

File tree

10 files changed

+287
-177
lines changed

10 files changed

+287
-177
lines changed

src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java

Lines changed: 133 additions & 87 deletions
Large diffs are not rendered by default.

src/main/java/com/google/devtools/build/lib/analysis/RunfilesSupport.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ public PathFragment getExecPath() {
148148
@Override
149149
public Map<PathFragment, Artifact> getMapping() {
150150
if (cachedMapping == null) {
151-
return runfiles.getRunfilesInputs(
152-
/* eventHandler= */ null, /* location= */ null, repoMappingManifest);
151+
return runfiles.getRunfilesInputs(repoMappingManifest);
153152
}
154153

155154
Map<PathFragment, Artifact> result = cachedMapping.get();
@@ -163,9 +162,7 @@ public Map<PathFragment, Artifact> getMapping() {
163162
return result;
164163
}
165164

166-
result =
167-
runfiles.getRunfilesInputs(
168-
/* eventHandler= */ null, /* location= */ null, repoMappingManifest);
165+
result = runfiles.getRunfilesInputs(repoMappingManifest);
169166
cachedMapping = new WeakReference<>(result);
170167
return result;
171168
}

src/main/java/com/google/devtools/build/lib/analysis/SourceManifestAction.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import com.google.devtools.build.lib.actions.ActionOwner;
2828
import com.google.devtools.build.lib.actions.Artifact;
2929
import com.google.devtools.build.lib.actions.ArtifactExpander;
30+
import com.google.devtools.build.lib.actions.ExecException;
31+
import com.google.devtools.build.lib.actions.UserExecException;
32+
import com.google.devtools.build.lib.analysis.Runfiles.ConflictType;
3033
import com.google.devtools.build.lib.analysis.actions.AbstractFileWriteAction;
3134
import com.google.devtools.build.lib.analysis.actions.DeterministicWriter;
3235
import com.google.devtools.build.lib.analysis.starlark.UnresolvedSymlinkAction;
@@ -35,6 +38,9 @@
3538
import com.google.devtools.build.lib.collect.nestedset.Order;
3639
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
3740
import com.google.devtools.build.lib.events.EventHandler;
41+
import com.google.devtools.build.lib.events.StoredEventHandler;
42+
import com.google.devtools.build.lib.server.FailureDetails;
43+
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
3844
import com.google.devtools.build.lib.util.Fingerprint;
3945
import com.google.devtools.build.lib.vfs.PathFragment;
4046
import java.io.BufferedWriter;
@@ -47,6 +53,7 @@
4753
import java.util.Comparator;
4854
import java.util.List;
4955
import java.util.Map;
56+
import java.util.function.BiConsumer;
5057
import javax.annotation.Nullable;
5158

5259
/**
@@ -199,9 +206,7 @@ public synchronized NestedSet<Artifact> getInputs() {
199206
@VisibleForTesting
200207
public void writeOutputFile(OutputStream out, @Nullable EventHandler eventHandler)
201208
throws IOException {
202-
writeFile(
203-
out,
204-
runfiles.getRunfilesInputs(eventHandler, getOwner().getLocation(), repoMappingManifest));
209+
writeFile(out, runfiles.getRunfilesInputs(repoMappingManifest));
205210
}
206211

207212
/**
@@ -222,10 +227,33 @@ public String getStarlarkContent() throws IOException {
222227
}
223228

224229
@Override
225-
public DeterministicWriter newDeterministicWriter(ActionExecutionContext ctx) {
226-
final Map<PathFragment, Artifact> runfilesInputs =
227-
runfiles.getRunfilesInputs(
228-
ctx.getEventHandler(), getOwner().getLocation(), repoMappingManifest);
230+
public DeterministicWriter newDeterministicWriter(ActionExecutionContext ctx)
231+
throws ExecException {
232+
StoredEventHandler eventHandler = new StoredEventHandler();
233+
BiConsumer<ConflictType, String> eventReceiver =
234+
runfiles.eventRunfilesConflictReceiver(eventHandler, getOwner().getLocation());
235+
boolean seenNestedRunfilesTree[] = new boolean[] {false};
236+
BiConsumer<ConflictType, String> receiver =
237+
(conflictType, message) -> {
238+
eventReceiver.accept(conflictType, message);
239+
if (conflictType == ConflictType.NESTED_RUNFILES_TREE) {
240+
seenNestedRunfilesTree[0] = true;
241+
}
242+
};
243+
244+
Map<PathFragment, Artifact> runfilesInputs =
245+
runfiles.getRunfilesInputs(receiver, repoMappingManifest);
246+
eventHandler.replayOn(ctx.getEventHandler());
247+
if (seenNestedRunfilesTree[0]) {
248+
FailureDetail failureDetail =
249+
FailureDetail.newBuilder()
250+
.setMessage("Cannot create input manifest for runfiles tree")
251+
.setAnalysis(
252+
FailureDetails.Analysis.newBuilder()
253+
.setCode(FailureDetails.Analysis.Code.INVALID_RUNFILES_TREE))
254+
.build();
255+
throw new UserExecException(failureDetail);
256+
}
229257
return out -> writeFile(out, runfilesInputs);
230258
}
231259

src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeStrategy.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,7 @@ private static ImmutableMap<PathFragment, PathFragment> getFilesetMap(
143143
private static Map<PathFragment, Artifact> getRunfilesMap(SymlinkTreeAction action) {
144144
// This call outputs warnings about overlapping symlinks. However, since this has already been
145145
// called by the SourceManifestAction, we silence the warnings here.
146-
return action
147-
.getRunfiles()
148-
.getRunfilesInputs(
149-
/* eventHandler= */ null,
150-
action.getOwner().getLocation(),
151-
action.getRepoMappingManifest());
146+
return action.getRunfiles().getRunfilesInputs(action.getRepoMappingManifest());
152147
}
153148

154149
private static void createOutput(

src/main/protobuf/failure_details.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ message Analysis {
12561256
INCOMPATIBLE_TARGET_REQUESTED = 19 [(metadata) = { exit_code: 1 }];
12571257
ANALYSIS_FAILURE_PROPAGATION_FAILED = 20 [(metadata) = { exit_code: 1 }];
12581258
ANALYSIS_CACHE_DISCARDED = 21 [(metadata) = { exit_code: 1 }];
1259+
INVALID_RUNFILES_TREE = 22 [(metadata) = { exit_code: 1 }];
12591260
}
12601261

12611262
Code code = 1;

src/test/java/com/google/devtools/build/lib/analysis/RunfilesRepoMappingManifestTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.devtools.build.lib.vfs.PathFragment;
2929
import java.util.Map.Entry;
3030
import net.starlark.java.eval.EvalException;
31-
import net.starlark.java.syntax.Location;
3231
import org.junit.Before;
3332
import org.junit.Test;
3433
import org.junit.runner.RunWith;
@@ -364,7 +363,7 @@ public void hasMappingForSymlinks() throws Exception {
364363
ImmutableList<String> runfilesPaths =
365364
runfilesSupport
366365
.getRunfiles()
367-
.getRunfilesInputs(reporter, Location.BUILTIN, runfilesSupport.getRepoMappingManifest())
366+
.getRunfilesInputs(runfilesSupport.getRepoMappingManifest())
368367
.keySet()
369368
.stream()
370369
.map(PathFragment::getPathString)

0 commit comments

Comments
 (0)