Skip to content

Commit 7d87996

Browse files
oquenchilcopybara-github
authored andcommitted
Fix bug in reuse_sandbox_directories
#19935 gives a clear repro. The problem appears when a stashed sandbox contains a directory whose same path is a regular file in a later execution. If we try to reuse the stashed sandbox we trigger an error if the old directory contained any files. This was due to simply calling delete() without checking first if it was a directory. The fix is to call deleteTree() instead in those cases. directory. Fixes #19935 RELNOTES:none PiperOrigin-RevId: 576889004 Change-Id: I73b145cd574b83c473ffaccd90b675eb5f086c0e
1 parent 0a9c9a7 commit 7d87996

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/java/com/google/devtools/build/lib/sandbox/SandboxHelpers.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ private static void cleanRecursively(
184184
if (SYMLINK.equals(dirent.getType())
185185
&& absPath.readSymbolicLink().equals(destination.get())) {
186186
inputsToCreate.remove(pathRelativeToWorkDir);
187+
} else if (absPath.isDirectory()) {
188+
absPath.deleteTree();
187189
} else {
188190
absPath.delete();
189191
}

src/test/java/com/google/devtools/build/lib/sandbox/SandboxHelpersTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,19 @@ public void cleanExisting_updatesDirs() throws IOException, InterruptedException
293293
// outputDir only exists partially
294294
execRootPath.getRelative(outputDir).getParentDirectory().createDirectoryAndParents();
295295
execRootPath.getRelative("justSomeDir/thatIsDoomed").createDirectoryAndParents();
296-
SandboxHelpers.cleanExisting(rootDir, inputs, inputsToCreate, dirsToCreate, execRootPath);
296+
// `thiswillbeafile/output` simulates a directory that was in the stashed dir but whose same
297+
// path is used later for a regular file.
298+
scratch.dir("/execRoot/thiswillbeafile/output");
299+
scratch.file("/execRoot/thiswillbeafile/output/file1");
300+
dirsToCreate.add(PathFragment.create("thiswillbeafile"));
301+
PathFragment input4 = PathFragment.create("thiswillbeafile/output");
302+
SandboxInputs inputs2 =
303+
new SandboxInputs(
304+
ImmutableMap.of(input1, inputTxt, input2, inputTxt, input3, inputTxt, input4, inputTxt),
305+
ImmutableMap.of(),
306+
ImmutableMap.of(),
307+
ImmutableMap.of());
308+
SandboxHelpers.cleanExisting(rootDir, inputs2, inputsToCreate, dirsToCreate, execRootPath);
297309
assertThat(dirsToCreate).containsExactly(inputDir2, inputDir3, outputDir);
298310
assertThat(execRootPath.getRelative("existing/directory/with").exists()).isTrue();
299311
assertThat(execRootPath.getRelative("partial").exists()).isTrue();

0 commit comments

Comments
 (0)