Emacs xwidgets is not broken anymore

The emacs package used to have support for xwidgets. However, it was discovered that the library necessary for this feature, webkit2gtk, above a certain version, would make Emacs crash. Because the maximum version which Emacs would accept to build with was below the version that nixpkgs has packaged for webkit2gtk, this feature has been disabled in nixpkgs.

However, recently upstream Emacs has re-enabled webkit2gtk above the threshold version, fixing the crashes. Hence, I think there is no reason to hold back the xwidgets feature. As a proof of concept, I have built the latest emacs (using the emacs overlay), with the xwidgets feature turned on and the broken flag overridden. Here is a screenshot of it, used with typst preview:

It works fine, one can click on the preview to go to its definition as one would if the preview were in a regular browser.

2 Likes

Hi, I am one of Nixpkgs Emacs maintainers and I am aware of that commit. Actually, it is me who tells you that commit in the matrix room.

I did plan to remove the xwidgets restriction from Nixpkgs. However, in my experiments, it did not seem to work: it just showed a blank screen after I evaluated (xwidget-webkit-browse-url "http://httpforever.com/"):

If it works for you, PR is welcome.

I plan to revisit removing the xwidgets restriction from Nixpkgs when Emacs 31 is released if no one does that before me.

2 Likes

Yes, that happened to me too. Pressing g, then RET seems to make it work. I’m not sure why it doesn’t work “out of the box”, but this shouldn’t be blocking for nixpkgs.

2 Likes

Pressing g, then RET

Oh, that does work! I’ll work on a Nixpkgs PR tomorrow.

BTW, how do you find this “trick”? I have never used xwidgets so maybe it is just a skill issue?

I have never used xwidgets before either, I just randomly interacted with Emacs until something happened (a tried and tested method) :slight_smile:

1 Like

I have further experimented with xwidgets and page loading, and arrived at the following conclusion: during the first 4/5 seconds, calls to xwidget-webkit-goto-uri (which is the low-level function that interfaces with the C library) won’t “go through”, in that nothing happens when they are called. Adding a 5 second timer works consistently for me:

(run-with-timer 5 nil (lambda () (xwidget-webkit-goto-uri xwidget url))

I have explained the situation on the Emacs devel mailing list, as I don’t really know how to debug the C side of this: I am not at all familiar with the C part of Emacs’ codebase, but by looking at the implementation of xwidget-webkit-goto-uri, I have the impression that it pretty much just forwards the call to the webkit gtk library, so if I had to guess I’d say the issue is in that library.

There is no mention of the “blank screen” issue in #80728 - 31.0.50; Re-enable xwidget for newer versions of webkit2gtk? - GNU bug report logs and that makes me wonder if that issue is Nixpkgs-specific.

Currently nixpkgs doesn’t seem to have the latest version of webkit gtk, maybe updating that would solve the issue?

I also asked the question on the matrix chan of webkit gtk (#webkitgtk:matrix.org if you want to join), but they don’t seem to have an answer yet either.

After discussing about it with the Emacs developer who worked on xwidgets (Dirk-Jan Binnema), it appears that the issue is opening the about:blank page on widget creation, on PGTK builds. Hence, the following patch works:

--- a/src/xwidget.c
+++ b/src/xwidget.c
@@ -362,8 +362,12 @@
 				"download-started",
 				G_CALLBACK (webkit_download_cb), xw);

+#if !defined HAVE_PGTK
+              /* when using pgtk, the about:blank workaround is not needed
+                 would in fact make the initial load fail.  */
 	      webkit_web_view_load_uri (WEBKIT_WEB_VIEW (xw->widget_osr),
 					"about:blank");
+#endif
 	      /* webkitgtk uses GSubprocess which sets sigaction causing
 		 Emacs to not catch SIGCHLD with its usual handle setup in
 		 'catch_child_signal'.  This resets the SIGCHLD sigaction.  */

I have tested it locally and it works for me. I’m unsure what I should do with this patch: Dirk said they didn’t apply this path upstream because they don’t know whether this will break with older webgtk versions. I see several possibilities:

  • I could open a PR to add this patch in nixpkgs, while waiting for upstream fix, but this might break some configurations other than mine, which I cannot test
  • I could add this patch somewhere in the documentation, so that if someone else has this issue they can easily fix it, but otherwise we don’t break anyone’s setup
  • I could wait for the upstream fix to happen, which should happen in the near future, as this is a known bug fix to a known bug.

@linj do you have insight on what might be the most useful thing to do?

It’s an upstream bug. When in doubt, don’t patch upstream bugs downstream if they’re not security relevant; nixpkgs is not the place to maintain upstream projects.

You could add it as a hint somewhere in the wiki, but I don’t think that’s actually useful; it’s an emacs bug and has nothing to do with nix/NixOS. Anyone else running into this should find your emails on the emacs mailing list, that’s where emacs bugs are detailed (or this thread via a search engine).

Personally I would just patch it downstream in my config if I wanted it fixed now, though tbh given you have an easy enough workaround it’s probably easier to just hit g for now, not worth having to recompile emacs on every update.

Maybe I should write somewhere the hacky workaround (pressing g + RET) then, that’s probably the most useful?

Or maybe it’s enough that this thread exists.

I’m happy that I have the choice, now. I guess I’ll use the patched version whenever I need to play around with xwidgets (as doing development with a bugged version is somewhat painful), and the workaround version for my everyday life.

After discussing about it with the Emacs developer who worked on xwidgets (Dirk-Jan Binnema)

To me, Emacs developers means people who have write access to Emacs git repo or authored many Emacs patches. I would consider Dirk-Jan Binnema an Emacs contributor, not an Emacs developer.

I’m unsure what I should do with this patch

Let’s give the discussion some more time to allow Emacs developers familiar with that part to chime in. After a fix is installed in upstream Emacs, we can consider cherry-picking the fix. In the meantime, this post can serve as some documentation. Of course, it is also helpful to add something to the wiki.

1 Like