ComPWA / taplo-pre-commit

A pre-commit hook for Taplo, a TOML formatter written in Rust
https://github.com/tamasfe/taplo
9 stars 3 forks source link

Support for Alpine Linux #22

Open tomassebestik opened 4 weeks ago

tomassebestik commented 4 weeks ago

Problem description

Hello,

First of all, thank you for this project! This is finally a nice, working, and configurable TOML formatter usable as a pre-commit hook.

On local machines, everything works great, but we also have a GitLab CI job with pre-commit to ensure the codebase is formatted even if developers forget to install pre-commit in the repo on their local machines.

This CI job uses an Alpine Docker image (python:3.11-alpine3.18), and currently, it is failing with:

[INFO] Installing environment for https://github.com/ComPWA/mirrors-taplo.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/root/.cache/pre-commit/reporohe5gkc/py_env-python3.11/bin/python', '-mpip', 'install', '.')
return code: 1
stdout:
    Processing /root/.cache/pre-commit/reporohe5gkc
      Preparing metadata (setup.py): started
      Preparing metadata (setup.py): finished with status 'done'
    Collecting taplo==0.9.3 (from pre_commit_placeholder_package==0.0.0)
      Downloading taplo-0.9.3.tar.gz (102 kB)
         ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 102.6/102.6 kB 4.2 MB/s eta 0:00:00
      Installing build dependencies: started
      Installing build dependencies: finished with status 'done'
      Getting requirements to build wheel: started
      Getting requirements to build wheel: finished with status 'done'
      Preparing metadata (pyproject.toml): started
      Preparing metadata (pyproject.toml): finished with status 'error'
stderr:
      error: subprocess-exited-with-error

      × Preparing metadata (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [6 lines of output]

          Cargo, the Rust package manager, is not installed or is not on PATH.
          This package requires Rust and Cargo to compile extensions. Install it through
          the system's package manager or via https://rustup.rs/

          Checking for Rust toolchain....
          [end of output]

The issue here, I believe, is the missing Python wheel for Alpine Linux (musl), so the installer tries to compile it from source and fails.

Right now due to missing Alpine support, we can't add https://github.com/ComPWA/mirrors-taplo to our standard hooks for all projects, which I would very much like to do.

Proposed solution

Is it possible to add and maintain support for Alpine Linux?

(Alpine Linux is widely used for CI, and many people could benefit from this.)

What should the interface look like?

No response

Additional context

No response

redeboer commented 4 weeks ago

Thanks for reporting! This is an upstream problem with the taplo package on PyPI, see https://github.com/tamasfe/taplo/issues/658.

redeboer commented 4 weeks ago
      × Preparing metadata (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [6 lines of output]

          Cargo, the Rust package manager, is not installed or is not on PATH.
          This package requires Rust and Cargo to compile extensions. Install it through
          the system's package manager or via https://rustup.rs/

          Checking for Rust toolchain....
          [end of output]

Btw interesting to see that pip falls back to compiling with Rust. You can retry with cargo installed, but the build will probably take a few minutes...

tomassebestik commented 3 weeks ago

Thank you, @redeboer, for the quick response and for reporting the issue to Taplo.


... You can retry with cargo installed, but the build will probably take a few minutes...

I tested it in that Alpine container with Rust installed - that fails too:

## (bind the current dir to the clean Alpine Python container)
$ docker run -it --rm -v $(pwd):/src -w /src --entrypoint=/bin/sh python:3.11-alpine3.18

## (in the container ....install Rust)
/src # apk add curl gcc
/src # curl –proto ‘=https’ –tlsv1.2 -sSf https://sh.rustup.rs | sh
/src # source $HOME/.cargo/env
/src # rustc --version
rustc 1.80.1 (3f5fd8dd4 2024-08-06)

## (set git)
/src # apk add git
/src # git init
/src # git config --global --add safe.directory /src

## (set pre-commit)
/src # pip install pre-commit
/src # pre-commit install

## (run pre-commit hook taplo-format)
/src # pre-commit run taplo-format --all-files
[INFO] Installing environment for https://github.com/ComPWA/mirrors-taplo.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/root/.cache/pre-commit/repovjupyw8e/py_env-python3.11/bin/python', '-mpip', 'install', '.')
return code: 1
stdout:
    Processing /root/.cache/pre-commit/repovjupyw8e
      Preparing metadata (setup.py): started
      Preparing metadata (setup.py): finished with status 'done'
    Collecting taplo==0.9.3 (from pre_commit_placeholder_package==0.0.0)
      Using cached taplo-0.9.3.tar.gz (102 kB)
      Installing build dependencies: started
      Installing build dependencies: finished with status 'done'
      Getting requirements to build wheel: started
      Getting requirements to build wheel: finished with status 'done'
      Preparing metadata (pyproject.toml): started
      Preparing metadata (pyproject.toml): finished with status 'error'
stderr:
      error: subprocess-exited-with-error

      × Preparing metadata (pyproject.toml) did not run successfully.
      │ exit code: 1
      ╰─> [17 lines of output]
          error: failed to load manifest for workspace member `/tmp/pip-install-nuxvxbqj/taplo_b4abb58585984f8f9c74160c05eb512b/crates/taplo-cli`
          referenced by workspace at `/tmp/pip-install-nuxvxbqj/taplo_b4abb58585984f8f9c74160c05eb512b/Cargo.toml`

          Caused by:
            failed to load manifest for dependency `taplo-lsp`

          Caused by:
            failed to read `/tmp/pip-install-nuxvxbqj/taplo_b4abb58585984f8f9c74160c05eb512b/crates/taplo-lsp/Cargo.toml`

          Caused by:
            No such file or directory (os error 2)
          💥 maturin failed
            Caused by: Cargo metadata failed. Does your crate compile with `cargo build`?
            Caused by: `cargo metadata` exited with an error:
          Error running maturin: Command '['maturin', 'pep517', 'write-dist-info', '--metadata-directory', '/tmp/pip-modern-metadata-iapqu48z', '--interpreter', '/root/.cache/pre-commit/repovjupyw8e/py_env-python3.11/bin/python']' returned non-zero exit status 1.
          Checking for Rust toolchain....
          Running `maturin pep517 write-dist-info --metadata-directory /tmp/pip-modern-metadata-iapqu48z --interpreter /root/.cache/pre-commit/repovjupyw8e/py_env-python3.11/bin/python`
          [end of output]

      note: This error originates from a subprocess, and is likely not a problem with pip.
    error: metadata-generation-failed

    × Encountered error while generating package metadata.
    ╰─> See above for output.

    note: This is an issue with the package mentioned above, not pip.
    hint: See above for details.
Check the log at /root/.cache/pre-commit/pre-commit.log
redeboer commented 3 weeks ago

Fix is being prepared in https://github.com/tamasfe/taplo/pull/659. If you can, you could already try installing taplo by installing from the artifacts there (taplo format directly, not via pre-commit). It works in an alpine Docker container: https://github.com/tamasfe/taplo/pull/659#issuecomment-2307460631

I tested it in that Alpine container with Rust installed

Thanks, interesting that it got some steps further at least :)

tomassebestik commented 3 weeks ago

That looks good!

$ docker run -it --rm -v $(pwd):/src -w /src --entrypoint=/bin/sh -e NOPERM_GH_TOKEN python:3.11-alpine3.18

/src # apk add curl unzip
/src # curl -L -H "Authorization: token $NOPERM_GH_TOKEN" -o wheels.zip https://api.github.com/repos/ta
masfe/taplo/actions/artifacts/1847999815/zip
/src # unzip wheels.zip -d wheels

/src # pip install --no-index --find-links wheels/ taplo
  Looking in links: wheels/
  Processing ./wheels/taplo-0.9.3-py3-none-musllinux_1_1_x86_64.whl
  Installing collected packages: taplo
  Successfully installed taplo-0.9.3

/src # taplo format pyproject.toml
 INFO taplo:format_files:load_config: found configuration file path="taplo.toml"
 INFO taplo:format_files:collect_files: found files total=1 excluded=0 cwd="/src"

Thanks again for all your work on this, looking forward to working with the pre-commit hook. 🙏🏻