| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package golang |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"go/parser" |
| 10 |
"go/token" |
| 11 |
"sort" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"time" |
| 15 |
|
| 16 |
"golang.org/x/tools/gopls/internal/cache" |
| 17 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 18 |
"golang.org/x/tools/gopls/internal/file" |
| 19 |
"golang.org/x/tools/internal/event" |
| 20 |
"golang.org/x/tools/internal/imports" |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func KnownPackagePaths(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]PackagePath, error) { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
current, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI()) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
src, err := fh.Content() |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
file, err := parser.ParseFile(token.NewFileSet(), fh.URI().Path(), src, parser.ImportsOnly) |
| 45 |
if err != nil { |
| 46 |
return nil, err |
| 47 |
} |
| 48 |
imported := make(map[PackagePath]bool) |
| 49 |
for _, imp := range file.Imports { |
| 50 |
if id := current.DepsByImpPath[metadata.UnquoteImportPath(imp)]; id != "" { |
| 51 |
if mp := snapshot.Metadata(id); mp != nil { |
| 52 |
imported[mp.PkgPath] = true |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
knownPkgs, err := snapshot.AllMetadata(ctx) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
seen := make(map[PackagePath]bool) |
| 63 |
for _, knownPkg := range knownPkgs { |
| 64 |
|
| 65 |
if knownPkg.Name == "main" { |
| 66 |
continue |
| 67 |
} |
| 68 |
|
| 69 |
if knownPkg.ForTest != "" { |
| 70 |
continue |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if imported[knownPkg.PkgPath] { |
| 76 |
continue |
| 77 |
} |
| 78 |
|
| 79 |
if !metadata.IsValidImport(current.PkgPath, knownPkg.PkgPath, snapshot.View().Type() != cache.GoPackagesDriverView) { |
| 80 |
continue |
| 81 |
} |
| 82 |
|
| 83 |
if isDirectlyCyclical(current, knownPkg) { |
| 84 |
continue |
| 85 |
} |
| 86 |
|
| 87 |
seen[knownPkg.PkgPath] = true |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if err := snapshot.RunProcessEnvFunc(ctx, func(ctx context.Context, o *imports.Options) error { |
| 92 |
ctx, cancel := context.WithTimeout(ctx, time.Millisecond*80) |
| 93 |
defer cancel() |
| 94 |
var seenMu sync.Mutex |
| 95 |
wrapped := func(ifix imports.ImportFix) { |
| 96 |
seenMu.Lock() |
| 97 |
defer seenMu.Unlock() |
| 98 |
|
| 99 |
seen[PackagePath(ifix.StmtInfo.ImportPath)] = true |
| 100 |
} |
| 101 |
return imports.GetAllCandidates(ctx, wrapped, "", fh.URI().Path(), string(current.Name), o.Env) |
| 102 |
}); err != nil { |
| 103 |
|
| 104 |
event.Error(ctx, "imports.GetAllCandidates", err) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
paths := make([]PackagePath, 0, len(seen)) |
| 109 |
for path := range seen { |
| 110 |
paths = append(paths, path) |
| 111 |
} |
| 112 |
sort.Slice(paths, func(i, j int) bool { |
| 113 |
importI, importJ := paths[i], paths[j] |
| 114 |
iHasDot := strings.Contains(string(importI), ".") |
| 115 |
jHasDot := strings.Contains(string(importJ), ".") |
| 116 |
if iHasDot != jHasDot { |
| 117 |
return jHasDot |
| 118 |
} |
| 119 |
return importI < importJ |
| 120 |
}) |
| 121 |
|
| 122 |
return paths, nil |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func isDirectlyCyclical(pkg, imported *metadata.Package) bool { |
| 135 |
_, ok := imported.DepsByPkgPath[pkg.PkgPath] |
| 136 |
return ok |
| 137 |
} |