unoplatform/uno · error · NullReferenceException

Couldn't find a field named _libraries in GLibrary

Error message

Couldn't find a field named _libraries in GLibrary

What it means

Same reflection patch in X11NativeWebView: after locating the GLibrary type it fetches the private static field '_libraries' via reflection and throws NullReferenceException if that field name no longer exists. The whole block is monkey-patching WebKitGtk#'s internal library-handle cache to substitute the 4.1 handle for the 4.0 one. A WebKitGtkSharp internals rename of _libraries breaks the patch deterministically.

Source

Thrown at src/AddIns/Uno.UI.WebView.Skia.X11/X11NativeWebView.cs:102

			// webkit2gtk-4.1 adds support for exposing libsoup objects, most importantly in webkit_uri_request_get_http_headers.
			// Gtk# by default loads libwebkit2gtk-4.0 (+ libsoup-2.0), but we want libwebkit2gtk-4.1 (+ libsoup-3.0), so what
			// we do is that before WebKitGtk# makes its dlopen calls, we load libwebkit2gtk-4.1 and put it where the handle to
			// libwebkit2gtk-4.0 will be expected to be, so that when WebKitGtk# attempts to dlopen(libwebkit2gtk-4.0), it will
			// find the handle already there and will just use it instead.
			if (!NativeLibrary.TryLoad("libwebkit2gtk-4.1.so", typeof(X11NativeWebView).Assembly, DllImportSearchPath.UserDirectories, out var webkitgtk41handle))
			{
				if (typeof(X11NativeWebView).Log().IsEnabled(LogLevel.Error))
				{
					typeof(X11NativeWebView).Log().Error($"libwebkit2gtk-4.1 was not found. Attempting to call {nameof(ProcessNavigation)} with an {nameof(HttpRequestMessage)} instance will crash the process.");
				}
			}
			else
			{
				Assembly assembly = Assembly.Load("WebKitGtkSharp");
				Type glibraryType = assembly.GetType("GLibrary") ?? throw new NullReferenceException("Couldn't find GLibrary in WebKitGtkSharp");
				Type libraryType = assembly.GetType("Library") ?? throw new NullReferenceException("Couldn't find Library in WebKitGtkSharp");
				object webkitLibraryEnum = Enum.ToObject(libraryType, 11);
				FieldInfo field = glibraryType.GetField("_libraries", BindingFlags.NonPublic | BindingFlags.Static) ?? throw new NullReferenceException("Couldn't find a field named _libraries in GLibrary");
				object libraries = field.GetValue(null) ?? throw new NullReferenceException("Couldn't read the static field named _libraries in GLibrary");
				var setItemMethod = libraries.GetType().GetMethod("set_Item") ?? throw new NullReferenceException("Couldn't find a method named set_Item in _libraries");
				setItemMethod.Invoke(libraries, [webkitLibraryEnum, webkitgtk41handle]);
				_usingWebKit2Gtk41 = true;
			}

			if (!WebKit.Global.IsSupported)
			{
				_initException = new PlatformNotSupportedException("libwebkit2gtk-4.0 is not found. Make sure that WebKit2GTK 4.0 and GTK 3 are installed. See https://aka.platform.uno/webview2 for more information");
				return;
			}

			GLib.ExceptionManager.UnhandledException += args =>
			{
				if (typeof(X11NativeWebView).Log().IsEnabled(LogLevel.Error))
				{
					typeof(X11NativeWebView).Log().Error("GLib exception", args.ExceptionObject as Exception);
				}

View on GitHub (pinned to 0418340488)

Solutions

  1. Pin the WebKitGtkSharp version whose internals match this reflection code (the version referenced by the add-in at the time it was written).
  2. Update the reflection field name '_libraries' in X11NativeWebView to match the current WebKitGtkSharp source.
  3. As a fallback path, let the code skip the 4.1 patch and rely on the libwebkit2gtk-4.0 path (only viable if 4.0 is installed and IsSupported is true).
Defensive patterns

Strategy: validation

Validate before calling

var t = asm.GetType("GLibrary");
bool fieldPresent = t?.GetField("_libraries", BindingFlags.NonPublic | BindingFlags.Static) is not null;

Type guard

static bool GLibraryHasLibrariesField(Assembly asm) => asm.GetType("GLibrary")?.GetField("_libraries", BindingFlags.NonPublic | BindingFlags.Static) is not null;

Prevention

When it happens

Trigger: GLibrary type resolves (so [44] didn't throw) but the private static field '_libraries' is missing — a WebKitGtkSharp version where the cache field was renamed or its backing changed.

Common situations: Bumping WebKitGtkSharp across a release that refactored its internal library registry; running against a distro-modified WebKitGtk# build.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/c12b8361c348d51e. Report an issue: GitHub.