Skip to content
Open
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
8 changes: 7 additions & 1 deletion pylsp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ def find_parents(root, path, names):
# Split the relative by directory, generate all the parent directories, then check each of them.
# This avoids running a loop that has different base-cases for unix/windows
# e.g. /a/b and /a/b/c/d/e.py -> ['/a/b', 'c', 'd']
dirs = [root] + os.path.relpath(os.path.dirname(path), root).split(os.path.sep)
try:
dirs = [root] + os.path.relpath(os.path.dirname(path), root).split(os.path.sep)
except ValueError:
# On Windows, relpath raises ValueError when path and root are on different mounts
# (e.g. a UNC share root vs a drive-letter path). Nothing to find in this case.
log.warning("Path %r not in %r", path, root)
return []

# Search each of /a/b/c, /a/b, /a
while dirs:
Expand Down
13 changes: 13 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ def test_find_parents(tmpdir) -> None:
]


def test_find_parents_cross_mount(tmpdir, monkeypatch) -> None:
"""find_parents returns [] when root and path are on different mounts (Windows UNC)."""
import unittest.mock as mock

subsubdir = tmpdir.ensure_dir("subdir", "subsubdir")
path = subsubdir.ensure("path.py")

with mock.patch("os.path.relpath", side_effect=ValueError("path is on mount 'C:', start on mount '\\\\unc\\share'")):
result = _utils.find_parents(tmpdir.strpath, path.strpath, ["test.cfg"])

assert result == []


def test_merge_dicts() -> None:
assert _utils.merge_dicts(
{"a": True, "b": {"x": 123, "y": {"hello": "world"}}},
Expand Down