libEGL warning: MESA-LOADER: failed to open DRI library

I created a classic core20 snap (https://github.com/christianrauch/qtcreator-ros-snap/blob/master/snap/snapcraft.yaml) that runs without issues on Ubuntu 18.04 and 20.04. On the Ubuntu 22.04 beta, however, I am getting a warning message:

libEGL warning: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)

and indeed the snap is not working as expected anymore. The search path is malformed. It contains \$${ORIGIN} which is not a valid path. I assume the variable ORIGIN is supposed to be set during the build or at runtime.

Searching in the forum revealed similar issues:

  1. libGL error: MESA-LOADER (solved via desktop-launch)
  2. Issue with GL after upgrading to 20.10 (solved via no-patchelf)
  3. OpenGL error: MESA-LOADER fails (solved via LIBGL_DRIVERS_PATH)

But the solutions presented there did not work for me:

The “desktop-launch” solution did not work for me since command: bin/desktop-launch $SNAP/bin/qtcreator fails during the build with an error message that the command “bin/desktop-launch” does not exist. Also, the “snapcraft-desktop-helpers” are phased out by extensions and seem to not be well supported anymore (no changes in the last 2~4 years).

Using no-patchelf causes another library not to be found when stating the snap. I suspect that patchelf is changing the library search path in another part that it is not supposed to do.

If I set LIBGL_DRIVERS_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri my snap crashes right from the start. The variable SNAPCRAFT_ARCH_TRIPLET is not set inside the snap. When I open a shell inside the snap and check the variable LIBGL_DRIVERS_PATH, it contains the part without whatever is supposed to be stored in $SNAPCRAFT_ARCH_TRIPLET.

The only workaround I found so far is to hard code LIBGL_DRIVERS_PATH to $SNAP/usr/lib/x86_64-linux-gnu/dri (i.e. hard code SNAPCRAFT_ARCH_TRIPLET to x86_64-linux-gnu). Edit: This does not seem to work anymore. Running LIBGL_DRIVERS_PATH=/snap/qtcreator-ros/current/usr/lib/x86_64-linux-gnu/dri qtcreator-ros crashes the snap somewhere inside $SNAP/usr/lib/x86_64-linux-gnu/dri/iris_dri.so as reported by LIBGL_DRIVERS_PATH=$SNAP/usr/lib/x86_64-linux-gnu/dri gdb --ex=r --args $SNAP/bin/qtcreator from within a snap shell (snap run --shell qtcreator-ros).

Q1: Can someone explain to me why this is OpenGL error is happening? I thought snaps are supposed to work across different distributions so I am quite surprised that this even breaks between two consecutive Ubuntu versions.

Q2: How is setting LIBGL_DRIVERS_PATH: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/dri supposed to work if SNAPCRAFT_ARCH_TRIPLET is not set? How do I make sure that mesa find all its libraries?

The solution is a combination of the mesa installation with and without no-patchelf, and setting the LIBGL_DRIVERS_PATH: OpenGL broken in classic core20 snap on 22.04.