Skip to content

The agent that grows with you

fix(pm): is_junction returns False for a path that does not exist

ethernetbb706e92c775
diff --git a/pm/filesystem.py b/pm/filesystem.py
index 03874126c59..560dc2367a1 100644
--- a/pm/filesystem.py
+++ b/pm/filesystem.py
@@ -20,8 +20,16 @@ _LOCK_POLL_SECONDS = 0.05
def is_junction(path: Path) -> bool:
- """Keep junctions opaque even before Python 3.12's Path.is_junction exists."""
- return os.name == "nt" and path.lstat().st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT
+ """Keep junctions opaque even before Python 3.12's Path.is_junction exists.
+
+ A path that cannot be stat'ed is not a junction, matching ``os.path.isjunction``: callers
+ probe slots that do not exist yet (a first plugin install) and must get False, not a crash.
+ POSIX stat results carry no reparse tag, which lands in the same False.
+ """
+ try:
+ return path.lstat().st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT
+ except (OSError, AttributeError):
+ return False
_VERBATIM = "\\\\?\\"
diff --git a/tests/pm/test_filesystem_is_junction.py b/tests/pm/test_filesystem_is_junction.py
new file mode 100644
index 00000000000..86b70362dd5
--- /dev/null
+++ b/tests/pm/test_filesystem_is_junction.py
@@ -0,0 +1,34 @@
+"""is_junction answers for any path, including one that does not exist yet."""
+import os
+import subprocess
+from pathlib import Path
+
+import pytest
+
+from pm.filesystem import is_junction
+
+
+def test_missing_path_is_not_a_junction(tmp_path: Path) -> None:
+ # A first plugin install probes plugins/<name> before anything is there.
+ assert is_junction(tmp_path / "absent") is False
+ assert is_junction(tmp_path / "absent" / "deeper") is False
+ # POSIX stat results have no reparse tag at all.
+ assert is_junction(tmp_path) is False
+
+
+def test_real_junction_and_plain_directory(tmp_path: Path) -> None:
+ target = tmp_path / "target"
+ target.mkdir()
+ junction = tmp_path / "junction"
+ command = str(Path(os.environ["SystemRoot"]) / "System32" / "cmd.exe")
+ result = subprocess.run(
+ [command, "/d", "/c", "mklink", "/J", str(junction), str(target)],
+ capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=15,
+ )
+ assert result.returncode == 0, result.stdout + result.stderr
+ try:
+ assert is_junction(junction) is True
+ assert is_junction(target) is False
+ finally:
+ junction.rmdir()