Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pdm to uv for faster dependency management #347

Merged
merged 9 commits into from
Nov 29, 2024
Merged

Conversation

Pradip-p
Copy link
Collaborator

Replaced pdm with uv for faster dependency management. uv is written in Rust, making it much faster for locking dependencies and managing Python versions.

Changes:

  • Switched all pdm usage to uv.
  • Refined Dockerfile for uv compatibility.
  • Added pre-commit hook to auto-update the lockfile.

Bonus:

Pre-commit hook ensures lockfile updates automatically.

Reference:

Inspired by [fmtm PR #1891](hotosm/fmtm#1891).

@spwoodcock @nrjadkry @nischalstha9 , I've updated the project to use uv as the package manager. Could you please review the changes.

@github-actions github-actions bot added enhancement New feature or request backend Related to backend code devops Related to deployment or configuration labels Nov 21, 2024
WORKDIR /project

# Install uv
COPY --from=ghcr.io/astral-sh/uv:0.5.2 /uv /bin/uv
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this definitely work? I'm sure you tested it and I just didn't realise this syntax was possible 😅

But I thought that --from and external source never used to be an option!

That's why I used this pattern:

FROM ghcr.io/astral-sh/uv:0.5.2 as uv-image
...
COPY --from=uv-image /uv /bin/uv

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thus must have been added in a recent docker version
https://docs.docker.com/build/building/multi-stage/#use-an-external-image-as-a-stage

# Install dependencies and project into the local packages directory
RUN pdm install --check --prod --no-editable
# Sync the project again using uv, ensuring all project dependencies are installed
RUN --mount=type=cache,target=/root/.cache/uv \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for the double sync?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for the double sync?

RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    uv sync --frozen --no-install-project

# Sync the project again using uv, ensuring all project dependencies are installed
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync

I found, the first uv sync with --no-install-project caches dependencies, while the second installs the project after copying the code. This speeds up builds. Details: [link].

Copy link
Member

@spwoodcock spwoodcock Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the idea!

But the code is not copied in between the two run commands, meaning this extra step doesn't really do anything 😆

(You could probably remove the second sync with no consequence)

Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice change! 🎉

@Pradip-p Pradip-p self-assigned this Nov 22, 2024
Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test out removing that extra sync stage to see if it's needed, otherwise all good 👍

@Pradip-p Pradip-p marked this pull request as ready for review November 25, 2024 13:42
@nrjadkry nrjadkry merged commit ffd14c6 into develop Nov 29, 2024
2 checks passed
@spwoodcock spwoodcock deleted the feat/uv-setup branch November 29, 2024 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend Related to backend code devops Related to deployment or configuration enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants