Skip to content

Use cuda-pathfinder in build-system.requires#1817

Open
rwgk wants to merge 5 commits intoNVIDIA:mainfrom
rwgk:pathfinder_in_build-system_requires
Open

Use cuda-pathfinder in build-system.requires#1817
rwgk wants to merge 5 commits intoNVIDIA:mainfrom
rwgk:pathfinder_in_build-system_requires

Conversation

@rwgk
Copy link
Copy Markdown
Collaborator

@rwgk rwgk commented Mar 25, 2026

Follow-up to #1801. Closes the build-time integration gap identified in #1803.

The build hooks in cuda_bindings and cuda_core now use cuda.pathfinder.get_cuda_path_or_home() to resolve CUDA_PATH/CUDA_HOME, replacing the previous inline os.environ.get() fallback.

This firmly establishes that cuda-pathfinder is usable as a tool at build time, not just at runtime, although a helper function (_import_get_cuda_path_or_home()) is needed because cuda is a Python namespace package; see #1824 for details.

  • Pin cuda-pathfinder>=1.5 in both build-system.requires and project.dependencies for cuda_bindings and cuda_core.
  • Namespace fix for PEP 517 isolated builds: In isolated build environments, backend-path=["."] causes the cuda namespace to shadow the installed cuda-pathfinder. The new _import_get_cuda_path_or_home() helper detects this and fixes cuda.__path__ by replacing the _NamespacePath with a plain list that includes the site-packages cuda/ directory.
  • Test updates: cuda_core/tests/test_build_hooks.py adds get_cuda_path_or_home.cache_clear() calls so monkeypatched env vars take effect.
  • Stale pixi version: Updated cuda_pathfinder/pixi.toml package version from 1.3.4a0 to 1.5.0 (just to avoid confusing someone looking at the version in passing).

rwgk added 2 commits March 24, 2026 17:00
Pin cuda-pathfinder>=1.5 in both build-system.requires and
project.dependencies.

Made-with: Cursor
The previous 1.3.4a0 was a stale value that could confuse someone.

Made-with: Cursor
@copy-pr-bot
Copy link
Copy Markdown
Contributor

copy-pr-bot bot commented Mar 25, 2026

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@rwgk
Copy link
Copy Markdown
Collaborator Author

rwgk commented Mar 25, 2026

/ok to test

@github-actions
Copy link
Copy Markdown

@rwgk rwgk marked this pull request as ready for review March 25, 2026 03:44
@rwgk rwgk self-assigned this Mar 25, 2026
@rwgk rwgk added enhancement Any code-related improvements P1 Medium priority - Should do cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module labels Mar 25, 2026
@rwgk rwgk added this to the cuda.core v0.7.0 milestone Mar 25, 2026
@rwgk rwgk added P1 Medium priority - Should do and removed P1 Medium priority - Should do labels Mar 25, 2026
@rwgk rwgk requested a review from cpcloud March 25, 2026 04:40
COMPILE_FOR_COVERAGE = bool(int(os.environ.get("CUDA_PYTHON_COVERAGE", "0")))


# Please keep in sync with the copy in cuda_bindings/build_hooks.py.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should avoid duplicating implementations. Could we not hoist this into a separate file and import from both?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We should avoid duplicating implementations.

Agreed

Could we not hoist this into a separate file and import from both?

No 😭


In the words of Cursor Opus 4.6 1M Thinking:

There's no reasonable way to share code between the two build_hooks.py files:

  • Each is a PEP 517 build backend loaded via backend-path=["."] from its own package directory (cuda_bindings/ or cuda_core/). They can't import from the monorepo root or from each other.
  • The function exists because cuda.pathfinder can't be imported normally in this context — so it can't live inside cuda-pathfinder either.
  • A shared helper package in build-system.requires would be massive overkill for ~15 lines of stable code.

The only alternatives are worse: a new PyPI package just for this, importlib file-path hacks, or copying a shared file into both directories at CI time.

The "please keep in sync" comments are the pragmatic solution here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We have other existing cases of "please keep in sync" that are impractical to avoid.

It just crossed my mind: a pre-commit check to enforce that they stay in sync is probably pretty easy to implement.

if os.path.isdir(os.path.join(sp_cuda, "pathfinder")):
cuda.__path__ = list(cuda.__path__) + [sp_cuda]
break
import cuda.pathfinder
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we include some message explaining that we iterated through sys.path looking for pathfinder and couldn't find it? That seems to be important information that would be missing from just throwing we couldn't import pathfinder module.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

          for p in sys.path:
              sp_cuda = os.path.join(p, "cuda")                                                                                
              if os.path.isdir(os.path.join(sp_cuda, "pathfinder")):
                  cuda.__path__ = list(cuda.__path__) + [sp_cuda]                                                              
                  break                 
          else:                                                                                                                
              raise ModuleNotFoundError(
                  "cuda-pathfinder is not installed in the build environment. "                                                
                  "Ensure 'cuda-pathfinder>=1.5' is in build-system.requires."                                                 
              )
          import cuda.pathfinder ```

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done: commit 3d422de

Copy link
Copy Markdown
Contributor

@cpcloud cpcloud left a comment

Choose a reason for hiding this comment

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

The path hacking needs some more justification and explicit examples of in what common scenarios a plain import isn't going to work.

Comment on lines +51 to +55
for p in sys.path:
sp_cuda = os.path.join(p, "cuda")
if os.path.isdir(os.path.join(sp_cuda, "pathfinder")):
cuda.__path__ = list(cuda.__path__) + [sp_cuda]
break
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.

Path hacking of any sort like this is a code smell. At the very least the docstring is missing a concrete example of why this is needed.

isolated build environments

is doing a ton of work there. Isolated how? What is backend-path and where does that come from? Where are all these nouns like backend-path and build-env coming from?

In what real-world scenarios does this actually happen where there's not a viable alternative?

I'm really skeptical that path hacking like this is necessary.

Copy link
Copy Markdown
Collaborator Author

@rwgk rwgk Mar 25, 2026

Choose a reason for hiding this comment

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

It's fully documented now here: #1824 (that in turn is pointing to #1803, where you can see the original development history for the workaround)

The comment here was updated:

commit 6892ae0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module enhancement Any code-related improvements P1 Medium priority - Should do

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants