Skip to content

Commit 7b0acc6

Browse files
author
Meitham Jamaa
committed
bugfix: ValueError: path is on mount crash when opening notebooks on UNC network shares (Windows)
1 parent bc1095f commit 7b0acc6

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

pylsp/_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ def find_parents(root, path, names):
9696
# Split the relative by directory, generate all the parent directories, then check each of them.
9797
# This avoids running a loop that has different base-cases for unix/windows
9898
# e.g. /a/b and /a/b/c/d/e.py -> ['/a/b', 'c', 'd']
99-
dirs = [root] + os.path.relpath(os.path.dirname(path), root).split(os.path.sep)
99+
try:
100+
dirs = [root] + os.path.relpath(os.path.dirname(path), root).split(os.path.sep)
101+
except ValueError:
102+
# On Windows, relpath raises ValueError when path and root are on different mounts
103+
# (e.g. a UNC share root vs a drive-letter path). Nothing to find in this case.
104+
log.warning("Path %r not in %r", path, root)
105+
return []
100106

101107
# Search each of /a/b/c, /a/b, /a
102108
while dirs:

test/test_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ def test_find_parents(tmpdir) -> None:
197197
]
198198

199199

200+
def test_find_parents_cross_mount(tmpdir, monkeypatch) -> None:
201+
"""find_parents returns [] when root and path are on different mounts (Windows UNC)."""
202+
import unittest.mock as mock
203+
204+
subsubdir = tmpdir.ensure_dir("subdir", "subsubdir")
205+
path = subsubdir.ensure("path.py")
206+
207+
with mock.patch("os.path.relpath", side_effect=ValueError("path is on mount 'C:', start on mount '\\\\unc\\share'")):
208+
result = _utils.find_parents(tmpdir.strpath, path.strpath, ["test.cfg"])
209+
210+
assert result == []
211+
212+
200213
def test_merge_dicts() -> None:
201214
assert _utils.merge_dicts(
202215
{"a": True, "b": {"x": 123, "y": {"hello": "world"}}},

0 commit comments

Comments
 (0)