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
6 changes: 3 additions & 3 deletions cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
find_lib_in_dir,
find_lib_in_site_packages,
)
from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
from cuda.pathfinder._utils.env_vars import get_conda_prefix, get_cuda_path_or_home
from cuda.pathfinder._utils.found_via import (
FOUND_VIA_CONDA,
FOUND_VIA_CUDA_PATH,
Expand Down Expand Up @@ -146,8 +146,8 @@ def find_in_site_packages(ctx: SearchContext) -> FindResult | NotFound:

def find_in_conda(ctx: SearchContext) -> FindResult | NotFound:
"""Search ``$CONDA_PREFIX``."""
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
conda_prefix = get_conda_prefix()
if conda_prefix is None:
return NotFound()
anchor = ctx.conda_anchor_point(conda_prefix)
return _find_under_anchor_root(ctx, anchor, FOUND_VIA_CONDA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
system_install_dir_patterns,
)
from cuda.pathfinder._utils.ctk_root_canary import CTK_ROOT_CANARY_ANCHOR_LIBNAMES
from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
from cuda.pathfinder._utils.env_vars import get_conda_prefix, get_cuda_path_or_home
from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages
from cuda.pathfinder._utils.found_via import (
FOUND_VIA_CONDA,
Expand Down Expand Up @@ -97,8 +97,8 @@ def find_in_site_packages(desc: HeaderDescriptor) -> LocatedHeaderDir | None:

def find_in_conda(desc: HeaderDescriptor) -> LocatedHeaderDir | None:
"""Search ``$CONDA_PREFIX``."""
conda_prefix = os.environ.get("CONDA_PREFIX")
if not conda_prefix:
conda_prefix = get_conda_prefix()
if conda_prefix is None:
return None
anchor_point = resolve_conda_anchor(desc, conda_prefix)
if anchor_point is None:
Expand Down
7 changes: 3 additions & 4 deletions cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

"""Search shared by the static-library and bitcode-library finders."""

import os
from pathlib import Path

from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
from cuda.pathfinder._utils.env_vars import get_conda_prefix, get_cuda_path_or_home
from cuda.pathfinder._utils.find_sub_dirs import find_sub_dirs_all_sitepackages
from cuda.pathfinder._utils.found_via import FOUND_VIA_CONDA, FOUND_VIA_CUDA_PATH, FOUND_VIA_SITE_PACKAGES
from cuda.pathfinder._utils.platform_aware import IS_WINDOWS
Expand Down Expand Up @@ -44,8 +43,8 @@ def locate_file(
if abs_path is not None:
return abs_path, FOUND_VIA_SITE_PACKAGES

conda_prefix = os.environ.get("CONDA_PREFIX")
if conda_prefix:
conda_prefix = get_conda_prefix()
if conda_prefix is not None:
anchor = Path(conda_prefix, "Library") if IS_WINDOWS else Path(conda_prefix)
for rel_path in conda_rel_paths:
abs_path = _resolved_if_file(anchor / rel_path, filename)
Expand Down
8 changes: 8 additions & 0 deletions cuda_pathfinder/cuda/pathfinder/_utils/env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,11 @@ def get_cuda_path_or_home() -> str | None:

# Return the first (highest priority) set variable
return next(iter(set_vars.values()))


def get_conda_prefix() -> str | None:
"""Return ``$CONDA_PREFIX``, or ``None`` if it is unset or empty.

Not cached: every call reads the environment.
"""
return os.environ.get("CONDA_PREFIX") or None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This confirms that the three call sites converted here treat an empty CONDA_PREFIX as unset, as before.

One non-blocking, pre-existing inconsistency, which I'd suggest fixing in a separate small PR rather than here. _binaries/find_nvidia_binary_utility.py (step 2, "Search in Conda environment") reads the variable itself and only checks for None:

if (conda_prefix := os.environ.get("CONDA_PREFIX")) is not None:
    ...
    dirs.append(os.path.join(conda_prefix, "bin"))

With CONDA_PREFIX="" this appends the relative directory "bin". It passes the assert directory check, because the string is non-empty. The executable lookup then resolves it against the current working directory. That contradicts the docstring's "Lookup is restricted to the trusted directories". This helper makes the fix a one-liner:

-from cuda.pathfinder._utils.env_vars import get_cuda_path_or_home
+from cuda.pathfinder._utils.env_vars import get_conda_prefix, get_cuda_path_or_home
@@
-    if (conda_prefix := os.environ.get("CONDA_PREFIX")) is not None:
+    if (conda_prefix := get_conda_prefix()) is not None:

A regression test would plant an executable at <cwd>/bin/<name>, then monkeypatch.chdir(tmp_path) and setenv("CONDA_PREFIX", ""). It would assert that find_nvidia_binary_utility(name) is None when the wheel, CUDA_PATH and canary steps all miss. It fails today and passes with the change.

I left it out of this PR on purpose, since the PR says it changes no behavior. It also deserves a one-line release note, because it changes behavior in that edge case.

11 changes: 11 additions & 0 deletions cuda_pathfinder/tests/test_utils_env_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from cuda.pathfinder._utils.env_vars import (
_paths_differ,
get_conda_prefix,
get_cuda_path_or_home,
)

Expand Down Expand Up @@ -248,3 +249,13 @@ def test_caching_behavior(monkeypatch, tmp_path):
get_cuda_path_or_home.cache_clear()
result3 = get_cuda_path_or_home()
assert pathlib.Path(result3) == second_dir


@pytest.mark.agent_authored(model="claude-fable-5-2")
def test_get_conda_prefix_treats_empty_as_unset(monkeypatch):
monkeypatch.delenv("CONDA_PREFIX", raising=False)
assert get_conda_prefix() is None
monkeypatch.setenv("CONDA_PREFIX", "")
assert get_conda_prefix() is None
monkeypatch.setenv("CONDA_PREFIX", "/opt/conda")
assert get_conda_prefix() == "/opt/conda"
Loading