Search code examples
pythonrustgithub-actionsmaturin

Github workflow Python project with Maturin fails to build


I have a Python project with rust extension module using pyo3 bindings. The project successfully builds and compiles locally, and it builds and compiles on readthedocs. It uses a pip install . method and builds local wheels after rust and all dependencies built on the local architecture.

However, it fails to build on github workflow. The relevant part of the github workflow commands is:

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        python-version: ["3.11"]

    steps:
    - uses: actions/checkout@v4
    - name: Set up Rust
      uses: actions-rust-lang/setup-rust-toolchain@v1
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v3
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install . -v

The main cause of error is that Rust compiler warnings, which seem to be ignored in the other cases are reported as direct errors and not warnings here.

For example the first is:

error: unused import: `Axis`
    --> src/dual/dual2.rs:12:38
     |
  12 | use ndarray::{Array, Array1, Array2, Axis};
     |                                      ^^^^
     |
     = note: `-D unused-imports` implied by `-D warnings`
     = help: to override `-D warnings` add `#[allow(unused_imports)]`

Ultimately this is irrelevant. Sure I could remove this, but some warnings reported as errors are not actually sensible to change.

It leads to:

  💥 maturin failed
    Caused by: Failed to build a native library through cargo
    Caused by: Cargo build finished with "exit status: 101": `env -u CARGO PYO3_ENVIRONMENT_SIGNATURE="cpython-3.11-64bit" PYO3_PYTHON="/opt/hostedtoolcache/Python/3.11.8/x64/bin/python" PYTHON_SYS_EXECUTABLE="/opt/hostedtoolcache/Python/3.11.8/x64/bin/python" "cargo" "rustc" "--message-format" "json-render-diagnostics" "--manifest-path" "/home/runner/work/folder/Cargo.toml" "--release" "--lib" "--crate-type" "cdylib"`
  Error: command ['maturin', 'pep517', 'build-wheel', '-i', '/opt/hostedtoolcache/Python/3.11.8/x64/bin/python', '--compatibility', 'off'] returned non-zero exit status 1
  error: subprocess-exited-with-error

What is the solution to get this building properly on github actions?


Solution

  • actions-rust-lang/setup-rust-toolchain@v1 sets -D warnings to make warnings errors, which ultimately is a good thing - you don't want code with warnings to pass your CI. Whether to deny all warnings is debatable, but some (like unused imports!) should definitely be there.

    So, the real fix is to go fix your code.

    Still, if you'd like to override this setting, you can do that: this workflow has the rustflags input, which you can set to empty to prevent -D warnings:

    jobs:
      build:
    
        runs-on: ubuntu-latest
        strategy:
          fail-fast: false
          matrix:
            python-version: ["3.11"]
    
        steps:
        - uses: actions/checkout@v4
        - name: Set up Rust
          uses: actions-rust-lang/setup-rust-toolchain@v1
          with:
            rustflags: ''
        - name: Set up Python ${{ matrix.python-version }}
          uses: actions/setup-python@v3
          with:
            python-version: ${{ matrix.python-version }}
        - name: Install dependencies
          run: |
            python -m pip install --upgrade pip
            pip install . -v