diff --git a/CLAUDE.md b/CLAUDE.md index 1c10d43..975cd9d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -45,7 +45,11 @@ Everything happens in `main()` in `main.go`, in a straight-line pipeline: ### Temp file / Snap sandboxing (`getTempDir`, `isSnap`) -Output location resolution order: `MDVIEW_DIR` env var → (if running as a Snap, per `SNAP_USER_COMMON` env var) `~/mdview-temp` → OS default temp dir. `MDVIEW_DIR` and `~/mdview-temp` are created with `MkdirAll`. Before writing a temp file, `removeStaleTempFiles` deletes generated `mdview<32 hex>.html` files older than 24 hours from that directory (the browser reads the new file after mdview exits, so mdview cannot delete it right away). This exists because Snap-sandboxed browsers (e.g. Snap Firefox) can't read from `/tmp`, only from the user's home directory — `check()` writes errors to stderr, names the failing path, and prints a Snap-specific hint for permission and not-found errors for this reason. +Output location resolution order: `MDVIEW_DIR` env var → (if running as a Snap, per `SNAP_USER_COMMON` env var) `~/mdview-temp` (the home directory comes from `SNAP_REAL_HOME`, because snapd remaps `HOME` to the snap's private data directory) → OS default temp dir. `MDVIEW_DIR` and `~/mdview-temp` are created with `MkdirAll`. Before writing a temp file, `removeStaleTempFiles` deletes generated `mdview<32 hex>.html` files older than 24 hours from that directory (the browser reads the new file after mdview exits, so mdview cannot delete it right away). This exists because Snap-sandboxed browsers (e.g. Snap Firefox) can't read from `/tmp`, only from the user's home directory — `check()` writes errors to stderr, names the failing path, and prints a Snap-specific hint for permission and not-found errors for this reason. + +The snap (`snap/snapcraft.yaml`, `base: core24`, `platforms:` instead of `architectures:`) plugs `home` and +`desktop`. `desktop` (auto-connected) lets `xdg-open` launch the default browser; it replaced the transitional +`unity7` plug. ### Vendored `mermaid.min.js` diff --git a/main.go b/main.go index f673f91..49c1d12 100644 --- a/main.go +++ b/main.go @@ -243,7 +243,12 @@ func getTempDir() string { } if isSnap() { - var tmpdir = filepath.Join(os.Getenv("HOME"), "mdview-temp") + // snapd points HOME at the snap's private data directory, so use the real home directory + home := os.Getenv("SNAP_REAL_HOME") + if home == "" { + home = os.Getenv("HOME") + } + var tmpdir = filepath.Join(home, "mdview-temp") check(os.MkdirAll(tmpdir, 0700)) return tmpdir } diff --git a/main_test.go b/main_test.go index a807292..b5db254 100644 --- a/main_test.go +++ b/main_test.go @@ -325,6 +325,19 @@ func TestGetTempDir(t *testing.T) { home := t.TempDir() t.Setenv("MDVIEW_DIR", "") t.Setenv("SNAP_USER_COMMON", "/snap/common") + t.Setenv("HOME", filepath.Join(home, "snap", "mdview", "1")) + t.Setenv("SNAP_REAL_HOME", home) + want := filepath.Join(home, "mdview-temp") + if got := getTempDir(); got != want { + t.Fatalf("getTempDir = %q, want %q", got, want) + } + }) + + t.Run("snap without SNAP_REAL_HOME falls back to HOME", func(t *testing.T) { + home := t.TempDir() + t.Setenv("MDVIEW_DIR", "") + t.Setenv("SNAP_USER_COMMON", "/snap/common") + t.Setenv("SNAP_REAL_HOME", "") t.Setenv("HOME", home) want := filepath.Join(home, "mdview-temp") if got := getTempDir(); got != want { diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index ace6158..ad677b7 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -7,13 +7,11 @@ description: | Type `mdview --help` for options. https://github.com/mapitman/mdview grade: stable confinement: strict -base: core22 +base: core24 apps: mdview: command: bin/mdview - plugs: [home,unity7] - environment: - HOME: /home/$USER + plugs: [home, desktop] adopt-info: mdview parts: mdview: @@ -29,10 +27,7 @@ parts: # - libblkid-dev build-snaps: - go -architectures: - - build-on: [amd64] - build-for: [amd64] - - build-on: [arm64] - build-for: [arm64] - - build-on: [armhf] - build-for: [armhf] +platforms: + amd64: + arm64: + armhf: