-
Notifications
You must be signed in to change notification settings - Fork 232
/
pkgparse.go
212 lines (185 loc) · 5.32 KB
/
pkgparse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package pkginfo
import (
"cmp"
"fmt"
"go/ast"
goparser "go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"encr.dev/pkg/fns"
"encr.dev/pkg/paths"
)
// File pkgparse implements parsing of packages.
// parseResult is the result from attempting to parse a package.
type parseResult struct {
done chan struct{} // closed when parsing is completed
pkg *Package
ok bool
bailout bool
}
// loadPkgSpec is the specification for how to load a package.
type loadPkgSpec struct {
// cause is the source position that caused the load.
// It's used to generate useful error messages.
cause token.Pos
// path is the package path.
path paths.Pkg
// dir is the directory containing the package.
dir paths.FS
}
// doParsePkg parses a single package in the given directory.
// It returns (nil, false) if the directory contains no Go files.
func (l *Loader) doParsePkg(s loadPkgSpec) (pkg *Package, ok bool) {
l.c.Errs.BailoutOnErrors(func() {
astPkgs, files := l.parseAST(s)
pkg = l.processPkg(s, astPkgs, files)
})
return pkg, pkg != nil
}
// processPkg combines the results of parsing a package into a single *Package.
func (l *Loader) processPkg(s loadPkgSpec, pkgs []*ast.Package, files []*File) *Package {
if n := len(pkgs); n > 1 {
// Make sure the extra packages are just "_test" packages.
// Pull out the package names.
slices.SortFunc(pkgs, func(a, b *ast.Package) int {
return cmp.Compare(a.Name, b.Name)
})
pkgNames := fns.Map(pkgs, func(pkg *ast.Package) string { return pkg.Name })
if n == 2 && pkgNames[1] == pkgNames[0]+"_test" {
// We're good
} else {
names := strings.Join(pkgNames[:n-1], ", ") + " and " + pkgNames[n-1]
l.c.Errs.Addf(s.cause, fmt.Sprintf("found multiple package names in package %s: %s", s.path, names))
}
} else if n == 0 {
// No Go files; ignore directory
return nil
}
p := pkgs[0]
pkg := &Package{
AST: p,
Name: p.Name,
ImportPath: s.path,
FSPath: s.dir,
Files: files,
Imports: make(map[paths.Pkg]ast.Node),
}
for _, f := range files {
f.Pkg = pkg
// Fill in imports.
for importPath, pointer := range f.Imports {
pkg.Imports[importPath] = pointer
}
// Fill in package docs.
if pkg.Doc == "" && !f.TestFile && f.initialAST.Doc != nil {
pkg.Doc = f.initialAST.Doc.Text()
}
}
return pkg
}
// parseAST is like go/parser.ParseDir but it constructs *File objects instead.
func (l *Loader) parseAST(s loadPkgSpec) ([]*ast.Package, []*File) {
dir := s.dir.ToIO()
entries, err := os.ReadDir(dir)
if err != nil {
l.c.Errs.Addf(s.cause, "parse package %q: %v", s.path, err)
return nil, nil
}
shouldParseFile := func(info fs.DirEntry) bool {
name := info.Name()
switch {
// Don't parse encore.gen.go files, since they're not intended to be checked in.
// We've had several issues where things work locally but not in CI/CD because
// the encore.gen.go file was parsed for local development which papered over issues.
case strings.Contains(name, "encore.gen.go"):
return false
case !l.c.ParseTests && strings.HasSuffix(name, "_test.go"):
return false
case !strings.HasSuffix(name, ".go"):
return false
default:
return true
}
}
type fileInfo struct {
path paths.FS
ioPath string
baseName string
}
infos := make([]fileInfo, 0, len(entries))
for _, e := range entries {
if !e.IsDir() && shouldParseFile(e) {
baseName := e.Name()
ioPath := filepath.Join(dir, baseName)
path := s.dir.Join(baseName)
infos = append(infos, fileInfo{path: path, ioPath: ioPath, baseName: baseName})
}
}
var pkgs []*ast.Package
var files []*File
seenPkgs := make(map[string]*ast.Package) // package name -> pkg
for _, d := range infos {
// Check if this file should be part of the build
matched, err := l.buildCtx.MatchFile(dir, d.baseName)
if err != nil {
l.c.Errs.Add(errMatchingFile.InFile(d.ioPath).Wrapping(err))
continue
} else if !matched {
continue
} else if strings.EqualFold(d.baseName, "encore.gen.go") {
continue
}
reader, err := os.Open(d.ioPath)
if err != nil {
l.c.Errs.Add(errReadingFile.InFile(d.ioPath).Wrapping(err))
continue
}
// Parse the package and imports only so code can consult that.
// We parse the full AST on-demand later.
mode := goparser.ParseComments | goparser.ImportsOnly
astFile, err := goparser.ParseFile(l.c.FS, d.ioPath, reader, mode)
_ = reader.Close()
if err != nil {
l.c.Errs.AddStd(err)
continue
}
pkgName := astFile.Name.Name
pkg, found := seenPkgs[pkgName]
if !found {
pkg = &ast.Package{
Name: pkgName,
Files: make(map[string]*ast.File),
}
seenPkgs[pkgName] = pkg
pkgs = append(pkgs, pkg)
}
pkg.Files[d.ioPath] = astFile
isTestFile := strings.HasSuffix(d.baseName, "_test.go") || strings.HasSuffix(pkgName, "_test")
files = append(files, &File{
l: l,
Name: d.baseName,
FSPath: d.path,
Pkg: nil, // will be set later
Imports: getFileImports(astFile),
TestFile: isTestFile,
initialAST: astFile,
})
}
return pkgs, files
}
func getFileImports(f *ast.File) map[paths.Pkg]ast.Node {
imports := make(map[paths.Pkg]ast.Node)
for _, s := range f.Imports {
if importPath, err := strconv.Unquote(s.Path.Value); err == nil {
if p, ok := paths.PkgPath(importPath); ok {
imports[p] = s
}
}
}
return imports
}