Skip to content

feat(base): cache uv directory - #900

Open
carlcsaposs-canonical wants to merge 3 commits into
canonical:mainfrom
carlcsaposs-canonical:uv-cache
Open

feat(base): cache uv directory#900
carlcsaposs-canonical wants to merge 3 commits into
canonical:mainfrom
carlcsaposs-canonical:uv-cache

Conversation

@carlcsaposs-canonical

@carlcsaposs-canonical carlcsaposs-canonical commented Feb 6, 2026

Copy link
Copy Markdown

Based on #394

Fixes canonical/charmcraft#2461

uv's cache directory is located in the same parent directory as pip's cache directory

guest_cache_proc = executor.execute_run(
["bash", "-c", "echo -n ${XDG_CACHE_HOME:-${HOME}/.cache}"],

https://docs.astral.sh/uv/reference/storage/#cache-directory
https://pip.pypa.io/en/stable/topics/caching/#default-paths

@carlcsaposs-canonical

Copy link
Copy Markdown
Author

The test failures appear to be unrelated to the changes in this PR (I do not have access to retrigger the tests)


I built charmcraft using this branch of craft-providers & the uv caching appears to be working

ubuntu@dualla:~$ git clone https://github.com/carlcsaposs-canonical/charmcraft.git
Cloning into 'charmcraft'...
remote: Enumerating objects: 21395, done.
remote: Counting objects: 100% (707/707), done.
remote: Compressing objects: 100% (503/503), done.
remote: Total 21395 (delta 501), reused 205 (delta 204), pack-reused 20688 (from 4)
Receiving objects: 100% (21395/21395), 8.35 MiB | 9.48 MiB/s, done.
Resolving deltas: 100% (15338/15338), done.
ubuntu@dualla:~$ cd charmcraft/
ubuntu@dualla:~/charmcraft$ git checkout test-uv-cache 
branch 'test-uv-cache' set up to track 'origin/test-uv-cache'.
Switched to a new branch 'test-uv-cache'
ubuntu@dualla:~/charmcraft$ sudo snap install snapcraft --classic
2026-02-09T09:12:57Z INFO Waiting for automatic snapd restart...
snapcraft 8.13.2 from Canonical✓ installed
ubuntu@dualla:~/charmcraft$ snapcraft pack -v
[...]
snapcraft internal error: 1 validation error for Project
version
  Value should have at most 32 items after validation, not 33 [type=too_long, input_value='0.0.post3261+g0ddb3bcba.d20260209', input_type=str]
    For further information visit https://errors.pydantic.dev/2.12/v/too_long
Failed to run snapcraft in instance
Full execution log: '/home/ubuntu/.local/state/snapcraft/log/snapcraft-20260209-091339.692530.log'
ubuntu@dualla:~/charmcraft$ nano snap/snapcraft.yaml 
ubuntu@dualla:~/charmcraft$ git diff
diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
index 7219dcbe..ab9f5f6b 100644
--- a/snap/snapcraft.yaml
+++ b/snap/snapcraft.yaml
@@ -150,7 +150,7 @@ parts:
       sed -i 's|#!/bin/sh|#!/snap/charmcraft/current/bin/python3|' $CRAFT_PART_INSTALL/bin/craftctl
 
       version="$(uv pip show charmcraft | grep "Version:" | cut -d' ' -f2)"
-      /snap/snapcraft/current/libexec/snapcraft/craftctl set version="${version}"
+      /snap/snapcraft/current/libexec/snapcraft/craftctl set version="0.0" #"${version}"
       [ -n "$(echo $version | grep "post")" ] && grade=devel || grade=stable
       /snap/snapcraft/current/libexec/snapcraft/craftctl set grade="${grade}"
 
ubuntu@dualla:~/charmcraft$ snapcraft pack -v
[...]
Packed charmcraft_0.0_arm64.snap
ubuntu@dualla:~/charmcraft$ sudo snap install ./charmcraft_0.0_arm64.snap --dangerous --classic
charmcraft 0.0 installed
ubuntu@dualla:~/charmcraft$ cd
ubuntu@dualla:~$ mkdir foo
ubuntu@dualla:~$ cd foo
ubuntu@dualla:~/foo$ charmcraft init
Charmed operator package file and directory tree initialised.

Now edit the following package files to provide fundamental charm metadata
and other information:

charmcraft.yaml
src/charm.py
src/foo.py
README.md

To manage your charm's dependencies, use uv.

To migrate from the Charm plugin to the uv plugin, see:
https://documentation.ubuntu.com/charmcraft/stable/howto/migrate-plugins/charm-to-uv/

ubuntu@dualla:~/foo$ charmcraft pack
Packed foo_arm64.charm
ubuntu@dualla:~/foo$ ls ~/snap/charmcraft/common/cache/charmcraft/charmcraft-buildd-base-v7.1/BuilddBaseAlias.NOBLE/
pip  uv
ubuntu@dualla:~/foo$ ls ~/snap/charmcraft/common/cache/charmcraft/charmcraft-buildd-base-v7.1/BuilddBaseAlias.NOBLE/uv/
CACHEDIR.TAG  archive-v0  interpreter-v4  sdists-v9  wheels-v5

@carlcsaposs-canonical
carlcsaposs-canonical marked this pull request as ready for review February 9, 2026 09:55
@carlcsaposs-canonical
carlcsaposs-canonical requested a review from a team as a code owner February 9, 2026 09:55
carlcsaposs-canonical added a commit to carlcsaposs-canonical/craft-parts that referenced this pull request Feb 9, 2026
Companion to canonical/craft-providers#900

When the cache dir is mounted from the LXD host, hardlinking from the global cache (uv's default behavior) is not possible

e.g.
```
:: warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
::          If the cache and target directories are on different filesystems, hardlinking may not be supported.
::          If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
```
carlcsaposs-canonical added a commit to carlcsaposs-canonical/craft-parts that referenced this pull request Feb 9, 2026
Companion to canonical/craft-providers#900

When the cache dir is mounted from the LXD host, hardlinking from the global cache (uv's default behavior) is not possible

e.g.
```
:: warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
::          If the cache and target directories are on different filesystems, hardlinking may not be supported.
::          If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
```
@bepri

bepri commented Feb 13, 2026

Copy link
Copy Markdown
Member

Lint error is fixed in #910

@bepri

bepri commented Feb 13, 2026

Copy link
Copy Markdown
Member

Also, the test failures involving "tracemalloc" are known flaky tests we haven't been able to figure out, so don't worry about those :)

@bepri
bepri requested a review from a team February 13, 2026 23:22
carlcsaposs-canonical added a commit to carlcsaposs-canonical/craft-parts that referenced this pull request Feb 18, 2026
Companion to canonical/craft-providers#900

When the cache dir is mounted from the LXD host, hardlinking from the global cache (uv's default behavior) is not possible

e.g.
```
:: warning: Failed to hardlink files; falling back to full copy. This may lead to degraded performance.
::          If the cache and target directories are on different filesystems, hardlinking may not be supported.
::          If this is intentional, set `export UV_LINK_MODE=copy` or use `--link-mode=copy` to suppress this warning.
```
@lengau

lengau commented Feb 26, 2026

Copy link
Copy Markdown
Collaborator

We're working on a new way to share the user cache (that'll share the entire .cache directory rather than directories for individual tools). That will be opt-in though.

@astrojuanlu

Copy link
Copy Markdown

Hi folks! Any blockers for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uv plugin doesn't cache wheels to LXC host

4 participants