diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/AsynchronousTreeDeleter.java b/src/main/java/com/google/devtools/build/lib/sandbox/AsynchronousTreeDeleter.java index 70aa26583f5a16..ce809f3ad6c9d4 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/AsynchronousTreeDeleter.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/AsynchronousTreeDeleter.java @@ -110,4 +110,8 @@ public void shutdown() { service = null; } } + + public Path getTrashBase() { + return trashBase; + } } diff --git a/src/main/java/com/google/devtools/build/lib/worker/BUILD b/src/main/java/com/google/devtools/build/lib/worker/BUILD index 3db83d01d93317..54351a17ccada3 100644 --- a/src/main/java/com/google/devtools/build/lib/worker/BUILD +++ b/src/main/java/com/google/devtools/build/lib/worker/BUILD @@ -104,7 +104,6 @@ java_library( "//src/main/java/com/google/devtools/build/lib/exec:execution_options", "//src/main/java/com/google/devtools/build/lib/exec:runfiles_tree_updater", "//src/main/java/com/google/devtools/build/lib/exec:spawn_strategy_registry", - "//src/main/java/com/google/devtools/build/lib/exec:tree_deleter", "//src/main/java/com/google/devtools/build/lib/exec/local", "//src/main/java/com/google/devtools/build/lib/runtime/commands/events", "//src/main/java/com/google/devtools/build/lib/sandbox:cgroups_info", @@ -193,7 +192,7 @@ java_library( ":worker_options", ":worker_process_status", "//src/main/java/com/google/devtools/build/lib/events", - "//src/main/java/com/google/devtools/build/lib/exec:tree_deleter", + "//src/main/java/com/google/devtools/build/lib/sandbox:tree_deleter", "//src/main/java/com/google/devtools/build/lib/vfs", "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment", "//src/main/protobuf:failure_details_java_proto", diff --git a/src/main/java/com/google/devtools/build/lib/worker/WorkerFactory.java b/src/main/java/com/google/devtools/build/lib/worker/WorkerFactory.java index 56d1202c2468a7..4d656821ed7808 100644 --- a/src/main/java/com/google/devtools/build/lib/worker/WorkerFactory.java +++ b/src/main/java/com/google/devtools/build/lib/worker/WorkerFactory.java @@ -17,7 +17,7 @@ import com.google.common.io.BaseEncoding; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.Reporter; -import com.google.devtools.build.lib.exec.TreeDeleter; +import com.google.devtools.build.lib.sandbox.AsynchronousTreeDeleter; import com.google.devtools.build.lib.server.FailureDetails.Worker.Code; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; @@ -49,7 +49,7 @@ public class WorkerFactory extends BaseKeyedPooledObjectFactory module.workerPool.borrowObject(key)); } + @Test + public void buildStarting_cleansStaleTrashDirCleanedOnFirstBuild() throws Exception { + WorkerModule module = new WorkerModule(); + WorkerOptions options = WorkerOptions.DEFAULTS; + + when(request.getOptions(WorkerOptions.class)).thenReturn(options); + setupEnvironment("/outputRoot"); + + module.beforeCommand(env); + Path workerDir = fs.getPath("/outputRoot/outputBase/bazel-workers"); + Path trashBase = workerDir.getChild(AsynchronousTreeDeleter.MOVED_TRASH_DIR); + trashBase.createDirectoryAndParents(); + + Path staleTrash = trashBase.getChild("random-trash"); + + staleTrash.createDirectoryAndParents(); + module.buildStarting(BuildStartingEvent.create(env, request)); + // Trash is cleaned upon first build. + assertThat(staleTrash.exists()).isFalse(); + + staleTrash.createDirectoryAndParents(); + module.buildStarting(BuildStartingEvent.create(env, request)); + // Trash is not cleaned upon subsequent builds. + assertThat(staleTrash.exists()).isTrue(); + } + private void setupEnvironment(String rootDir) throws IOException, AbruptExitException { storedEventHandler = new StoredEventHandler(); Path root = fs.getPath(rootDir);