Description
Problem statement
I don't understand enough about how scikit-build-core is working here to be confident in any recommendation, so to avoid the XY problem I'll state exactly what I'm observing before I make any suggestions.
I have two projects that use Cython where one contains pxd files with declarations that I want to be able to cimport
into the other package. If I install the first package, everything works fine when I build the second package. If I have an editable installation of the first package, however, Cython fails to find the headers:
Error compiling Cython file:
------------------------------------------------------------
...
from foo.bar_a cimport baz
^
------------------------------------------------------------
...: 'foo/bar_a.pxd' not found
I would like for this to work.
Some observations
IIUC the way editable installs work is that build artifacts are copied into site-packages from the build directory (ideally not a temp one, so something specified like build-dir=build/{wheel_tag}
in pyproject.toml) after the build completes, but Python modules are instead remapped via a custom import loader registered via *_editable.py
in site-packages.py.
Inspecting this list reveals that Cython modules are being included. I see something like this in the installed _editable.py
file:
install({'foo.bar_a': '/path/to/foo/bar_a.pyx', 'foo.bar_b': '/path/to/foo/bar_b.pxd'}, {'foo.bar_a': 'foo/bar_a/lib.cpython-310-x86_64-linux-gnu.so'}, '$build_dir', False, True, [], [])
In this case, I've introduce an additional module foo.bar_b
that is only a set of (in this case cdef extern
) declarations. That could be useful for the purpose of e.g. doing what Cython's C/C++ standard library support does with cimport
ing from libc
or libcpp
.
Note that, interestingly, the pyx file is included in the set of what I assume are source files, but the corresponding compiled extension module is also included in the second list of what I assume are meant to be importable extension modules (and the extension module is not in the source tree, it is placed into site-packages). On the other hand, the pxd file shows up in the first list and not in the second.
My guess is that the pyx file shouldn't be in the first list at all; Cython source files require compilation, so there's no reason to include them in the first list if it is indeed a list of source files that should be found in the search tree upon import. However, I don't think that's relevant for my problem, and I suspect that in practice it has no effect and importlib just ignores the pyx file. What I'm observing is because Cython's cimports don't have anything to do with Python's import machinery, so registering this import loader for a pxd file has no effect on Cython compilation.
Proposed solution
If my ramblings above seem correct, then my guess is that the best solution here would be for editable installs to support some way to specify an additional set of files that need to be installed into the site directory for an editable install. It is insufficient to only take extension modules when Cython headers are in use, so I think the pxd files need to be installed as well just like the DSOs. My suggestion of configuring an additional set of files (ideally globs or so) is to avoid indexing too heavily towards Cython and to instead make the support more general.