facelessuser / MarkdownPreview

Markdown preview and build plugin for Sublime Text https://facelessuser.github.io/MarkdownPreview/
Other
402 stars 53 forks source link

Preview command generates files but does not show in browser #170

Closed ArnaudBru closed 10 months ago

ArnaudBru commented 10 months ago

When I run the preview in browser command from SublimeText, I get a file not found error.

The file is created in /tmp but I cannot open it in my browser from there, this is likely due to the fact that Linux supports "mount namespaces" which allow different processes to have different views of what's mounted where (e.g. this is how containers such as Docker or LXC are implemented).

Firefox was installed through the snap system, which isolates each app in its own container with limited access to the real system – what Firefox sees as /tmp inside the container corresponds to /tmp/snap.firefox/tmp (or /tmp/snap-private-tmp/snap.firefox/tmp) outside the container.

This issue happens with Ubuntu 22.04 Firefox 116.0.3 (64-bit) Sublime Text Build 4152 MarkdownPreview v2.4.3

facelessuser commented 10 months ago

I am open to suggestions. If you know of a workable solution, a PR would be appreciated.

facelessuser commented 10 months ago

This is what we currently do. Linux seems to love to make this increasingly more difficult to abstract: https://github.com/facelessuser/MarkdownPreview/blob/master/browser.py#L60.

facelessuser commented 10 months ago

I guess this isn't a "browser opening" issue as much as it is a tmp file creation issue. As I have no way of knowing what specific tmp a browser can see or does not see, we'd have to make some tmp file relocation feature where the user can specify where they want their tmp to be so their browser can see it. I'm not sure I'm interested in jumping through hoops to try and automatically figure out what specific tmp we should use, so having the user specify it is probably going to be the compromise.

If anyone wants to tackle this, I'm happy to take a PR, otherwise, I'll put it on my list and see when I have time to update this.

facelessuser commented 10 months ago

@ArnaudBru I forgot we already abstracted temp files, try out this setting: https://github.com/facelessuser/MarkdownPreview/blob/master/MarkdownPreview.sublime-settings#L317

facelessuser commented 10 months ago

As it appears the temp folder can already be controlled, closing this as no change. If this is not sufficient feel free to reopen, but please provide details as to why it is not sufficient.

agnostic-apollo commented 8 months ago

For Ubuntu 22.04, where firefox is provided by snap, I added an entry in /etc/fstab for a bind mount of the /run/user/1000/home.tmp directory at ~/tmp. The mount is done with a systemd user unit. The /run/user/1000 directory is basically a tmpfs mount for the user 1000 that is automatically done by pam_systemd when user logs in (mount | grep tmpfs) and will be cleared automatically on reboot, so better to use it than using a real directory which will have to be cleared manually or some other way.

  1. Run sudo nano /etc/fstab add /run/user/1000/home.tmp /home/<user>/tmp none bind,noauto,user,exec,x-gvfs-hide 0 0 and save and exit (goodluck!). The <user> needs to be replaced by your username and replace 1000 if your uid is different (id -u). Check mount options info in its man page. The bind options is so that a bind mount is created. The noauto option so that mount is not automatically done on boot, check notes below. The user option so that root is not required to mount and it can be done by the user level systemd service. The exec option so that binaries can be executed, it needs to be explicitly specified as user option implies noexec by default. The x-gvfs-hide option so that mount is not listed in the file manager sidebar, check gvfs docs.

  2. Run mkdir -p ~/.config/systemd/user && nano ~/.config/systemd/user/tmp-mount.service and paste following and save and exit. The %U expands to user id and %h expands to home directory, check systemd specifiers.

[Unit]
Description=Mount ~/tmp directory

[Service]
ExecStart=/bin/bash -c 'mkdir -p /run/user/%U/home.tmp && /bin/mount %h/tmp'
ExecStop=/bin/umount %h/tmp
RemainAfterExit=yes

[Install]
WantedBy=default.target
  1. Enable the service with systemctl --user enable tmp-mount.service so that mount is automatically done on reboots.

  2. Start the service with systemctl --user start tmp-mount.service so that mount is done now without requiring a reboot. You can check service status with systemctl --user status tmp-mount.service and can unmount with systemctl --user stop tmp-mount.service if needed.

  3. Check mount with mount | grep /home and it should show something like following.

tmpfs on /home/<user>/tmp type tmpfs (rw,nosuid,nodev,relatime,size=3265312k,nr_inodes=816328,mode=700,uid=1000,gid=1000,inode64,user=<user>,x-gvfs-hide)
  1. Add "path_tempfile": "/home/<user>/tmp/MarkdownPreview" to MarkdownPreview settings. It doesn't expand environmental variables like $HOME, so will require hard coding the path.

Notes

Hopefully, this helps others.

Edit: Finally had to reboot my laptop and mounting with /etc/fstab automatically will not work and systemd user unit should be used instead.