diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java index b313bd8b6380c9..7e4961bd3a8188 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java @@ -1015,13 +1015,20 @@ private void createSymlinks(Iterable symlinks) throws IOExcepti "Failed creating directory and parents for %s", symlink.path()) .createDirectoryAndParents(); - // If a directory output is being materialized as a symlink, we must first delete the - // preexisting empty directory. - if (symlink.path().exists(Symlinks.NOFOLLOW) - && symlink.path().isDirectory(Symlinks.NOFOLLOW)) { + // If a directory output is being materialized as a symlink, creating the symlink fails as we + // must first delete the preexisting empty directory. Since this is rare (and in the future + // BwoB may no longer eagerly create these directories), we don't delete the directory + // beforehand. + try { + symlink.path().createSymbolicLink(symlink.target()); + } catch (IOException e) { + if (!symlink.path().isDirectory(Symlinks.NOFOLLOW)) { + throw e; + } + // Retry after deleting the directory. symlink.path().delete(); + symlink.path().createSymbolicLink(symlink.target()); } - symlink.path().createSymbolicLink(symlink.target()); } }