Skip to content

Commit

Permalink
[8.0.0] Partial fix for bug with creating a junction to file instead …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
bazel-io and AlexanderGolovlev authored Oct 25, 2024
1 parent e672d4f commit c3ad87e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ protected void createSymbolicLink(PathFragment linkPath, PathFragment targetFrag
try {
java.nio.file.Path link = getIoFile(linkPath).toPath();
java.nio.file.Path target = getIoFile(targetPath).toPath();
// Still Create a dangling junction if the target doesn't exist.
if (!target.toFile().exists() || target.toFile().isDirectory()) {
if (target.toFile().isDirectory()) {
WindowsFileOperations.createJunction(link.toString(), target.toString());
} else if (createSymbolicLinks) {
WindowsFileOperations.createSymlink(link.toString(), target.toString());
} else if (!target.toFile().exists()) {
// Still Create a dangling junction if the target doesn't exist.
WindowsFileOperations.createJunction(link.toString(), target.toString());
} else {
if (createSymbolicLinks) {
WindowsFileOperations.createSymlink(link.toString(), target.toString());
} else {
Files.copy(target, link);
}
Files.copy(target, link);
}
} catch (java.nio.file.FileAlreadyExistsException e) {
throw new IOException(linkPath + ERR_FILE_EXISTS, e);
Expand Down
3 changes: 2 additions & 1 deletion src/main/native/windows/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@ int CreateSymlink(const wstring& symlink_name, const wstring& symlink_target,
const wstring target = AddUncPrefixMaybe(symlink_target);

DWORD attrs = GetFileAttributesW(target.c_str());
if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
if ((attrs != INVALID_FILE_ATTRIBUTES) &&
(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
// Instead of creating a symlink to a directory use a Junction.
return CreateSymlinkResult::kTargetIsDirectory;
}
Expand Down

0 comments on commit c3ad87e

Please sign in to comment.