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

Commit b876567

Browse files
author
Henry Wong
committed
[elastic] [WIP] Try invoke 'go list ...' again if we get nothing from the previous 'go list ...' invoke
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.
1 parent bf4dec8 commit b876567

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

go/packages/golist.go

+6
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,12 @@ 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. Separate it from the cache
657+
// mechanism.
658+
if cfg.Mode&(NeedImports|NeedTypes|NeedSyntax|NeedTypesInfo) != 0 {
659+
res, err := golistDriver(cfg, rootsDirs, words...)
660+
return res, err
661+
}
656662
if val, ok := goListLRUCache.Get(hashKey); ok {
657663
res := val.(goListResult)
658664
return res.response, res.err

internal/lsp/cache/load.go

+10
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 to load the packages for current file. This time won't parse the imports and anything related to
90+
// the dependencies.
91+
if len(pkgs) == 0 {
92+
cfg := v.Config(ctx)
93+
cfg.Tests = false
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)