Skip to content
This repository was archived by the owner on Jan 31, 2024. It is now read-only.

Commit 7b98147

Browse files
author
Henry Wong
authored
[elastic] Try invoke 'go list ...' again if we get nothing from the previous 'go list ...' invoke (#85)
The original 'go list ...' invoke has strict limitations about the load mode, once 'go list ...' failed, we will get nothing about the packages. For this case, try to invoke 'go list ...' with weaker limitations, like without any dependency related information query. Although the information we get is limited to the current file, it is better than nothing.
1 parent 37aa6c2 commit 7b98147

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

go/packages/golist.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@ func golistDriverLRUCached(cfg *Config, rootsDirs func() *goInfo, words ...strin
653653
h.Write([]byte(cfg.Dir))
654654
h.Write([]byte(words[len(words)-1]))
655655
hashKey := h.Sum32()
656+
// If this is a temporary `go list ...` invoke, i.e. without deps information query. Don't entangle it with the go
657+
// list cache.
658+
if cfg.Mode&(NeedImports|NeedTypes|NeedSyntax|NeedTypesInfo) == 0 {
659+
return golistDriver(cfg, rootsDirs, words...)
660+
}
656661
if val, ok := goListLRUCache.Get(hashKey); ok {
657662
res := val.(goListResult)
658663
return res.response, res.err

internal/lsp/cache/load.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ func (v *view) checkMetadata(ctx context.Context, f *goFile) (map[packageID]*met
8686
ctx, done := trace.StartSpan(ctx, "packages.Load", telemetry.File.Of(f.filename()))
8787
defer done()
8888
pkgs, err := packages.Load(v.Config(ctx), fmt.Sprintf("file=%s", f.filename()))
89+
// Give another try with loose mode to load the packages for current file.
90+
if len(pkgs) == 0 {
91+
cfg := v.Config(ctx)
92+
cfg.Tests = false
93+
// Remove any dependency require mode.
94+
cfg.Mode = packages.NeedName |
95+
packages.NeedFiles |
96+
packages.NeedCompiledGoFiles
97+
pkgs, err = packages.Load(cfg, fmt.Sprintf("file=%s", f.filename()))
98+
}
8999
if len(pkgs) == 0 {
90100
if err == nil {
91101
err = errors.Errorf("go/packages.Load: no packages found for %s", f.filename())

0 commit comments

Comments
 (0)