Skip to content

Commit

Permalink
fix: make dpkg/apt aware of installed python packages
Browse files Browse the repository at this point in the history
This fixes an issue where calling "craftctl" in a scriptlet would fail with a
ModuleNotFoundError for craft_parts. This happened when the snapcraft project
included build-packages that pulled in libpython3.10-minimal.

The reason that this failed is because in Rockcraft the `python` plugin (used
to build the `snapcraft` part) works by adding a custom "sitecustomize.py" in
`/usr/lib/python3.10/` to add the part's Python libraries to the Python path.
This is fine unless the `libpython3.10-minimal` package is explicitly installed
later on a container created from the rock, because that package has its own
sitecustomize.py file which overwrites Rockcraft's.

The solution is to make apt and dpkg aware that all of these Python-related
packages are already installed in the rock, by adding the dependency explicitly
as an `overlay-package`. This also has the extra beneficial side-effect of
improving installation of build-packages because dpkg/apt are aware of the
installed packages and don't have to download and re-install them unnecessarily.

Fixes #33.
  • Loading branch information
tigarmo committed May 9, 2024
1 parent 61584d6 commit 182b5ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rockcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ parts:
source-tag: ${CRAFT_PROJECT_VERSION}
stage-packages:
- python3-venv
overlay-packages:
# Note: this declaration seems redundant but it's here to ensure that the
# Apt installation inside the rock is aware that these Python packages
# (python3-venv and its dependencies) are already installed. Otherwise,
# installing them (as a build-package in a snapcraft.yaml) would clobber
# the sitecustomize.py added by rockcraft.
- python3-venv
python-packages:
- wheel
- pip
Expand Down
7 changes: 7 additions & 0 deletions tests/spread/general/craftctl/snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ build-base: SNAPCRAFT_BUILD_BASE
parts:
hello:
plugin: nil
build-packages:
# Note: We add this package explicitly here to make sure that the
# declaration of Python build packages does not clobber the existing
# Python installation (with Snapcraft libraries).
- python3-venv
override-pull: |
echo -e "#!/usr/bin/env bash\necho hello" > hello.sh
chmod +x hello.sh
craftctl get grade | grep devel
craftctl set version="22"
craftctl set grade=stable
craftctl default
override-build: |
craftctl get version | grep 22
craftctl get grade | grep stable
craftctl default
echo "This is the build step"
cp hello.sh "$CRAFT_PART_INSTALL"/

0 comments on commit 182b5ce

Please sign in to comment.