Edit: the maintainer of the emacs snap asked me to raise an issue, [which I've done](https://github.com/alexmurray/emacs-snap/issues/36), and he'll look into it.
**OS**: Ubuntu 20.04.4 LTS, Gnome 3.36.8
**Emacs**: GNU Emacs 28.1 (build 2, x86\_64-pc-linux-gnu, GTK+ Version 3.24.20, cairo version 1.16.0) of 2022-04-06; installed from the current stable snap package
**Firefox**: 99.0 (edit for additional info: Firefox is installed via apt not snap, Chromium (see later) is installed via snap)
**To reproduce in emacs -q**: M-x browse-url-firefox RET (url)
(I’m using the wordle url to test but any url I put in place of my placeholder (url) in that command does the same).
**Expected behaviour**: the url is opened in my already running instance of firefox as a new tab or if there isn’t one then it runs a new instance of firefox that uses my profile.
**Actual behaviour**: the url is always opened in a new instance of firefox, this looks to the OS as if it is an Emacs window (the application bar shows me two Emacs windows not one Emacs window and one firefox window), this new instance of firefox doesn’t appear to be running my profile (I’m not logged in anywhere). Opening a second link will open it within this not-right instance of firefox.
The problem started after I upgraded both emacs and firefox last week (within a few hours and I don’t think I opened any links in between).
I’ve tried googling and all I can find is: [https://emacs.stackexchange.com/questions/71275/how-to-open-firefox-with-default-bookmarks-and-add-ons-profile-when-clicking-url](https://emacs.stackexchange.com/questions/71275/how-to-open-firefox-with-default-bookmarks-and-add-ons-profile-when-clicking-url) which I think is the same problem conceptualised a little differently. I don’t want to downgrade to Emacs 26.3 to fix it tho! Using the comments on this I did a little more troubleshooting.
If I open a terminal outside of emacs (just a normal terminal) and do ‘xdg-open (url)’ then I get the expected behaviour. In emacs if I do ‘M-! xdg-open (url)’ then I get the buggy behaviour. Which I think means the problem is within emacs and not something just to do with firefox.
(Edit: It was suggested in comments it might be my firefox profile that was the problem. So I logged into my machine as a different user to test this (so the exact same software stack but none of my customisations including not my firefox profile). I get the same buggy behaviour with browse-url-firefox as described above, so it's not my firefox profile.)
I looked at the help for process-environment as suggested in the comments on stack exchange, but I’m not entirely sure what I should be looking for in the long list of variables. I can see that /usr/bin is in the path, and ‘which firefox’ tells me /usr/bin/firefox (both from the terminal and run as a shell command using M-! from within Emacs). I can see it has my home directory set properly, both as HOME and as SNAP\_REAL\_HOME, I also noticed there’s a whole bunch of things to do with snap.
Poking around at browse-url to see if I could get a way to reproduce it without relying on firefox being the default browser I found there are specific commands for different browsers, and as I also have chromium installed I checked the behaviour of browse-url-chromium. I get the expected behaviour – i.e. the url is opened in the already running instance of chromium if I have one, and if a new instance is opened (because I don’t have one running) then the OS sees it as a chromium window not an emacs window and appears to be using my profile.
Writing this post I also discovered that if I have opened a new not-right instance of firefox using browse-url-firefox from emacs -q then following a link from my normal emacs opens as a new tab in that not-right firefox rather than opening yet another new instance of firefox.
I’m a bit stuck now. It’s clearly not a behaviour that many people are seeing, otherwise I’m sure I’d’ve found more using google (or maybe my search terms are just bad). It “feels” like some environment variable is wrong but I don’t know if I just need to set something (nor what I need to set), and if that’s not the case I’m not sure if it should be raised as a bug in emacs or a bug in the snap package. Or maybe something completely different I haven’t thought of!
Can anyone help?