maturin icon indicating copy to clipboard operation
maturin copied to clipboard

abi3 Windows builds with pyo3 >= 0.29 are unimportable on MSYS2/MinGW (python3.dll vs libpython3.dll)

Open ariostas opened this issue 3 months ago • 1 comments

I ran into this issue during a dependebot update (https://github.com/ariostas/cygv/pull/86). I looked into it a bit with Claude, and it found that some other repos have ran into the same issue. The output from Claude is attached below.

:robot: AI text below :robot:

Summary

Building an abi3 extension module against pyo3 >= 0.29 in an MSYS2/MINGW64 environment produces a .pyd that links and installs without any error but cannot be imported:

ImportError: DLL load failed while importing cygv: The specified module could not be found.

The extension's import table names python3.dll, which does not exist on MSYS2 — it ships libpython3.dll. The same project builds and imports fine with pyo3 0.28.

Environment

  • maturin 1.14.1 (mingw-w64-x86_64-python-maturin), driven by pip install --no-build-isolation .
  • pyo3 0.29.2, features = ["abi3-py310"]
  • MSYS2 MINGW64, mingw-w64-x86_64-python 3.14.7, mingw-w64-x86_64-rust 1.97.0
  • Failing run: https://github.com/ariostas/cygv/actions/runs/31948115189/job/95167262967

Root cause

set_env_vars computes force_target_abi = stable_abi.is_some() && pyo3_ver >= 0.29.0 (src/compile.rs:872-873) and, when it is set, writes its own PYO3_CONFIG_FILE rather than letting pyo3 probe the interpreter (src/compile.rs:916-944). With pyo3 0.28 that branch is inert, so the interpreter got probed as usual.

The generated config (src/python_interpreter/config.rs:460-534) carries implementation, version, target_abi, shared, build_flags and pointer_width — but no lib_name, and nothing that records that the interpreter is a MinGW one. lib_name is only written for Android (lines 519-526).

pyo3 then fills the gap:

// pyo3-build-config 0.29.2, src/impl_.rs:692
if config.lib_name.is_none() {
    config.lib_name = Some(default_lib_name_for_target(config.target_abi, target));
}

and default_lib_name_for_target hardcodes mingw = false:

// src/impl_.rs:2372
fn default_lib_name_for_target(abi: PythonAbi, target: &Triple) -> String {
    if target.operating_system == OperatingSystem::Windows {
        default_lib_name_windows(abi, false, false).unwrap()

For abi3 that yields python3. pyo3 0.29 made raw-dylib the default on Windows and python3 is in its known-DLL list, so the extension gets #[link(name = "python3", kind = "raw-dylib")] and the DLL name goes straight into the import table. raw-dylib needs no import library, so nothing fails at build time — the failure only shows up at import.

With pyo3 0.28 the same python3 name went through -lpython3; the MinGW linker resolved that to /mingw64/lib/libpython3.dll.a, and the import table got the correct libpython3.dll. That is why this regressed silently.

This is not the same bug as PyO3/pyo3#6157, and it survives its fix (PyO3/pyo3#6185, released in pyo3 0.29.2). That fix teaches default_lib_name_windows to add the lib prefix when mingw is true, but the flag is only ever true when pyo3 probes a live interpreter — which is exactly what the config file generated here replaces. The run linked above is pyo3 0.29.2, i.e. with #6185 already applied.

Reproducer

In an MSYS2 MINGW64 shell, with any pyo3 project using an abi3-py3* feature:

pip install --no-build-isolation .
python -c "import your_module"   # ImportError: DLL load failed
objdump -p .../your_module.pyd | grep 'DLL Name'   # python3.dll

Suggested fix

Write lib_name for MinGW interpreters the way it is already written for Android. maturin has the signal in the same struct: InterpreterConfig::ext_suffix is .cp314-mingw_x86_64_msvcrt_gnu.pyd on MSYS2 (and SOABI likewise starts with cp314-mingw), so something along the lines of

if target.is_windows() && self.ext_suffix.contains("-mingw") {
    let lib_name = if stable_abi.is_some() {
        "libpython3".to_string()
    } else {
        format!("libpython{}.{}", self.major, self.minor)
    };
    write!(content, "\nlib_name={lib_name}").unwrap();
}

would give pyo3 the name it would have derived from the interpreter itself. (pyo3 cannot infer this from the target triple: x86_64-pc-windows-gnu is also used to cross-compile against a regular MSVC CPython, where python3.dll is right.)

Downstream

MSYS2 has been hitting this: msys2/MINGW-packages#30442 (_polars_runtime.pyd links against python3.dll instead of libpython3.dll) for a maturin abi3 build, and msys2/MINGW-packages#29942 for the setuptools-rust equivalent. Both PKGBUILDs now carry export PYO3_USE_RAW_DYLIB=0.

Workaround

Setting PYO3_USE_RAW_DYLIB=0 restores the pyo3 0.28 behaviour and fixes it.

ariostas avatar Aug 16 '26 14:08 ariostas

I reproduced this on a fresh native MSYS2/MINGW64 environment using maturin built from the current upstream main at:

479658ad9bce22c6e4955b2e47182111c166e37c

Environment:

  • MSYSTEM=MINGW64
  • Python 3.14.7
  • sysconfig.get_platform() = mingw_x86_64_msvcrt_gnu
  • Rust host: x86_64-pc-windows-gnu

Using identical minimal abi3-py310 extension sources with isolated Cargo target directories, wheels, and fresh venvs, I observed:

  • PyO3 0.28.2, PYO3_USE_RAW_DYLIB unset:

    • Build: succeeds
    • Import: succeeds
    • Python DLL in installed .pyd: libpython3.dll
  • PyO3 0.29.2, PYO3_USE_RAW_DYLIB unset:

    • Build: succeeds
    • Import: fails with ImportError: DLL load failed
    • Python DLL in installed .pyd: python3.dll
  • PyO3 0.29.2, PYO3_USE_RAW_DYLIB=0:

    • Build: succeeds
    • Import: succeeds
    • Python DLL in installed .pyd: libpython3.dll

For both PyO3 0.29.2 cases, maturin generated:

target/maturin/pyo3-config-x86_64-pc-windows-gnu-3.14-abi3.txt

with the following contents:

implementation=CPython
version=3.10
target_abi=CPython-abi3-3.10
shared=true
build_flags=
suppress_build_script_link_lines=false

lib_name was absent in both cases.

One installation detail: native MSYS2 pip rejects direct installation of the generated win_amd64 wheel because the interpreter platform tag is mingw_x86_64_msvcrt_gnu. To isolate the runtime loader behavior, I installed each exact, unchanged wheel into its fresh venv using pip’s explicit --platform win_amd64, --only-binary=:all:, and --target <venv-site-packages> options. Imports were run outside the project directories, and objdump -p was applied to the exact installed .pyd.

This confirms the reported runtime regression and the PYO3_USE_RAW_DYLIB=0 workaround.

I’d like to work on a focused regression test and fix. My initial plan is to use the interpreter-derived sysconfig.get_platform() value as the MinGW signal, with coverage for MinGW abi3/abi3t and a non-MinGW Windows control, rather than inferring MinGW from the Rust target triple or EXT_SUFFIX.

If someone is already working on this, or if you would prefer this information to be represented differently in the generated PyO3 configuration, please let me know.

Udonburo avatar Aug 30 '26 22:08 Udonburo