electron-builder snap installer confusing home directory

旧时模样 提交于 2021-02-11 06:56:11

问题


This is a follow up of the previous question Defult home directory for snap installer using electron-builder, asked as requested there.

I'm currently building a electron app an building the installer like .deb and .snap with electron-builder. My app uses showSaveDialog/showOpenDialog to open/save file that opens up a nautilus (in ubuntu) like file explorer. Now if the app is installed using the .snap installer, the Home tab in the explorer pop-up points to the /home/user/snap/<app>/<revision>/ as can be seen in the picture below, not the actual directory we usually call home in linux (/home/username/). As the Home in this pop-up explorer window is not the actual user's home directory, it's getting confusing for users.
Is there a way to fix this?


回答1:


This seems to be a common problem that the GTK-based open/save dialogs have when run in the snap confinement.

There are the following bug reports sprinkled around the web:

  • https://bugs.launchpad.net/ubuntu/+source/chromium-browser/+bug/1848918
  • https://bugs.launchpad.net/ubuntu/+source/chromium-browser/+bug/1798450
  • https://forum.snapcraft.io/t/libreoffice-snap-home-folder-points-to-wrong-folder-on-open-dialog/6203/2
  • https://github.com/ubuntu/snapcraft-desktop-helpers/issues/167

A commenter in the last link above suggested to set the $G_HOME environment variable.

You can override this variable to affect the file chooser, but you'll need to do it early. In my test, it needs to be done before the app-ready event:

const os = require("os");
process.env.G_HOME = os.userInfo().homedir;

const {app, dialog} = require("electron");

app.on("ready", function() {
    console.log(process.env.G_HOME); // should print your actual home directory
    dialog.showOpenDialogSync();
});

Do this at your own risk; it is possible that there are other, potentially undesired effects of this change. I did not run this under snap confinement, but was able to change the directory the file chooser uses for the "Home" entry.



来源:https://stackoverflow.com/questions/65872077/electron-builder-snap-installer-confusing-home-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!