1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:04:46 -07:00

internal/imports: remove go/packages support

We don't use the go/packages resolver in goimports, and as we develop
gopls it only becomes harder to use there. Give up.

Change-Id: Ic8b566c6dd730b23b0c81d7d34a41f16fe0be7e8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212630
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2019-12-23 19:04:25 -05:00
parent fd66c7521c
commit ac3e9e73d1
2 changed files with 12 additions and 135 deletions

View File

@ -27,7 +27,6 @@ import (
"unicode/utf8"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
"golang.org/x/tools/internal/gopathwalk"
)
@ -725,9 +724,6 @@ type ProcessEnv struct {
GOPATH, GOROOT, GO111MODULE, GOPROXY, GOFLAGS, GOSUMDB string
WorkingDir string
// If true, use go/packages regardless of the environment.
ForceGoPackages bool
// Logf is the default logger for the ProcessEnv.
Logf func(format string, args ...interface{})
@ -757,11 +753,6 @@ func (e *ProcessEnv) GetResolver() Resolver {
if e.resolver != nil {
return e.resolver
}
if e.ForceGoPackages {
e.resolver = &goPackagesResolver{env: e}
return e.resolver
}
out, err := e.invokeGo("env", "GOMOD")
if err != nil || len(bytes.TrimSpace(out.Bytes())) == 0 {
e.resolver = &gopathResolver{env: e}
@ -771,14 +762,6 @@ func (e *ProcessEnv) GetResolver() Resolver {
return e.resolver
}
func (e *ProcessEnv) newPackagesConfig(mode packages.LoadMode) *packages.Config {
return &packages.Config{
Mode: mode,
Dir: e.WorkingDir,
Env: e.env(),
}
}
func (e *ProcessEnv) buildContext() *build.Context {
ctx := build.Default
ctx.GOROOT = e.GOROOT
@ -867,81 +850,6 @@ type Resolver interface {
ClearForNewScan()
}
// gopackagesResolver implements resolver for GOPATH and module workspaces using go/packages.
type goPackagesResolver struct {
env *ProcessEnv
}
func (r *goPackagesResolver) ClearForNewScan() {}
func (r *goPackagesResolver) loadPackageNames(importPaths []string, srcDir string) (map[string]string, error) {
if len(importPaths) == 0 {
return nil, nil
}
cfg := r.env.newPackagesConfig(packages.LoadFiles)
pkgs, err := packages.Load(cfg, importPaths...)
if err != nil {
return nil, err
}
names := map[string]string{}
for _, pkg := range pkgs {
names[VendorlessPath(pkg.PkgPath)] = pkg.Name
}
// We may not have found all the packages. Guess the rest.
for _, path := range importPaths {
if _, ok := names[path]; ok {
continue
}
names[path] = ImportPathToAssumedName(path)
}
return names, nil
}
func (r *goPackagesResolver) scan(refs references, _ bool, _ []gopathwalk.RootType) ([]*pkg, error) {
var loadQueries []string
for pkgName := range refs {
loadQueries = append(loadQueries, "iamashamedtousethedisabledqueryname="+pkgName)
}
sort.Strings(loadQueries)
cfg := r.env.newPackagesConfig(packages.LoadFiles)
goPackages, err := packages.Load(cfg, loadQueries...)
if err != nil {
return nil, err
}
var scan []*pkg
for _, goPackage := range goPackages {
scan = append(scan, &pkg{
dir: filepath.Dir(goPackage.CompiledGoFiles[0]),
importPathShort: VendorlessPath(goPackage.PkgPath),
goPackage: goPackage,
packageName: goPackage.Name,
})
}
return scan, nil
}
func (r *goPackagesResolver) loadExports(ctx context.Context, pkg *pkg) (string, []string, error) {
if pkg.goPackage == nil {
return "", nil, fmt.Errorf("goPackage not set")
}
var exports []string
fset := token.NewFileSet()
for _, fname := range pkg.goPackage.CompiledGoFiles {
f, err := parser.ParseFile(fset, fname, nil, 0)
if err != nil {
return "", nil, fmt.Errorf("parsing %s: %v", fname, err)
}
for name := range f.Scope.Objects {
if ast.IsExported(name) {
exports = append(exports, name)
}
}
}
return pkg.goPackage.Name, exports, nil
}
func addExternalCandidates(pass *pass, refs references, filename string) error {
dirScan, err := pass.env.GetResolver().scan(refs, false, nil)
if err != nil {
@ -1138,7 +1046,6 @@ func packageDirToName(dir string) (packageName string, err error) {
}
type pkg struct {
goPackage *packages.Package
dir string // absolute file path to pkg directory ("/usr/lib/go/src/net/http")
importPathShort string // vendorless import path ("net/http", "a/b")
packageName string // package name loaded from source if requested

View File

@ -1576,10 +1576,9 @@ var _ = bytes.Buffer
}
type testConfig struct {
gopathOnly bool
goPackagesIncompatible bool
module packagestest.Module
modules []packagestest.Module
gopathOnly bool
module packagestest.Module
modules []packagestest.Module
}
// fm is the type for a packagestest.Module's Files, abbreviated for shorter lines.
@ -1592,37 +1591,12 @@ func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
c.modules = []packagestest.Module{c.module}
}
var kinds []string
for _, exporter := range packagestest.All {
kinds = append(kinds, exporter.Name())
kinds = append(kinds, exporter.Name()+"_GoPackages")
}
for _, kind := range kinds {
t.Run(kind, func(t *testing.T) {
t.Run(exporter.Name(), func(t *testing.T) {
t.Helper()
forceGoPackages := false
var exporter packagestest.Exporter
if c.gopathOnly && strings.HasPrefix(kind, "Modules") {
if c.gopathOnly && exporter.Name() == "Modules" {
t.Skip("test marked GOPATH-only")
}
if c.goPackagesIncompatible && strings.HasSuffix(kind, "_GoPackages") {
t.Skip("test marked go/packages-incompatible")
}
switch kind {
case "GOPATH":
exporter = packagestest.GOPATH
case "GOPATH_GoPackages":
exporter = packagestest.GOPATH
forceGoPackages = true
case "Modules":
exporter = packagestest.Modules
case "Modules_GoPackages":
exporter = packagestest.Modules
forceGoPackages = true
default:
panic("unknown test type")
}
exported := packagestest.Export(t, exporter, c.modules)
defer exported.Cleanup()
@ -1636,14 +1610,13 @@ func (c testConfig) test(t *testing.T, fn func(*goimportTest)) {
it := &goimportTest{
T: t,
env: &ProcessEnv{
GOROOT: env["GOROOT"],
GOPATH: env["GOPATH"],
GO111MODULE: env["GO111MODULE"],
GOSUMDB: env["GOSUMDB"],
WorkingDir: exported.Config.Dir,
ForceGoPackages: forceGoPackages,
Debug: *testDebug,
Logf: log.Printf,
GOROOT: env["GOROOT"],
GOPATH: env["GOPATH"],
GO111MODULE: env["GO111MODULE"],
GOSUMDB: env["GOSUMDB"],
WorkingDir: exported.Config.Dir,
Debug: *testDebug,
Logf: log.Printf,
},
exported: exported,
}
@ -2459,7 +2432,6 @@ import "bytes"
var _ = &bytes.Buffer{}
`
testConfig{
goPackagesIncompatible: true,
module: packagestest.Module{
Name: "mycompany.net",
},
@ -2540,7 +2512,6 @@ func TestGetCandidates(t *testing.T) {
Files: fm{"bar/bar.go": "package bar\n"},
},
},
goPackagesIncompatible: true, // getAllCandidates doesn't support the go/packages resolver.
}.test(t, func(t *goimportTest) {
candidates, err := getAllCandidates("x.go", t.env)
if err != nil {
@ -2577,7 +2548,6 @@ func TestGetPackageCompletions(t *testing.T) {
Files: fm{"rand/bar.go": "package rand\nvar Bar int\n"},
},
},
goPackagesIncompatible: true, // getPackageCompletions doesn't support the go/packages resolver.
}.test(t, func(t *goimportTest) {
candidates, err := getPackageExports("rand", "x.go", t.env)
if err != nil {