Skip to content

Commit 2eb6138

Browse files
committed
gopls/internal/lsp/filecache: use TempDir if UserCacheDir fails us
On the builders, HOME=/ which causes UserCacheDir to be non-writable. This change attempts to MkdirAll the UserCacheDir and, if that fails, we fail back to TempDir, which is assumed to exist and be writable. Updates golang/go#57638 Change-Id: I6eb81c59b50f90a62c103a2511425fa2f24452fa Reviewed-on: https://go-review.googlesource.com/c/tools/+/460917 Reviewed-by: Bryan Mills <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 36bd3db commit 2eb6138

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

gopls/internal/lsp/filecache/filecache.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ func getCacheDir() string {
180180
}
181181
goplsDir := filepath.Join(userDir, "gopls")
182182

183+
// UserCacheDir may return a nonexistent directory
184+
// (in which case we must create it, which may fail),
185+
// or it may return a non-writable directory, in
186+
// which case we should ideally respect the user's express
187+
// wishes (e.g. XDG_CACHE_HOME) and not write somewhere else.
188+
// Sadly UserCacheDir doesn't currently let us distinguish
189+
// such intent from accidental misconfiguraton such as HOME=/
190+
// in a CI builder. So, we check whether the gopls subdirectory
191+
// can be created (or already exists) and not fall back to /tmp.
192+
// See also https://github.com/golang/go/issues/57638.
193+
if os.MkdirAll(goplsDir, 0700) != nil {
194+
goplsDir = filepath.Join(os.TempDir(), "gopls")
195+
}
196+
183197
// Start the garbage collector.
184198
go gc(goplsDir)
185199

0 commit comments

Comments
 (0)