Skip to content

Commit

Permalink
gopls/internal/lsp/filecache: use TempDir if UserCacheDir fails us
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
adonovan committed Jan 6, 2023
1 parent 36bd3db commit 2eb6138
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions gopls/internal/lsp/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ func getCacheDir() string {
}
goplsDir := filepath.Join(userDir, "gopls")

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

// Start the garbage collector.
go gc(goplsDir)

Expand Down

0 comments on commit 2eb6138

Please sign in to comment.