From 91f80e683c10fea00e7f965a1a7cac482ce52541 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Wed, 7 Nov 2018 16:22:34 -0500 Subject: [PATCH] go/packages: use absolute path of dir in call to go list for file= query When running go list for a file= query, the go/packages code previously ran "go list ." command in the directory of the provided file. This was necessary for compatibility with the fallback, but caused problems when running file= queries for files in the module cache. We now use absolute directory paths (supported in Go >=1.11) in the non-fallback case and retain the previous behavior in the fallback case. Change-Id: I3a9feaaeda12e068ec3d510b5f78437482c65b4c Reviewed-on: https://go-review.googlesource.com/c/148199 Run-TryBot: Michael Matloob TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- go/packages/golist.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/go/packages/golist.go b/go/packages/golist.go index 4b707a5e8b..6be07b9ad8 100644 --- a/go/packages/golist.go +++ b/go/packages/golist.go @@ -86,9 +86,11 @@ extractQueries: // TODO(matloob): Remove the definition of listfunc and just use golistPackages once go1.12 is released. var listfunc driver + var isFallback bool listfunc = func(cfg *Config, words ...string) (*driverResponse, error) { response, err := golistDriverCurrent(cfg, words...) if _, ok := err.(goTooOldError); ok { + isFallback = true listfunc = golistDriverFallback return listfunc(cfg, words...) } @@ -132,7 +134,7 @@ extractQueries: response.Packages = append(response.Packages, p) } - containsResults, err := runContainsQueries(cfg, listfunc, addPkg, containFiles) + containsResults, err := runContainsQueries(cfg, listfunc, isFallback, addPkg, containFiles) if err != nil { return nil, err } @@ -146,13 +148,23 @@ extractQueries: return response, nil } -func runContainsQueries(cfg *Config, driver driver, addPkg func(*Package), queries []string) ([]string, error) { +func runContainsQueries(cfg *Config, driver driver, isFallback bool, addPkg func(*Package), queries []string) ([]string, error) { var results []string for _, query := range queries { // TODO(matloob): Do only one query per directory. fdir := filepath.Dir(query) - cfg.Dir = fdir - dirResponse, err := driver(cfg, ".") + // Pass absolute path of directory to go list so that it knows to treat it as a directory, + // not a package path. + pattern, err := filepath.Abs(fdir) + if err != nil { + return nil, fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err) + } + if isFallback { + pattern = "." + cfg.Dir = fdir + } + + dirResponse, err := driver(cfg, pattern) if err != nil { return nil, err }