-
Notifications
You must be signed in to change notification settings - Fork 232
/
loader.go
174 lines (152 loc) · 4.14 KB
/
loader.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
package pkginfo
import (
"go/build"
"go/token"
"os"
"sync"
"golang.org/x/exp/slices"
"golang.org/x/tools/go/packages"
"encr.dev/pkg/paths"
"encr.dev/v2/internals/parsectx"
"encr.dev/v2/internals/perr"
)
// New creates a new Loader.
func New(c *parsectx.Context) *Loader {
l := &Loader{
c: c,
modules: make(map[paths.Mod]*Module),
parsed: make(map[paths.Pkg]*parseResult),
}
l.init()
return l
}
// A Loader provides lazy loading of package information.
type Loader struct {
c *parsectx.Context
// initialized by init.
mainModule *Module
runtimeModule *Module
buildCtx *build.Context
packagesConfig *packages.Config
// modules contains loaded module information.
modulesMu sync.Mutex
modules map[paths.Mod]*Module
// parsed is a cache of parse results, guarded by parsedMu.
parsedMu sync.Mutex
parsed map[paths.Pkg]*parseResult // importPath -> result
}
func (l *Loader) init() {
// Resolve the main module.
l.mainModule = l.loadModuleFromDisk(l.c.MainModuleDir, "")
// Manually cache the main module.
l.modules[l.mainModule.Path] = l.mainModule
// Resolve the encore.dev runtime module.
l.runtimeModule = &Module{
l: l,
RootDir: l.c.Build.EncoreRuntime,
Path: "encore.dev",
Version: "v1.0.0",
}
l.modules["encore.dev"] = l.runtimeModule
b := l.c.Build
d := &build.Default
l.buildCtx = &build.Context{
GOARCH: b.GOARCH,
GOOS: b.GOOS,
GOROOT: b.GOROOT.ToIO(),
Dir: l.c.MainModuleDir.ToIO(),
CgoEnabled: b.CgoEnabled,
UseAllFiles: false,
Compiler: d.Compiler,
BuildTags: append(slices.Clone(d.BuildTags), b.BuildTags...),
ToolTags: slices.Clone(d.ToolTags),
ReleaseTags: slices.Clone(d.ReleaseTags),
}
// Set up the go/packages configuration for resolving modules.
cgoEnabled := "0"
if b.CgoEnabled {
cgoEnabled = "1"
}
l.packagesConfig = &packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedModule,
Context: l.c.Ctx,
Dir: l.c.MainModuleDir.ToIO(),
Env: append(os.Environ(),
"GOOS="+b.GOOS,
"GOARCH="+b.GOARCH,
"GOROOT="+b.GOROOT.ToIO(),
"CGO_ENABLED="+cgoEnabled,
),
Fset: l.c.FS,
Tests: l.c.ParseTests,
Overlay: nil,
Logf: func(format string, args ...any) {
l.c.Log.Debug().Str("component", "pkgload").Msgf("go/packages: "+format, args...)
},
}
}
// MainModule returns the parsed main module.
func (l *Loader) MainModule() *Module {
return l.mainModule
}
// RuntimeModule returns the parsed runtime module.
func (l *Loader) RuntimeModule() *Module {
return l.runtimeModule
}
// MustLoadPkg loads a package.
// If the package contains no Go files, it bails out.
func (l *Loader) MustLoadPkg(cause token.Pos, pkgPath paths.Pkg) (pkg *Package) {
pkg, ok := l.LoadPkg(cause, pkgPath)
if !ok {
l.c.Errs.Addf(cause, "could not find package %q", pkgPath)
l.c.Errs.Bailout()
}
return pkg
}
// LoadPkg loads a package.
// If the package contains no Go files to build, it returns (nil, false).
func (l *Loader) LoadPkg(cause token.Pos, pkgPath paths.Pkg) (pkg *Package, ok bool) {
// Do we have the result cached already?
l.parsedMu.Lock()
result, wasCached := l.parsed[pkgPath]
if !wasCached {
// Not cached; store a new entry so other goroutines will wait for us.
result = &parseResult{done: make(chan struct{})}
l.parsed[pkgPath] = result
defer close(result.done)
}
l.parsedMu.Unlock()
if wasCached {
// We have a cached package. Wait for parsing to complete.
select {
case <-result.done:
if result.bailout {
return nil, false
}
return result.pkg, result.ok
case <-l.c.Ctx.Done():
// The context was cancelled first.
return nil, false
}
}
// Not cached. Do the parsing.
// Catch any err since this runs in a separate goroutine.
defer func() {
if _, caught := perr.CatchBailout(recover()); caught {
result.bailout = true
pkg = nil
ok = false
}
}()
module := l.resolveModuleForPkg(cause, pkgPath)
relPath, ok := module.Path.RelativePathToPkg(pkgPath)
if !ok {
return nil, false
}
result.pkg, result.ok = l.doParsePkg(loadPkgSpec{
cause: cause,
path: pkgPath,
dir: module.RootDir.Join(relPath.ToIO()),
})
return result.pkg, result.ok
}