Skip to content

Commit c3ad87e

Browse files
[8.0.0] Partial fix for bug with creating a junction to file instead of symlink (#24058)
This is a partial fix for issue #21747 for the `--windows_enable_symlinks` case. The fix was suggested in discussion of this issue. This resolves the problem with creating a junction if the target path doesn't exist for those who have Windows symlinks enabled, until a complete solution is provided. Closes #24051. PiperOrigin-RevId: 688724224 Change-Id: Ie44f7834af5fd35ab57961e6012b9f336c25d606 Commit 3e5514d Co-authored-by: Alexander Golovlev <golovlev@gmail.com>
1 parent e672d4f commit c3ad87e

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

src/main/java/com/google/devtools/build/lib/windows/WindowsFileSystem.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ protected void createSymbolicLink(PathFragment linkPath, PathFragment targetFrag
7979
try {
8080
java.nio.file.Path link = getIoFile(linkPath).toPath();
8181
java.nio.file.Path target = getIoFile(targetPath).toPath();
82-
// Still Create a dangling junction if the target doesn't exist.
83-
if (!target.toFile().exists() || target.toFile().isDirectory()) {
82+
if (target.toFile().isDirectory()) {
83+
WindowsFileOperations.createJunction(link.toString(), target.toString());
84+
} else if (createSymbolicLinks) {
85+
WindowsFileOperations.createSymlink(link.toString(), target.toString());
86+
} else if (!target.toFile().exists()) {
87+
// Still Create a dangling junction if the target doesn't exist.
8488
WindowsFileOperations.createJunction(link.toString(), target.toString());
8589
} else {
86-
if (createSymbolicLinks) {
87-
WindowsFileOperations.createSymlink(link.toString(), target.toString());
88-
} else {
89-
Files.copy(target, link);
90-
}
90+
Files.copy(target, link);
9191
}
9292
} catch (java.nio.file.FileAlreadyExistsException e) {
9393
throw new IOException(linkPath + ERR_FILE_EXISTS, e);

src/main/native/windows/file.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,8 @@ int CreateSymlink(const wstring& symlink_name, const wstring& symlink_target,
505505
const wstring target = AddUncPrefixMaybe(symlink_target);
506506

507507
DWORD attrs = GetFileAttributesW(target.c_str());
508-
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
508+
if ((attrs != INVALID_FILE_ATTRIBUTES) &&
509+
(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
509510
// Instead of creating a symlink to a directory use a Junction.
510511
return CreateSymlinkResult::kTargetIsDirectory;
511512
}

0 commit comments

Comments
 (0)