Skip to content

fix long path issue on Windows#10794

Merged
radoering merged 1 commit intopython-poetry:mainfrom
radoering:fix-windows-long-path-issue
Mar 29, 2026
Merged

fix long path issue on Windows#10794
radoering merged 1 commit intopython-poetry:mainfrom
radoering:fix-windows-long-path-issue

Conversation

@radoering
Copy link
Copy Markdown
Member

@radoering radoering commented Mar 29, 2026

Pull Request Check List

#10792 introduced an issue with long paths on Windows that causes sporadic test failures that look like:

[68](https://github.com/radoering/poetry/actions/runs/23705665774/job/69057056580#step:5:6069)
E           Attempting to write py.test.exe outside of the target directory
E           Target directory: C:\Users\runneradmin\AppData\Local\Temp\pytest-of-runneradmin\pytest-0\popen-gw0\test_execute_prints_warning_fo1\.venv\scripts
E           Target path: \\?\C:\Users\runneradmin\AppData\Local\Temp\pytest-of-runneradmin\pytest-0\popen-gw0\test_execute_prints_warning_fo1\.venv\scripts\py.test.exe

This change works around this issue. To be consistent, I replaced all uses of Path.is_relative_to() with the method that works around the issue.

  • Added tests for changed code.
  • Updated documentation for changed code.

Summary by Sourcery

Add a cross-platform path relativity helper and adopt it to avoid Windows long-path issues.

Bug Fixes:

  • Prevent incorrect path relativity checks on Windows when long-path or UNC prefixes are involved, avoiding erroneous writes outside target directories and misclassification of environments.

Enhancements:

  • Introduce a shared is_relative_to() helper in the compatibility module and use it across path-related checks in installation and environment management code.
  • Include more detailed error information when attempting to write outside the wheel installation target directory.

Tests:

  • Add unit tests for the is_relative_to() helper covering POSIX paths, Windows drive-letter paths, and Windows long-path and UNC-prefixed paths.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 29, 2026

Reviewer's Guide

Introduces a compatibility helper for Path.is_relative_to that normalizes Windows long path prefixes and replaces direct Path.is_relative_to calls with this helper across the codebase, adding tests to verify behavior on POSIX and Windows paths (including long-path and UNC variants) and improving error diagnostics when wheel files attempt to write outside their installation target directory.

Sequence diagram for wheel_installer.write_to_fs path safety check with is_relative_to

sequenceDiagram
    participant WheelInstaller
    participant SchemeDict as scheme_dict
    participant Compat as _compat
    participant TargetDir as target_dir
    participant TargetPath as target_path

    WheelInstaller->>SchemeDict: lookup scheme
    SchemeDict-->>WheelInstaller: scheme_path
    WheelInstaller->>TargetDir: Path(scheme_path).resolve()
    WheelInstaller->>TargetPath: (target_dir / path).resolve()
    WheelInstaller->>Compat: is_relative_to(target_path, target_dir)
    Compat-->>WheelInstaller: is_relative
    alt target_path not relative to target_dir
        WheelInstaller-->>WheelInstaller: raise ValueError with path, target_dir, target_path
    else target_path inside target_dir
        WheelInstaller-->>WheelInstaller: proceed to write file
    end
Loading

File-Level Changes

Change Details Files
Add cross-platform is_relative_to helper that works around Windows long-path prefix issues and unit tests covering its behavior.
  • Introduce is_relative_to(path1, path2) in the compatibility module to normalize and strip Windows long path and UNC prefixes before delegating to Path.is_relative_to
  • Implement Windows-specific prefix handling using string checks and reconstruction of normal and UNC paths
  • Add parameterized tests for is_relative_to on relative, absolute POSIX, and Windows paths, including cases with and without long-path/UNC prefixes and platform-specific skipping.
src/poetry/utils/_compat.py
tests/utils/test_compat.py
Replace direct uses of Path.is_relative_to with the new helper to avoid Windows long-path issues and slightly improve diagnostics for wheel installation.
  • Update wheel_installer.write_to_fs to use is_relative_to(target_path, target_dir) instead of Path.is_relative_to and expand the ValueError message with target directory and path for easier debugging
  • Update env remove command to use is_relative_to when checking whether virtualenv paths are under the project directory
  • Update python list command to use is_relative_to to determine if a Python installation is managed by Poetry based on its path
  • Update Python manager to skip interpreters located in the active virtualenv using is_relative_to instead of Path.is_relative_to.
src/poetry/installation/wheel_installer.py
src/poetry/console/commands/env/remove.py
src/poetry/console/commands/python/list.py
src/poetry/utils/env/python/manager.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="tests/utils/test_compat.py" line_range="12-26" />
<code_context>
+from poetry.utils._compat import is_relative_to
+
+
+@pytest.mark.parametrize(
+    ("path1", "path2", "expected"),
+    [
+        ("a", "a", True),
+        ("a/b", "a/b", True),
+        ("a/b", "a", True),
+        ("a", "a/b", False),
+        ("a/b/c/d", "a/b", True),
+        ("a/b", "a/b/c/d", False),
+    ],
+)
+def test_is_relative_to(path1: str, path2: str, expected: bool) -> None:
+    assert is_relative_to(Path(path1), Path(path2)) is expected
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Add Windows tests for paths on different drives / different UNC servers to avoid false positives

Current Windows parametrization only covers cases where `path1` and `path2` are on the same drive/server. To guard against false positives from the prefix-stripping logic, please add negative cases where roots differ, e.g.:

- `path1=r"C:\a\b"`, `path2=r"D:\a"``False`
- `path1=r"\\?\C:\a\b"`, `path2=r"D:\"``False`
- `path1=r"\\?\UNC\server1\a\b"`, `path2=r"\\?\UNC\server2\a"``False`
- `path1=r"\\server1\a\b"`, `path2=r"\\?\UNC\server2\a"``False`

These will help ensure unrelated roots are never treated as parents after prefix removal.

```suggestion
@pytest.mark.parametrize(
    ("path1", "path2", "expected"),
    [
        ("a", "a", True),
        ("a/b", "a/b", True),
        ("a/b", "a", True),
        ("a", "a/b", False),
        ("a/b/c/d", "a/b", True),
        ("a/b", "a/b/c/d", False),
    ],
)
def test_is_relative_to(path1: str, path2: str, expected: bool) -> None:
    assert is_relative_to(Path(path1), Path(path2)) is expected


@pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific path semantics")
@pytest.mark.parametrize(
    ("path1", "path2", "expected"),
    [
        ("C:\\a\\b", "D:\\a", False),
        ("\\\\?\\C:\\a\\b", "D:\\", False),
        ("\\\\?\\UNC\\server1\\a\\b", "\\\\?\\UNC\\server2\\a", False),
        ("\\\\server1\\a\\b", "\\\\?\\UNC\\server2\\a", False),
    ],
)
def test_is_relative_to_windows_different_roots(
    path1: str, path2: str, expected: bool
) -> None:
    assert is_relative_to(Path(path1), Path(path2)) is expected
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@radoering radoering force-pushed the fix-windows-long-path-issue branch from 0b9e540 to 2d47912 Compare March 29, 2026 11:31
@radoering radoering force-pushed the fix-windows-long-path-issue branch from 2d47912 to ba4b185 Compare March 29, 2026 11:37
@radoering radoering merged commit a16fbb1 into python-poetry:main Mar 29, 2026
102 of 104 checks passed
radoering added a commit that referenced this pull request Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant