From 12161f39cde8ab98efdf135b4e2c0a9bac018e07 Mon Sep 17 00:00:00 2001 From: Sergey Kostyaev Date: Thu, 14 Feb 2019 15:23:55 +0700 Subject: [PATCH] fix parsing nested modules --- langserver/internal/cache/view.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/langserver/internal/cache/view.go b/langserver/internal/cache/view.go index 96b39ff..35bd716 100644 --- a/langserver/internal/cache/view.go +++ b/langserver/internal/cache/view.go @@ -9,6 +9,8 @@ import ( "go/ast" "go/parser" "go/token" + "os" + "path/filepath" "strings" "sync" @@ -78,12 +80,25 @@ func (v *View) getFile(uri source.URI) *File { return f } +func isFileInsideGomod(path string) bool { + gopath := os.Getenv("GOPATH") + if gopath == "" { + gopath = filepath.Join(os.Getenv("HOME"), "go") + } + gomodpath := filepath.Join(gopath, "pkg", "mod") + + return strings.HasPrefix(path, gomodpath) +} + func (v *View) parse(uri source.URI) error { path, err := uri.Filename() if err != nil { return err } + if !isFileInsideGomod(path) { + v.Config.Dir = filepath.Dir(path) + } v.Config.ParseFile = v.parseFile pkgs, err := packages.Load(v.Config, fmt.Sprintf("file=%s", path)) if len(pkgs) == 0 {