go/packages: fix bug in contains for ad-hoc packages
The test was wrong and the code was wrong. Change-Id: I4ffe4bb4788912210b307a06e168629c6800d0fb Reviewed-on: https://go-review.googlesource.com/c/tools/+/184043 Run-TryBot: Michael Matloob <matloob@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
4298585011
commit
aac0b97cf9
|
@ -230,8 +230,10 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q
|
||||||
return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err)
|
return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err)
|
||||||
}
|
}
|
||||||
dirResponse, err := driver(cfg, pattern)
|
dirResponse, err := driver(cfg, pattern)
|
||||||
if err != nil {
|
if err != nil || (len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].Errors) == 1) {
|
||||||
// Couldn't find a package for the directory. Try to load the file as an ad-hoc package.
|
// There was an error loading the package. Try to load the file as an ad-hoc package.
|
||||||
|
// Usually the error will appear in a returned package, but may not if we're in modules mode
|
||||||
|
// and the ad-hoc is located outside a module.
|
||||||
var queryErr error
|
var queryErr error
|
||||||
dirResponse, queryErr = driver(cfg, query)
|
dirResponse, queryErr = driver(cfg, query)
|
||||||
if queryErr != nil {
|
if queryErr != nil {
|
||||||
|
|
|
@ -527,28 +527,32 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
|
||||||
lpkg.color = grey
|
lpkg.color = grey
|
||||||
stack = append(stack, lpkg) // push
|
stack = append(stack, lpkg) // push
|
||||||
stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports
|
stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports
|
||||||
lpkg.Imports = make(map[string]*Package, len(stubs))
|
// If NeedImports isn't set, the imports fields will all be zeroed out.
|
||||||
for importPath, ipkg := range stubs {
|
// If NeedDeps isn't also set we want to keep the stubs.
|
||||||
var importErr error
|
if ld.Mode&NeedImports != 0 && ld.Mode&NeedDeps != 0 {
|
||||||
imp := ld.pkgs[ipkg.ID]
|
lpkg.Imports = make(map[string]*Package, len(stubs))
|
||||||
if imp == nil {
|
for importPath, ipkg := range stubs {
|
||||||
// (includes package "C" when DisableCgo)
|
var importErr error
|
||||||
importErr = fmt.Errorf("missing package: %q", ipkg.ID)
|
imp := ld.pkgs[ipkg.ID]
|
||||||
} else if imp.color == grey {
|
if imp == nil {
|
||||||
importErr = fmt.Errorf("import cycle: %s", stack)
|
// (includes package "C" when DisableCgo)
|
||||||
}
|
importErr = fmt.Errorf("missing package: %q", ipkg.ID)
|
||||||
if importErr != nil {
|
} else if imp.color == grey {
|
||||||
if lpkg.importErrors == nil {
|
importErr = fmt.Errorf("import cycle: %s", stack)
|
||||||
lpkg.importErrors = make(map[string]error)
|
}
|
||||||
|
if importErr != nil {
|
||||||
|
if lpkg.importErrors == nil {
|
||||||
|
lpkg.importErrors = make(map[string]error)
|
||||||
|
}
|
||||||
|
lpkg.importErrors[importPath] = importErr
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
lpkg.importErrors[importPath] = importErr
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if visit(imp) {
|
if visit(imp) {
|
||||||
lpkg.needsrc = true
|
lpkg.needsrc = true
|
||||||
|
}
|
||||||
|
lpkg.Imports[importPath] = imp.Package
|
||||||
}
|
}
|
||||||
lpkg.Imports[importPath] = imp.Package
|
|
||||||
}
|
}
|
||||||
if lpkg.needsrc {
|
if lpkg.needsrc {
|
||||||
srcPkgs = append(srcPkgs, lpkg)
|
srcPkgs = append(srcPkgs, lpkg)
|
||||||
|
|
|
@ -1912,7 +1912,7 @@ func testAdHocContains(t *testing.T, exporter packagestest.Exporter) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
exported.Config.Mode = packages.NeedImports | packages.NeedFiles
|
exported.Config.Mode = packages.NeedImports | packages.NeedFiles
|
||||||
pkgs, err := packages.Load(exported.Config, filename)
|
pkgs, err := packages.Load(exported.Config, "file="+filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue