From c8fa6e27e1859dfdb8b2e2823109d881c78db234 Mon Sep 17 00:00:00 2001 From: Rob Parolin Date: Thu, 26 Nov 2026 11:34:43 -0700 Subject: [PATCH 1/2] refactor(pathfinder): read CONDA_PREFIX in one place Three finders read `CONDA_PREFIX` on their own and each treated an empty value as unset: dynamic libraries, headers, and the shared static and bitcode search. `get_conda_prefix()` in `_utils/env_vars.py` now holds that rule, and the three call it. It is not cached. Like the code it replaces, it reads the environment on every call. The binary finder is left alone on purpose. It treats an empty `CONDA_PREFIX` as set, so switching it over would change behavior. No behavior change. Co-Authored-By: Claude Fable 5.2 Signed-off-by: Rob Parolin --- .../cuda/pathfinder/_dynamic_libs/search_steps.py | 6 +++--- .../cuda/pathfinder/_headers/find_nvidia_headers.py | 6 +++--- .../cuda/pathfinder/_static_libs/locate_file.py | 7 +++---- cuda_pathfinder/cuda/pathfinder/_utils/env_vars.py | 8 ++++++++ cuda_pathfinder/tests/test_utils_env_vars.py | 11 +++++++++++ 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py b/cuda_pathfinder/cuda/pathfinder/_dynamic_libs/search_steps.py index ec21abb9bd0c..924dccf52885 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 84a45996614c..bc40f6ce6f92 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 62f7b94b0651..b09e8dbf6cc3 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 b8c3267d541a..3a41e27c02b8 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 e675f243b4ad..50036c4d62e9 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" From 5d5f8164d6b0370f2869b181f30d87a513f1f4d4 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 26 Nov 2026 11:17:28 -0800 Subject: [PATCH 2/2] ci: require manual copy-pr-bot approval Signed-off-by: Ralf W. Grosse-Kunstleve