BuildPackageNotFound for valid packages when snap uses core24 on Ubuntu 26.04 (libapt ABI mismatch)
Metadata
Current evaluation
No evaluation has been recorded for this issue yet.
Issue body
## Summary
When a snap built on `core24` (which bundles `libapt-pkg.so.6.0` / python-apt 2.7.x) runs in `--destructive-mode` on **Ubuntu 26.04**, craft-parts raises `BuildPackageNotFound` for packages that **do** exist on that system.
The root cause is an **ABI mismatch**: the snap's python-apt was compiled against `libapt-pkg.so.6.0` (apt 2.x, Ubuntu 24.04), but Ubuntu 26.04 ships **apt 3.x** (`libapt-pkg.so.7.0`). The snap cannot correctly read the host's apt package lists.
## Observed error
```
2026-04-21 21:26:37.718 Cannot find package listed in 'build-packages': python3-dev
craft_parts.packages.errors.BuildPackageNotFound: Cannot find package listed in 'build-packages': python3-dev
```
This occurs even though `python3-dev` is a valid package on Ubuntu 26.04. The same error occurs for `python3-venv`.
## Steps to reproduce
1. Build a snap using `base: core24`
2. Install and run that snap on Ubuntu 26.04 (which has apt 3.x / libapt-pkg.so.7.0)
3. Run `charmcraft pack --destructive-mode` on a charm that uses the `python` or `uv` plugin (both of which add `python3-dev` to `build-packages`)
4. Observe `BuildPackageNotFound: python3-dev`
## Root cause analysis
### ABI mismatch
```
# Snap's apt_pkg extension module links against:
libapt-pkg.so.6.0 => /snap/core24/current/lib/x86_64-linux-gnu/libapt-pkg.so.6.0
# Ubuntu 26.04 system libapt:
/usr/lib/x86_64-linux-gnu/libapt-pkg.so.7.0 (apt 3.2.0)
```
The python-apt bundled in the snap (version `2.7.7+ubuntu5`) was compiled against core24's `libapt-pkg.so.6.0`. On Ubuntu 26.04, `/snap/core24/current/lib/x86_64-linux-gnu/libapt-pkg.so.6.0` resolves to the 24.04 library.
### configure_apt sets Dir to a non-existent path
In `AptCache.configure_apt()`, when running inside a snap, the code sets:
```python
apt_dir = os.path.join(snap_dir, "usr", "lib", "apt")
apt_pkg.config.set("Dir", apt_dir)
```
However, the charmcraft snap does **not** include `usr/lib/apt`. This path does not exist, so apt's method/solver resolution breaks. The code then sets `Dir::State` to `/var/lib/apt` to point at the host's package lists, but because `Dir` is pointing at a missing path, the relative paths for `Dir::State::Lists` may not resolve correctly.
### apt binary cache format version mismatch
Ubuntu 26.04's `/var/cache/apt/pkgcache.bin` reports **cache data version 16.0**. The apt 2.x library (bundled in the snap via core24) likely supports an older cache version. Even if the cache can be opened, package lookups may silently fail or return no results.
### Evidence from charmcraft CI
The `tests/spread/ubuntu-26.04/charm:platforms` and `tests/spread/ubuntu-26.04/charm:uv` spread tests have been failing **consistently** on every CI run for months (30+ consecutive failures as of 2026-04-22). The same error appears each time. Tests on 18.04, 20.04, 24.04, and 25.10 all pass.
## Workaround
None currently available without rebuilding the snap against `core26` (which would bundle `libapt-pkg.so.7.0`).
## Suggested fix
craft-parts should handle the case where the snap's bundled python-apt cannot read the host's apt cache. Options:
1. **Document the core requirement**: craft-parts should document that the application snap's base must match the host Ubuntu series for `install_packages` / `build-packages` to work. Raise a clear error with a helpful message instead of `BuildPackageNotFound`.
2. **Detect apt ABI mismatch**: Before attempting to use python-apt's cache, check that the `libapt-pkg.so` ABI version used by the bundled python-apt matches the host's libapt. If not, fall back to shelling out to `apt-cache show <package>` or skip the pre-check entirely.
3. **Don't fail on cache-open errors**: If `apt.cache.Cache(rootdir="/")` fails or returns an empty cache (zero packages), treat it as "cache unavailable" rather than "package not found", and defer to `apt-get install` to produce the real error.
## Environment
- Host: Ubuntu 26.04 (apt 3.2.0, libapt-pkg.so.7.0)
- Snap base: core24 (bundles libapt-pkg.so.6.0, python-apt 2.7.7+ubuntu5)
- craft-parts version: 2.29.0
- charmcraft version: 4.2.x (snap)
- Failed tasks: `tests/spread/ubuntu-26.04/charm:platforms`, `tests/spread/ubuntu-26.04/charm:uv`
- CI run demonstrating failures: https://github.com/canonical/charmcraft/actions/runs/24746125338/job/72401080653
Evaluation history
No evaluation history available.