Can't run tutorial on Alpine Linux when `flatpak` is installed but not `flatpak-libs`
Closed this issue · 3 comments
Describe the bug
following along with the beeware tutorial on my alpine desktop, i found that running briefcase new failed with a library error. this is not uncommon on a musl system; and is something i can usually fix easily. here it was a failure to load libflatpak.so.0. i fixed it by installing flatpak-libs, so it turns out there's not really an issue with library linkage (like such an error on this distro usually indicates). but before i found this out, during my attempt to produce a minimal reproducible example, i found that running the example in a container without flatpak at all worked!
Steps to reproduce
the container is defined in the following Containerfile:
FROM alpine:latest
RUN apk update && apk add python3 py3-pip gcc python3-dev musl-dev linux-headers git cairo-dev gobject-introspection-dev gtk4.0-dev font-dejavu font-noto font-noto-emoji
RUN mkdir /beeware && cd /beeware && python3 -m venv ./beeware-venv && . ./beeware-venv/bin/activate && pip install --upgrade pip && pip install briefcase && printf 'Containerized Hello World\nhelloworldinabox\ncom.example\nContainerized Hello World\n"Hello World" in a neat little box\nLiz\nelizabeth@example.com\nhttps://example.com/helloworld\n1\n1\n' | briefcase new
WORKDIR /beeware/helloworldinabox
CMD env DISPLAY=:0 ../beeware-venv/bin/briefcase devplace this file in an empty directory and run something like podman build -t beewaretutorial . && podman run -it --rm -v /tmp/.X11-unix/:/tmp/.X11-unix/ --device /dev/dri beewaretutorial to see the normal behaviour
now add RUN apk add flatpak (but not flatpak-libs) before the CMD line to produce the error
Expected behavior
i'd like flatpak detection to check for libflatpak.so.0 before attempting to load it, and to be able to disable the use of flatpak with a flag or environment variable
Screenshots
No response
Environment
- Operating System: Alpine Linux 3.22.0
- Python version: 3.12.11
- Software versions:
- Briefcase: 0.3.23
- Toga: 0.5.1
- kernel: 6.15.0
- sway 1.10.1
- xwayland 24.1.8
- gtk 4.18.6
Logs
Installing dev requirements... done
[helloworldinabox] Starting in dev mode...
===========================================================================
** (__main__.py:581): WARNING **: 01:22:58.499: Failed to load shared library 'libflatpak.so.0' referenced by the typelib: Error loading shared library libflatpak.so.0: No such file or directory
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "<frozen runpy>", line 226, in run_module
File "<frozen runpy>", line 98, in _run_module_code
File "<frozen runpy>", line 88, in _run_code
File "/beeware/helloworldinabox/src/helloworldinabox/__main__.py", line 4, in <module>
main().main_loop()
^^^^^^
File "/beeware/helloworldinabox/src/helloworldinabox/app.py", line 26, in main
return ContainerizedHelloWorld()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/beeware/beeware-venv/lib/python3.12/site-packages/toga/app.py", line 285, in __init__
self.factory = get_platform_factory()
^^^^^^^^^^^^^^^^^^^^^^
File "/beeware/beeware-venv/lib/python3.12/site-packages/toga/platform.py", line 120, in get_platform_factory
factory = importlib.import_module(f"{backend.value}.factory")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 999, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/beeware/beeware-venv/lib/python3.12/site-packages/toga_gtk/factory.py", line 7, in <module>
from .hardware.location import Location
File "/beeware/beeware-venv/lib/python3.12/site-packages/toga_gtk/hardware/location.py", line 52, in <module>
(Flatpak.PortalError.quark(), Flatpak.PortalError.NOT_ALLOWED),
^^^^^^^^^^^^^^^^^^^^^^^^^^^
gi.repository.GLib.GError: g-invoke-error-quark: Could not locate flatpak_portal_error_quark: Error loading shared library libflatpak.so.0: No such file or directory (1)
Additional context
this looks like it might really be a bug in toga, but i wrote it up here because i found it running briefcase dev for the beeware tutorial. i'm not sure where a flag or env var would be appropriately set and/or checked, either. i haven't had a chance to dive into the source for either project just yet; sorry if this is the wrong place!
If it's possible to install the Flatpak typelib without the library that it refers to, then I'd say that's a bug in the Alpine Linux packaging structure. However, we could still add a workaround if it affects a large number of users.
The error isn't being raised by Briefcase - in briefcase dev, there won't be any Briefcase-based check of Flatpak libraries, because Flatpak isn't a default packaging environment. This error is being raised by Toga trying to determine if it is running in a sandboxed environment. It's failing because of the following (slightly simplified) code:
import gi
try:
gi.require_version("Flatpak", "1.0")
from gi.repository import Flatpak # noqa: F401
errors = (Flatpak.PortalError.quark(), Flatpak.PortalError.NOT_ALLOWED)
except (ImportError, ValueError): # pragma: no cover
pass
The errors = ... line is failing because the symbols can't be loaded.
This occurs because it's running in an environment where the flatpak libraries are available and can be loaded by girepository, but the environment itself isn't sandboxed.
This indicates the "sandbox identification" code in Toga isn't quite right. We need to improve the mechanism for identifying if we're in a Flatpak sandbox that isn't based entirely upon the presence of girepository bindings.
I'll migrate this issue to Toga on that basis.
@mhsmith i think you're right, this is a bug with alpine. i've opened an issue with them https://gitlab.alpinelinux.org/alpine/aports/-/issues/17345
removing the typelib file causes the tutorial app to run normally with briefcase dev. thanks for your time :)