diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py index ec21abb9bd0..924dccf5288 100644 --- a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py +++ b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py @@ -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, @@ -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) diff --git a/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py b/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py index 84a45996614..bc40f6ce6f9 100644 --- a/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py +++ b/cuda_pathfinder/cuda/pathfinder/_headers/find_nvidia_headers.py @@ -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, @@ -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: diff --git a/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py b/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py index 62f7b94b065..b09e8dbf6cc 100644 --- a/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py +++ b/cuda_pathfinder/cuda/pathfinder/_static_libs/locate_file.py @@ -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 @@ -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) diff --git a/cuda_pathfinder/cuda/pathfinder/_utils/env_vars.py b/cuda_pathfinder/cuda/pathfinder/_utils/env_vars.py index b8c3267d541..3a41e27c02b 100644 --- a/cuda_pathfinder/cuda/pathfinder/_utils/env_vars.py +++ b/cuda_pathfinder/cuda/pathfinder/_utils/env_vars.py @@ -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 diff --git a/cuda_pathfinder/tests/test_utils_env_vars.py b/cuda_pathfinder/tests/test_utils_env_vars.py index e675f243b4a..50036c4d62e 100644 --- a/cuda_pathfinder/tests/test_utils_env_vars.py +++ b/cuda_pathfinder/tests/test_utils_env_vars.py @@ -10,6 +10,7 @@ from cuda.pathfinder._utils.env_vars import ( _paths_differ, + get_conda_prefix, get_cuda_path_or_home, ) @@ -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"