internal/imports: prevent panic in imports GOMODCACHE logic

Fixes golang/go#42949

Change-Id: Icabfce85550cb6e69383ba50c2c675a5c730bec5
Reviewed-on: https://go-review.googlesource.com/c/tools/+/275451
Trust: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2020-12-04 16:21:12 -05:00
parent 627cb6d672
commit 1dfd70e0ab
1 changed files with 5 additions and 1 deletions

View File

@ -88,7 +88,11 @@ func (r *ModuleResolver) init() error {
if gmc := r.env.Env["GOMODCACHE"]; gmc != "" {
r.moduleCacheDir = gmc
} else {
r.moduleCacheDir = filepath.Join(filepath.SplitList(goenv["GOPATH"])[0], "/pkg/mod")
gopaths := filepath.SplitList(goenv["GOPATH"])
if len(gopaths) == 0 {
return fmt.Errorf("empty GOPATH")
}
r.moduleCacheDir = filepath.Join(gopaths[0], "/pkg/mod")
}
sort.Slice(r.modsByModPath, func(i, j int) bool {