cmd/guru: if query file is not beneath GOPATH, treat it as a package
This allows type-based guru queries to work on arbitrary files at the root or even outside of a GOPATH workspace (as "go run foo.go" does). Fixes golang/go#15797 Change-Id: I2be28f7259448e6398aae84d6ae7e71d8649967a Reviewed-on: https://go-review.googlesource.com/30451 Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
73d2e795b8
commit
27f88d9b7a
|
|
@ -173,32 +173,35 @@ func importQueryPackage(pos string, conf *loader.Config) (string, error) {
|
||||||
|
|
||||||
_, importPath, err := guessImportPath(filename, conf.Build)
|
_, importPath, err := guessImportPath(filename, conf.Build)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err // can't find GOPATH dir
|
// Can't find GOPATH dir.
|
||||||
}
|
// Treat the query file as its own package.
|
||||||
|
importPath = "command-line-arguments"
|
||||||
|
conf.CreateFromFilenames(importPath, filename)
|
||||||
|
} else {
|
||||||
|
// Check that it's possible to load the queried package.
|
||||||
|
// (e.g. guru tests contain different 'package' decls in same dir.)
|
||||||
|
// Keep consistent with logic in loader/util.go!
|
||||||
|
cfg2 := *conf.Build
|
||||||
|
cfg2.CgoEnabled = false
|
||||||
|
bp, err := cfg2.Import(importPath, "", 0)
|
||||||
|
if err != nil {
|
||||||
|
return "", err // no files for package
|
||||||
|
}
|
||||||
|
|
||||||
// Check that it's possible to load the queried package.
|
switch pkgContainsFile(bp, filename) {
|
||||||
// (e.g. guru tests contain different 'package' decls in same dir.)
|
case 'T':
|
||||||
// Keep consistent with logic in loader/util.go!
|
conf.ImportWithTests(importPath)
|
||||||
cfg2 := *conf.Build
|
case 'X':
|
||||||
cfg2.CgoEnabled = false
|
conf.ImportWithTests(importPath)
|
||||||
bp, err := cfg2.Import(importPath, "", 0)
|
importPath += "_test" // for TypeCheckFuncBodies
|
||||||
if err != nil {
|
case 'G':
|
||||||
return "", err // no files for package
|
conf.Import(importPath)
|
||||||
}
|
default:
|
||||||
|
// This happens for ad-hoc packages like
|
||||||
switch pkgContainsFile(bp, filename) {
|
// $GOROOT/src/net/http/triv.go.
|
||||||
case 'T':
|
return "", fmt.Errorf("package %q doesn't contain file %s",
|
||||||
conf.ImportWithTests(importPath)
|
importPath, filename)
|
||||||
case 'X':
|
}
|
||||||
conf.ImportWithTests(importPath)
|
|
||||||
importPath += "_test" // for TypeCheckFuncBodies
|
|
||||||
case 'G':
|
|
||||||
conf.Import(importPath)
|
|
||||||
default:
|
|
||||||
// This happens for ad-hoc packages like
|
|
||||||
// $GOROOT/src/net/http/triv.go.
|
|
||||||
return "", fmt.Errorf("package %q doesn't contain file %s",
|
|
||||||
importPath, filename)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.TypeCheckFuncBodies = func(p string) bool { return p == importPath }
|
conf.TypeCheckFuncBodies = func(p string) bool { return p == importPath }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue