Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Windows developer mode symlinks #13629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/main/native/windows/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ using std::wstring;
DWORD DetermineSymlinkPrivilegeFlag() {
DWORD val = 0;
DWORD valSize = sizeof(val);
if ( // The unprivileged create flag was introduced in Windows 10 build
// 14972:
// https://blogs.windows.com/windowsdeveloper/2016/12/02/symlinks-windows-10/
!IsWindowsVersionOrGreater(10, 0, 14972)
// Check if developer mode is disabled:
|| RegGetValueW(
// Check if developer mode is disabled
if (RegGetValueW(
HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock",
L"AllowDevelopmentWithoutDevLicense", RRF_RT_DWORD, nullptr, &val,
Expand Down Expand Up @@ -467,6 +463,13 @@ int CreateSymlink(const wstring& symlink_name, const wstring& symlink_target,

if (!CreateSymbolicLinkW(name.c_str(), target.c_str(),
symlinkPrivilegeFlag)) {
if (GetLastError() == ERROR_INVALID_PARAMETER) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This means on older Windows versions with developer mode enabled, each symlink operation triggers CreateSymlinkLink twice, which has some performance implication. But I guess it's small issue which won't impact the vast majority of Windows users.

// We are on a version of Windows that does not support this flag.
// Retry without the flag and return to error handling if necessary.
if (CreateSymbolicLinkW(name.c_str(), target.c_str(), 0)) {
return CreateSymlinkResult::kSuccess;
}
}
*error = MakeErrorMessage(
WSTR(__FILE__), __LINE__, L"CreateSymlink", symlink_target,
GetLastError() == ERROR_PRIVILEGE_NOT_HELD
Expand Down
5 changes: 2 additions & 3 deletions src/main/native/windows/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ bool IsDeveloperModeEnabled();
DWORD DetermineSymlinkPrivilegeFlag();

// The flag SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE requires
// developer mode to be enabled. If it is not enabled, or the current
// version of Windows does not support it, do not use the flag.
// The process will need to be run with elevated privileges.
// developer mode to be enabled. If it is not enabled, do not use the
// flag. The process will need to be run with elevated privileges.
const DWORD symlinkPrivilegeFlag = DetermineSymlinkPrivilegeFlag();

template <typename char_type>
Expand Down
7 changes: 7 additions & 0 deletions src/main/tools/build-runfiles-windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ class RunfilesCreator {
if (!CreateSymbolicLinkW(
it.first.c_str(), it.second.c_str(),
bazel::windows::symlinkPrivilegeFlag | create_dir)) {
if (GetLastError() == ERROR_INVALID_PARAMETER) {
// We are on a version of Windows that does not support this flag.
// Retry without the flag and return to error handling if necessary.
if (CreateSymbolicLinkW(it.first.c_str(), it.second.c_str(), create_dir)) {
return;
}
}
if (GetLastError() == ERROR_PRIVILEGE_NOT_HELD) {
die(L"CreateSymbolicLinkW failed:\n%hs\n",
"Bazel needs to create symlinks to build the runfiles tree.\n"
Expand Down