Skip to content

Commit d6b3bf3

Browse files
committed
fix(cuda.pathfinder): return absolute library paths
Convert discovered static and bitcode paths to absolute paths so cached results remain valid after changing directories. Preserve symlink-parent traversal and cover each discovery source. Signed-off-by: 0z5a <[email protected]>
1 parent deb3d04 commit d6b3bf3

4 files changed

Lines changed: 130 additions & 6 deletions

File tree

‎cuda_pathfinder/cuda/pathfinder/_static_libs/find_bitcode_lib.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
143143
"""
144144
finder = _FindBitcodeLib(name)
145145

146+
# Path.absolute preserves symlink/.. traversal rather than normalizing it.
146147
abs_path = finder.try_site_packages()
147148
if abs_path is not None:
148149
return LocatedBitcodeLib(
149150
name=name,
150-
abs_path=str(abs_path),
151+
abs_path=str(abs_path.absolute()),
151152
filename=finder.filename,
152153
found_via="site-packages",
153154
)
@@ -156,7 +157,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
156157
if abs_path is not None:
157158
return LocatedBitcodeLib(
158159
name=name,
159-
abs_path=str(abs_path),
160+
abs_path=str(abs_path.absolute()),
160161
filename=finder.filename,
161162
found_via="conda",
162163
)
@@ -165,7 +166,7 @@ def locate_bitcode_lib(name: str) -> LocatedBitcodeLib:
165166
if abs_path is not None:
166167
return LocatedBitcodeLib(
167168
name=name,
168-
abs_path=str(abs_path),
169+
abs_path=str(abs_path.absolute()),
169170
filename=finder.filename,
170171
found_via="CUDA_PATH",
171172
)

‎cuda_pathfinder/cuda/pathfinder/_static_libs/find_static_lib.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
142142
"""
143143
finder = _FindStaticLib(name)
144144

145+
# Path.absolute preserves symlink/.. traversal rather than normalizing it.
145146
abs_path = finder.try_site_packages()
146147
if abs_path is not None:
147148
return LocatedStaticLib(
148149
name=name,
149-
abs_path=str(abs_path),
150+
abs_path=str(abs_path.absolute()),
150151
filename=finder.filename,
151152
found_via="site-packages",
152153
)
@@ -155,7 +156,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
155156
if abs_path is not None:
156157
return LocatedStaticLib(
157158
name=name,
158-
abs_path=str(abs_path),
159+
abs_path=str(abs_path.absolute()),
159160
filename=finder.filename,
160161
found_via="conda",
161162
)
@@ -164,7 +165,7 @@ def locate_static_lib(name: str) -> LocatedStaticLib:
164165
if abs_path is not None:
165166
return LocatedStaticLib(
166167
name=name,
167-
abs_path=str(abs_path),
168+
abs_path=str(abs_path.absolute()),
168169
filename=finder.filename,
169170
found_via="CUDA_PATH",
170171
)

‎cuda_pathfinder/tests/test_find_bitcode_lib.py‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,68 @@ def find_expected_sub_dir(sub_dir):
135135
assert located_lib.found_via == "CUDA_PATH"
136136

137137

138+
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
139+
@pytest.mark.parametrize("source", ("site-packages", "conda", "CUDA_PATH"))
140+
@pytest.mark.agent_authored(model="gpt-6")
141+
def test_locate_bitcode_lib_returns_absolute_path_for_relative_root(monkeypatch, tmp_path, source):
142+
monkeypatch.chdir(tmp_path)
143+
monkeypatch.delenv("CONDA_PREFIX", raising=False)
144+
monkeypatch.delenv("CUDA_HOME", raising=False)
145+
monkeypatch.delenv("CUDA_PATH", raising=False)
146+
147+
info = _bitcode_lib_info("device")
148+
root = Path("relative-root")
149+
if source == "site-packages":
150+
lib_dir = root / Path(info["site_packages_dirs"][0])
151+
monkeypatch.setattr(find_bitcode_lib_module, "find_sub_dirs_all_sitepackages", lambda _sub_dir: [str(lib_dir)])
152+
else:
153+
monkeypatch.setattr(find_bitcode_lib_module, "find_sub_dirs_all_sitepackages", lambda _sub_dir: [])
154+
if source == "conda":
155+
monkeypatch.setenv("CONDA_PREFIX", str(root))
156+
lib_dir = _conda_anchor(root) / Path(info["rel_path"])
157+
else:
158+
monkeypatch.setenv("CUDA_HOME", str(root))
159+
lib_dir = root / Path(info["rel_path"])
160+
161+
expected = tmp_path / lib_dir / info["filename"]
162+
expected.parent.mkdir(parents=True)
163+
expected.touch()
164+
located = locate_bitcode_lib("device")
165+
assert located.found_via == source
166+
assert located.abs_path == str(expected)
167+
assert find_bitcode_lib("device") == str(expected)
168+
169+
monkeypatch.chdir(tmp_path.parent)
170+
assert Path(located.abs_path).is_file()
171+
assert Path(find_bitcode_lib("device")).is_file()
172+
173+
174+
@pytest.mark.skipif(os.name == "nt", reason="POSIX symlink parent traversal")
175+
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
176+
@pytest.mark.agent_authored(model="gpt-6")
177+
def test_locate_bitcode_lib_preserves_symlink_parent(monkeypatch, tmp_path):
178+
monkeypatch.chdir(tmp_path)
179+
monkeypatch.delenv("CONDA_PREFIX", raising=False)
180+
monkeypatch.delenv("CUDA_PATH", raising=False)
181+
monkeypatch.setenv("CUDA_HOME", "alias/..")
182+
monkeypatch.setattr(find_bitcode_lib_module, "find_sub_dirs_all_sitepackages", lambda _sub_dir: [])
183+
184+
real_root = tmp_path / "real"
185+
(real_root / "child").mkdir(parents=True)
186+
(tmp_path / "alias").symlink_to(real_root / "child", target_is_directory=True)
187+
info = _bitcode_lib_info("device")
188+
expected = real_root / info["rel_path"] / info["filename"]
189+
expected.parent.mkdir(parents=True)
190+
expected.touch()
191+
192+
located = locate_bitcode_lib("device")
193+
assert Path(located.abs_path).is_absolute()
194+
assert Path(located.abs_path).samefile(expected)
195+
cached = find_bitcode_lib("device")
196+
monkeypatch.chdir(tmp_path.parent)
197+
assert Path(cached).samefile(expected)
198+
199+
138200
@pytest.mark.usefixtures("clear_find_bitcode_lib_cache")
139201
def test_find_bitcode_lib_not_found_error_includes_cuda_home_directory_listing(monkeypatch, tmp_path):
140202
cuda_home = tmp_path / "cuda-home"

‎cuda_pathfinder/tests/test_find_static_lib.py‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,66 @@ def test_locate_static_lib_search_order(monkeypatch, tmp_path):
117117
assert located_lib.found_via == "CUDA_PATH"
118118

119119

120+
@pytest.mark.usefixtures("clear_find_static_lib_cache")
121+
@pytest.mark.parametrize("source", ("site-packages", "conda", "CUDA_PATH"))
122+
@pytest.mark.agent_authored(model="gpt-6")
123+
def test_locate_static_lib_returns_absolute_path_for_relative_root(monkeypatch, tmp_path, source):
124+
monkeypatch.chdir(tmp_path)
125+
monkeypatch.delenv("CONDA_PREFIX", raising=False)
126+
monkeypatch.delenv("CUDA_HOME", raising=False)
127+
monkeypatch.delenv("CUDA_PATH", raising=False)
128+
129+
root = Path("relative-root")
130+
if source == "site-packages":
131+
lib_dir = root / Path(CUDADEVRT_INFO["site_packages_dirs"][0])
132+
monkeypatch.setattr(find_static_lib_module, "find_sub_dirs_all_sitepackages", lambda _sub_dir: [str(lib_dir)])
133+
else:
134+
monkeypatch.setattr(find_static_lib_module, "find_sub_dirs_all_sitepackages", lambda _sub_dir: [])
135+
if source == "conda":
136+
monkeypatch.setenv("CONDA_PREFIX", str(root))
137+
lib_dir = _conda_anchor(root) / Path(CUDADEVRT_INFO["conda_rel_paths"][0])
138+
else:
139+
monkeypatch.setenv("CUDA_HOME", str(root))
140+
lib_dir = root / Path(CUDADEVRT_INFO["ctk_rel_paths"][0])
141+
142+
expected = tmp_path / lib_dir / CUDADEVRT_INFO["filename"]
143+
expected.parent.mkdir(parents=True)
144+
expected.touch()
145+
located = locate_static_lib("cudadevrt")
146+
assert located.found_via == source
147+
assert located.abs_path == str(expected)
148+
assert find_static_lib("cudadevrt") == str(expected)
149+
150+
monkeypatch.chdir(tmp_path.parent)
151+
assert Path(located.abs_path).is_file()
152+
assert Path(find_static_lib("cudadevrt")).is_file()
153+
154+
155+
@pytest.mark.skipif(os.name == "nt", reason="POSIX symlink parent traversal")
156+
@pytest.mark.usefixtures("clear_find_static_lib_cache")
157+
@pytest.mark.agent_authored(model="gpt-6")
158+
def test_locate_static_lib_preserves_symlink_parent(monkeypatch, tmp_path):
159+
monkeypatch.chdir(tmp_path)
160+
monkeypatch.delenv("CONDA_PREFIX", raising=False)
161+
monkeypatch.delenv("CUDA_PATH", raising=False)
162+
monkeypatch.setenv("CUDA_HOME", "alias/..")
163+
monkeypatch.setattr(find_static_lib_module, "find_sub_dirs_all_sitepackages", lambda _sub_dir: [])
164+
165+
real_root = tmp_path / "real"
166+
(real_root / "child").mkdir(parents=True)
167+
(tmp_path / "alias").symlink_to(real_root / "child", target_is_directory=True)
168+
expected = real_root / Path(CUDADEVRT_INFO["ctk_rel_paths"][0]) / CUDADEVRT_INFO["filename"]
169+
expected.parent.mkdir(parents=True)
170+
expected.touch()
171+
172+
located = locate_static_lib("cudadevrt")
173+
assert Path(located.abs_path).is_absolute()
174+
assert Path(located.abs_path).samefile(expected)
175+
cached = find_static_lib("cudadevrt")
176+
monkeypatch.chdir(tmp_path.parent)
177+
assert Path(cached).samefile(expected)
178+
179+
120180
@pytest.mark.usefixtures("clear_find_static_lib_cache")
121181
def test_locate_static_lib_conda_rel_path_fallback(monkeypatch, tmp_path):
122182
filename = CUDADEVRT_INFO["filename"]

0 commit comments

Comments
 (0)