Closed nathanjmcdougall closed 1 month ago
+1 having a generic uv
pre-commit hook we can pass args to would also permit for additional flexibility in the future
Coming from PDM I'd also love to see an export hook. This would be especially nice since sbom generators can usually read requirements files but cannot read uv lock files yet.
What's the benefit of committing the requirements file to your repository instead of generating it on-demand?
Generating it on demand requires uv
to be installed. So if I am trying to run this against some sbom generators inside a container I either have to first install uv or generate the requirements.txt outside the container. Also stuff like heroku buildpacks nowadays only support requirements.txt etc…
I am not saying that not having this would be the end of the world, but for the transitional period till we have official cross-tool lockfiles via a PEP it will help with "legacy" tooling.
Elaborating on @apollo13's answer, some cloud platforms like PromptFlow or Azure Functions use requirements.txt to build your virtual environment server-side, so you have to generate it and commit to the repo. Using pre-commit hooks to automate that is a great way to make sure the lockfile and generated requirements.txt for downstream services that are not UV-aware are in sync.
I'd review a contribution adding this.
Since one cannot pass options to hooks (unless I am missing something in the docs) we'd have to go down a similar route as PDM does: https://pdm-project.org/latest/usage/advanced/#export-requirementstxt -- ie essentially you'd have to pass custom args to specify the file you want to generate. Or should we hardcode to requirements.txt (though I guess some people might have a problem with that)?
Perhaps hard-code a default but allow the user to over-ride it. For example, for cloud platforms like @stewartadam mentioned (which is my user-case), they always expect requirements.txt
at the root of the project. I'd expect it to be uncommon where both:
a) the requirements file(s?) cannot be dynamically generated, and
b) the requirements file shouldn't be ./requirements.txt
Make sense, but I fear we are blocked on https://github.com/astral-sh/uv/issues/7058 getting implemented.
Draft PR at #18 -- will test and finish once a new uv release is out with support for specifying the output.
Came here to contribute a PR for this now that export had an output option, but looks like @apollo13 is on it. PR looks great to me.
+1 having a generic
uv
pre-commit hook we can pass args to would also permit for additional flexibility in the future
I think you can use a local
repo to create generics like this:
- repo: local
hooks:
- id: uv-export
name: uv export requirements
entry: uv
args: [export, -o, requirements.txt, --no-hashes, -q]
language: system
pass_filenames: false
always_run: true
This looks exactly like what I'm looking for, let me give that a shot. Of course it'd be nice to have a upstream one that sets up uv
for you out of the box in pre-commit, but this is a good workaround in the interim.
Yep, it does violate one of the ideas of pre-commit which is installing tools, but does work. For example, I had to change the pre-commit.yml
github action to Install uv
and install python with uv
(instead of just - uses: actions/setup-python@v3
) so that that local hook could work.
Alternatively, you can do this:
- repo: local
hooks:
- id: uv-export
name: uv export requirements
entry: uv
args: [export, -o, requirements.txt, --no-hashes, -q]
additional_dependencies: [uv]
language: python
pass_filenames: false
always_run: true
So that pre-commit will automatically install uv
in a managed venv.
It seems sensible to me to support the new
uv export --format requirements-txt
interface as its own hook. For me, this will increasingly become the most common way to generate requirements.txt files rather than theuv pip compile
interface.