fix(windows): don't require symlinks for the runtime venv (#4509) On Windows, `py_binary` and `py_test` create a venv at runtime by symlinking the interpreter, its `.dll` files and the `site-packages` files into a temporary directory. Without the privilege to create symlinks, this fails with `WinError 1314`, which is the default for users that can't enable Developer Mode. On Windows, if creating a symlink fails, the bootstrap now uses hard links for files, junctions for directories, and copies if those aren't possible, e.g. when the temporary directory is on a different volume. The runfiles files are already copies when Bazel can't create symlinks, so they aren't moved, as that would break the next run that uses the same runfiles tree. Other platforms are unchanged. Windows refuses to delete read-only files, such as hard links to Bazel outputs, and the read-only attribute is shared by all the hard links of a file, so the venv's read-only files are deleted with `FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE` instead of clearing the attribute. This needs Windows 10 1809 or Windows Server 2019, so older versions still require the privilege to create symlinks. The `RULES_PYTHON_TESTING_DISABLE_SYMLINKS` environment variable forces the fallback on Windows, which a new test uses, and a new CI job uses to build and test `examples/bzlmod` with `--nowindows_enable_symlinks`. Fixes #3792 --------- Co-authored-by: Richard Levasseur <richardlev@gmail.com>
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml index 2f7bdd9..c5ccd9b 100644 --- a/.bazelci/presubmit.yml +++ b/.bazelci/presubmit.yml
@@ -394,6 +394,28 @@ - "bazel build --vendor_dir=./vendor //..." - "rm -rf ./vendor" + integration_test_bzlmod_windows_no_symlinks: + <<: *reusable_build_test_all + name: "examples/bzlmod: Windows, without symlinks" + working_directory: examples/bzlmod + platform: windows + bazel: 8.x + batch_commands: + # The last value wins, so this overrides the setting in .bazelrc to + # check that binaries work without the privilege to create symlinks. + - "echo startup --nowindows_enable_symlinks >> .bazelrc" + build_flags: &windows_no_symlinks_flags + ? "--keep_going" + # The CI user may be able to create symlinks, so force the fallback. + ? "--action_env=RULES_PYTHON_TESTING_DISABLE_SYMLINKS=1" + # Without symlinks, Bazel creates a junction instead of a copy in the + # runfiles tree for an output that isn't downloaded from the remote + # cache yet, and a junction to a file doesn't work. + ? "--remote_download_all" + # Repeat in build_flags so `bazel test` reuses the build's analysis cache. + ? "--test_env=RULES_PYTHON_TESTING_DISABLE_SYMLINKS=1" + test_flags: *windows_no_symlinks_flags + integration_test_multi_python_versions_ubuntu_workspace: <<: *reusable_build_test_all <<: *common_workspace_flags
diff --git a/news/3792.fixed.md b/news/3792.fixed.md new file mode 100644 index 0000000..5a7dfa3 --- /dev/null +++ b/news/3792.fixed.md
@@ -0,0 +1,5 @@ +(binaries/tests) (Windows) {obj}`py_binary` and {obj}`py_test` no longer need +the privilege to create symlinks to run on Windows 10 1809, Windows Server +2019 or later. If creating a symlink fails, the venv that is created at runtime +uses hard links, junctions or copies instead +([#3792](https://github.com/bazel-contrib/rules_python/issues/3792)).
diff --git a/python/private/python_bootstrap_template.txt b/python/private/python_bootstrap_template.txt index 8d95f9d..72bf62d 100644 --- a/python/private/python_bootstrap_template.txt +++ b/python/private/python_bootstrap_template.txt
@@ -116,6 +116,19 @@ BIN_DIR_NAME = "bin" if not IS_WINDOWS else "Scripts" LIB_DIR_NAME = "lib" if not IS_WINDOWS else "Lib" +# Windows only: set to False once creating a symlink fails, e.g. without the +# privilege to create symlinks, so that it isn't attempted for every file. +# Tests set the environment variable to use the fallback even when symlinks +# can be created. +_symlinks_supported = not ( + IS_WINDOWS and os.environ.get("RULES_PYTHON_TESTING_DISABLE_SYMLINKS") +) + +# The build of Windows 10 1809 and Windows Server 2019, which added +# FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE that the fallback for when +# symlinks can't be created needs to clean up. +_WINDOWS_FALLBACK_MIN_BUILD = 17763 + # Windows APIs can be picky about slashes depending on the context, # so convert to backslashes to avoid any issues. if IS_WINDOWS: @@ -441,11 +454,10 @@ link_to = normpath(join(runfiles_root, link_to_rf_path)) venv_parent = dirname(venv_abs_path) # A cached venv can leave this parent as a dangling link to an old EXTRACT_ROOT. - if os.path.islink(venv_parent): - try: - os.stat(venv_parent) - except FileNotFoundError: - os.unlink(venv_parent) + # islink() is False for a junction on Windows, but lexists() doesn't + # follow it, and unlink() removes it. + if os.path.lexists(venv_parent) and not os.path.exists(venv_parent): + os.unlink(venv_parent) os.makedirs(venv_parent, exist_ok=True) _symlink_exist_ok(from_=venv_abs_path, to=link_to) @@ -599,7 +611,7 @@ if delete_dirs: for delete_dir in delete_dirs: print_verbose("cleanup: rmtree:", delete_dir) - shutil.rmtree(delete_dir, True) + _rmtree(delete_dir) sys.exit(ret_code) def _run_execv(python_program, argv, env): @@ -617,13 +629,151 @@ raise def _symlink_exist_ok(*, from_, to): + """Creates a symlink, or on Windows, a hard link, junction or copy if that's not possible.""" + global _symlinks_supported + # On Windows, symlinks have to be told whether they're + # pointing to a file or directory. Python is supposed to auto-detect + # this, but in practice, this doesn't reliably happen. + is_dir = os.path.isdir(to) + if _symlinks_supported: + try: + os.symlink(to, from_, target_is_directory=is_dir) + return + except FileExistsError: + return + except OSError as e: + if not IS_WINDOWS: + raise + if sys.getwindowsversion().build < _WINDOWS_FALLBACK_MIN_BUILD: + raise OSError( + "Creating symlinks failed, which is required before Windows 10 " + "1809 and Windows Server 2019: {}".format(e) + ) from e + print_verbose("symlink failed, falling back to links or copies:", e) + _symlinks_supported = False + + if os.path.lexists(from_): + return + # The target can itself be a symlink, e.g. when Bazel could create the + # runfiles symlinks. A hard link to a symlink is also a symlink, so link to + # the file or directory that it points to instead. + to = os.path.realpath(to) + if is_dir: + _link_dir_windows(from_, to) + else: + _link_file_windows(from_, to) + +def _link_file_windows(from_, to): + # Hard links don't need any privileges, but only work within the same + # volume, so fall back to copying the file. try: - # On Windows, symlinks have to be told whether they're - # pointing to a file or directory. Python is supposed to auto-detect - # this, but in practice, this doesn't reliably happen. - os.symlink(to, from_, target_is_directory=os.path.isdir(to)) + os.link(to, from_) + # Another process sharing RULES_PYTHON_EXTRACT_ROOT created it first. except FileExistsError: - pass + return + except OSError as e: + print_verbose("hard link failed, copying:", to, "error:", e) + shutil.copy2(to, from_) + +def _link_dir_windows(from_, to): + import _winapi + try: + # Junctions don't need any privileges, but need an absolute target. + _winapi.CreateJunction(to, from_) + # Another process sharing RULES_PYTHON_EXTRACT_ROOT created it first. + except FileExistsError: + return + except OSError as e: + print_verbose("junction failed, copying:", to, "error:", e) + # This can still fail with FileExistsError if another process sharing + # RULES_PYTHON_EXTRACT_ROOT creates the directory while copying. Both + # creating the junction and a concurrent run have to fail for that, so + # this isn't handled. + shutil.copytree(to, from_) + +_kernel32 = None + +def _get_kernel32(): + """Returns kernel32 with the prototypes that cleaning up the venv needs.""" + global _kernel32 + if _kernel32 is not None: + return _kernel32 + + import ctypes + from ctypes import wintypes + + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) + kernel32.CreateFileW.restype = wintypes.HANDLE + kernel32.CreateFileW.argtypes = [ + wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID, + wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE, + ] + kernel32.SetFileInformationByHandle.restype = wintypes.BOOL + kernel32.SetFileInformationByHandle.argtypes = [ + wintypes.HANDLE, ctypes.c_int, wintypes.LPVOID, wintypes.DWORD, + ] + kernel32.CloseHandle.restype = wintypes.BOOL + kernel32.CloseHandle.argtypes = [wintypes.HANDLE] + _kernel32 = kernel32 + return kernel32 + +def _remove_read_only_file_windows(path): + """Removes a read-only file without changing its read-only attribute. + + Windows refuses to delete a read-only file, such as a hard link to or a + copy of a Bazel output. Clearing the attribute would also clear it for the + other hard links of the file, i.e. the Bazel output, so the file is deleted + with FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE instead. + """ + import ctypes + from ctypes import wintypes + + kernel32 = _get_kernel32() + + DELETE = 0x00010000 + FILE_SHARE_READ_WRITE_DELETE = 0x7 + OPEN_EXISTING = 3 + FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000 + FILE_DISPOSITION_INFO_EX = 21 + FILE_DISPOSITION_FLAG_DELETE = 0x1 + FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE = 0x10 + INVALID_HANDLE_VALUE = wintypes.HANDLE(-1).value + + handle = kernel32.CreateFileW( + path, DELETE, FILE_SHARE_READ_WRITE_DELETE, None, OPEN_EXISTING, + FILE_FLAG_OPEN_REPARSE_POINT, None, + ) + if handle == INVALID_HANDLE_VALUE: + raise ctypes.WinError(ctypes.get_last_error()) + try: + flags = wintypes.DWORD( + FILE_DISPOSITION_FLAG_DELETE | FILE_DISPOSITION_FLAG_IGNORE_READONLY_ATTRIBUTE + ) + if not kernel32.SetFileInformationByHandle( + handle, FILE_DISPOSITION_INFO_EX, ctypes.byref(flags), ctypes.sizeof(flags) + ): + raise ctypes.WinError(ctypes.get_last_error()) + finally: + # The file is deleted when the handle is closed. + kernel32.CloseHandle(handle) + +def _rmtree(path): + if not IS_WINDOWS: + shutil.rmtree(path, True) + return + + def _on_error(func, error_path, _): + if func not in (os.unlink, os.remove): + return + try: + _remove_read_only_file_windows(error_path) + except OSError as e: + print_verbose("cleanup: failed to remove:", error_path, "error:", e) + + if sys.version_info >= (3, 12): + shutil.rmtree(path, onexc=_on_error) + else: + shutil.rmtree(path, onerror=_on_error) def main():
diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index eda0e73..f2cd3a1 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel
@@ -74,6 +74,15 @@ ) sh_py_run_test( + name = "run_binary_without_symlinks_test", + bootstrap_impl = "system_python", + py_src = "bin.py", + sh_src = "run_binary_without_symlinks_test.sh", + # The fallback for when symlinks can't be created is Windows only. + target_compatible_with = ["@platforms//os:windows"], +) + +sh_py_run_test( name = "run_binary_find_runfiles_test", py_src = "bin.py", sh_src = "run_binary_find_runfiles_test.sh",
diff --git a/tests/bootstrap_impls/bin.py b/tests/bootstrap_impls/bin.py index 7ebe0c0..5913d92 100644 --- a/tests/bootstrap_impls/bin.py +++ b/tests/bootstrap_impls/bin.py
@@ -37,4 +37,6 @@ print("sys.flags.safe_path:", sys.flags.safe_path) print("file:", __file__) print("sys.executable:", sys.executable) +print("sys.executable is symlink:", os.path.islink(sys.executable)) +print("sys.prefix:", sys.prefix) print("sys._base_executable:", getattr(sys, "_base_executable", None))
diff --git a/tests/bootstrap_impls/cached_venv_dangling_symlink_test.py b/tests/bootstrap_impls/cached_venv_dangling_symlink_test.py index fc59b23..363736d 100644 --- a/tests/bootstrap_impls/cached_venv_dangling_symlink_test.py +++ b/tests/bootstrap_impls/cached_venv_dangling_symlink_test.py
@@ -43,17 +43,55 @@ class CachedVenvDanglingSymlinkTest(unittest.TestCase): def test_replaces_dangling_package_link_before_creating_child_link(self) -> None: + self._test_replaces_dangling_package_link(symlinks_supported=True) + + @unittest.skipUnless(os.name == "nt", "junctions are Windows only") + def test_replaces_dangling_package_junction_before_creating_child_link( + self, + ) -> None: + self._test_replaces_dangling_package_link(symlinks_supported=False) + + @unittest.skipUnless(os.name == "nt", "junctions are Windows only") + def test_dangling_junction_is_detected_and_removed(self) -> None: + # The bootstrap relies on these to remove a dangling junction, which + # islink() doesn't detect. + import _winapi + + with tempfile.TemporaryDirectory() as temp: + root = pathlib.Path(temp) + target = root / "target" + target.mkdir() + junction = root / "junction" + _winapi.CreateJunction(str(target), str(junction)) # pyrefly: ignore[missing-attribute] + target.rmdir() + + self.assertFalse(os.path.islink(junction)) + self.assertTrue(os.path.lexists(junction)) + self.assertFalse(os.path.exists(junction)) + os.unlink(junction) + self.assertFalse(os.path.lexists(junction)) + + def _test_replaces_dangling_package_link(self, *, symlinks_supported) -> None: bootstrap = _load_bootstrap() + # False uses the fallback for when symlinks can't be created, which + # creates a junction for a directory. + bootstrap["_symlinks_supported"] = symlinks_supported with tempfile.TemporaryDirectory() as temp: root = pathlib.Path(temp) runfiles = root / "runfiles" old_package = runfiles / "old/annotated_types" old_package.mkdir(parents=True) (old_package / "__init__.py").touch() + # The venv isn't run, and Windows can't delete a hard link to the + # running interpreter, e.g. when cleaning up the temporary + # directory, so link to a stand-in instead. + python = root / "python/python.exe" + python.parent.mkdir() + python.touch() bootstrap.update( EXTRACT_ROOT=str(root / "cache"), PYTHON_BINARY="_main/_process.venv/Scripts/python.exe", - PYTHON_BINARY_ACTUAL=sys.executable, + PYTHON_BINARY_ACTUAL=str(python), RESOLVE_PYTHON_BINARY_AT_RUNTIME=False, RUNTIME_VENV_SYMLINKS={ "Lib/site-packages/annotated_types": "old/annotated_types", @@ -78,7 +116,9 @@ package_init = package / "__init__.py" self.assertTrue(package.is_dir()) self.assertFalse(package.is_symlink()) - self.assertTrue(package_init.is_symlink()) + self.assertFalse(getattr(os.path, "isjunction", lambda _: False)(package)) + # The fallback creates a hard link for a file. + self.assertEqual(package_init.is_symlink(), symlinks_supported) self.assertTrue(os.path.samefile(package_init, new_package / "__init__.py"))
diff --git a/tests/bootstrap_impls/run_binary_without_symlinks_test.sh b/tests/bootstrap_impls/run_binary_without_symlinks_test.sh new file mode 100755 index 0000000..fc8fea9 --- /dev/null +++ b/tests/bootstrap_impls/run_binary_without_symlinks_test.sh
@@ -0,0 +1,79 @@ +#!/usr/bin/env bash +# --- begin runfiles.bash initialization v3 --- +# Copy-pasted from the Bazel Bash runfiles library v3. +set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v3 --- +set +e + +bin=$(rlocation "$BIN_RLOCATION") +if [[ -z "$bin" ]]; then + echo "Unable to locate test binary: $BIN_RLOCATION" + exit 1 +fi + +# Checks that the output matches a pattern. +# +# Modifies: +# Creates EXPECTATION_FAILED in the working directory on mismatch. +function expect_match() { + local expected_pattern=$1 + local actual=$2 + if ! (echo "$actual" | grep "$expected_pattern" ) >/dev/null; then + echo "expected to match: $expected_pattern" + echo "===== actual START =====" + echo "$actual" + echo "===== actual END =====" + echo + touch EXPECTATION_FAILED + return 1 + fi +} + +# Converts a Windows path to a Unix one under MSYS, so that bash can use it. +function to_unix() { + cygpath --unix "$1" +} + +# Use hard links, junctions or copies instead of symlinks for the venv that +# is created at runtime, like without the privilege to create symlinks. +# tr: remove the CR of the CRLF line endings of the output on Windows. +venvs_root=$(mktemp --directory "$(to_unix "$TEST_TMPDIR")/venvs_root.XXXXXX") +actual=$(RULES_PYTHON_TESTING_DISABLE_SYMLINKS=1 RULES_PYTHON_EXTRACT_ROOT="$venvs_root" "$bin" | tr --delete '\r') +expect_match "Hello" "$actual" +# Match the name only, because the path is a Windows path on Windows. +expect_match "sys.executable:.*$(basename "$venvs_root")" "$actual" +expect_match "sys.executable is symlink: False" "$actual" + +# Without RULES_PYTHON_EXTRACT_ROOT, the venv is removed after the run, which +# must work for read-only Bazel outputs that are hard linked or copied, and must +# not make the hard linked outputs writable. +runfiles=$(to_unix "${RUNFILES_DIR:-$TEST_SRCDIR}") +# -L: follow the runfiles symlinks to the Bazel outputs. +read_only_before=$(find -L "$runfiles" -type f ! -writable | sort) +if [[ -z "$read_only_before" ]]; then + echo "expected read-only files in the runfiles: $runfiles" + touch EXPECTATION_FAILED +fi +actual=$(RULES_PYTHON_TESTING_DISABLE_SYMLINKS=1 "$bin" | tr --delete '\r') +expect_match "Hello" "$actual" +expect_match "sys.executable is symlink: False" "$actual" +venv=$(to_unix "$(echo "$actual" | grep "^sys.prefix:" | cut --delimiter=' ' --fields=2-)") +if [[ -z "$venv" || -e "$venv" ]]; then + echo "expected the venv to be removed: $venv" + touch EXPECTATION_FAILED +fi +read_only_after=$(find -L "$runfiles" -type f ! -writable | sort) +if [[ "$read_only_before" != "$read_only_after" ]]; then + echo "expected the read-only runfiles to be unchanged" + diff <(echo "$read_only_before") <(echo "$read_only_after") + touch EXPECTATION_FAILED +fi + +# Exit if any of the expects failed +[[ ! -e EXPECTATION_FAILED ]]