From 8195f27a661846311eae8412f8b8b3f0f0c2d368 Mon Sep 17 00:00:00 2001 From: "John W. Parent" <45471568+johnwparent@users.noreply.github.com> Date: Fri, 17 Mar 2023 13:19:32 -0400 Subject: Windows: properly handle symlink failures (#36003) In the Windows filesystem logic for creating a symlink, we intend to fall back to a copy when the symlink cannot be created (for some configuration settings on Windows it is not possible for the user to create a symlink). It turns out we were overly-broad in which exceptions lead to this fallback, and the subsequent copy would also fail: at least one case where this occurred is when we attempted to create a symlink that already existed. The updated logic expressly avoids falling back to a copy when the file/symlink already exists. --- lib/spack/llnl/util/filesystem.py | 2 +- lib/spack/llnl/util/symlink.py | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py index 889791d310..8af2f28342 100644 --- a/lib/spack/llnl/util/filesystem.py +++ b/lib/spack/llnl/util/filesystem.py @@ -2407,7 +2407,7 @@ class WindowsSimulatedRPath(object): # For py2 compatibility, we have to catch the specific Windows error code # associate with trying to create a file that already exists (winerror 183) except OSError as e: - if sys.platform == "win32" and e.winerror == 183: + if sys.platform == "win32" and (e.winerror == 183 or e.errno == errno.EEXIST): # We have either already symlinked or we are encoutering a naming clash # either way, we don't want to overwrite existing libraries already_linked = islink(dest_file) diff --git a/lib/spack/llnl/util/symlink.py b/lib/spack/llnl/util/symlink.py index 10616adfe8..69aacaf9f0 100644 --- a/lib/spack/llnl/util/symlink.py +++ b/lib/spack/llnl/util/symlink.py @@ -30,9 +30,15 @@ def symlink(real_path, link_path): try: # Try to use junctions _win32_junction(real_path, link_path) - except OSError: - # If all else fails, fall back to copying files - shutil.copyfile(real_path, link_path) + except OSError as e: + if e.errno == errno.EEXIST: + # EEXIST error indicates that file we're trying to "link" + # is already present, don't bother trying to copy which will also fail + # just raise + raise + else: + # If all else fails, fall back to copying files + shutil.copyfile(real_path, link_path) def islink(path): -- cgit v1.2.3-60-g2f50