From 71813fdda5e6230e31e657df77e84aff805c1230 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 5 Aug 2025 14:03:30 +0200 Subject: [PATCH] Create a user-specific temp dir to avoid permission problems on multi-user machines In 3a9dbf7341f0 we created a global /tmp/lazygit/ folder that contains the temp directories of each running instance, to avoid polluting /tmp with multiple folders. The problem with that approach was that the folder was created with 700 permissions, so if multiple users were using lazygit on the same machine (e.g. a server), all users except the first one would get fatal errors on startup. Fix this by creating temp folders containing the user's uid. --- pkg/app/entry_point.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/pkg/app/entry_point.go b/pkg/app/entry_point.go index 2b683707c..6fb64c5ff 100644 --- a/pkg/app/entry_point.go +++ b/pkg/app/entry_point.go @@ -8,6 +8,7 @@ import ( _ "net/http/pprof" "os" "os/exec" + "os/user" "path/filepath" "runtime" "runtime/debug" @@ -124,11 +125,7 @@ func Start(buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTes os.Exit(0) } - tmpDirBase := filepath.Join(os.TempDir(), "lazygit") - if err := os.MkdirAll(tmpDirBase, 0o700); err != nil { - log.Fatal(err.Error()) - } - tempDir, err := os.MkdirTemp(tmpDirBase, "") + tempDir, err := os.MkdirTemp(getTempDirBase(), "lazygit-*") if err != nil { log.Fatal(err.Error()) } @@ -314,3 +311,19 @@ func getGitVersionInfo() string { gitVersion := strings.Trim(strings.TrimPrefix(string(stdout), "git version "), " \r\n") return gitVersion } + +func getTempDirBase() string { + tempDir := os.TempDir() + + user, err := user.Current() + if err != nil || user.Uid == "" { + return tempDir + } + + tmpDirBase := filepath.Join(tempDir, "lazygit-"+user.Uid) + if err := os.MkdirAll(tmpDirBase, 0o700); err != nil { + return tempDir + } + + return tmpDirBase +}