@@ -23,9 +23,9 @@ import (
23
23
)
24
24
25
25
type importer struct {
26
- view * view
27
- ctx context.Context
28
- config * packages.Config
26
+ snapshot * snapshot
27
+ ctx context.Context
28
+ config * packages.Config
29
29
30
30
// seen maintains the set of previously imported packages.
31
31
// If we have seen a package that is already in this map, we have a circular import.
@@ -62,7 +62,7 @@ type checkPackageHandle struct {
62
62
config * packages.Config
63
63
}
64
64
65
- func (cph * checkPackageHandle ) mode () source.ParseMode {
65
+ func (cph * checkPackageHandle ) Mode () source.ParseMode {
66
66
if len (cph .files ) == 0 {
67
67
return - 1
68
68
}
@@ -83,19 +83,15 @@ type checkPackageData struct {
83
83
err error
84
84
}
85
85
86
- func (pkg * pkg ) GetImport (ctx context.Context , pkgPath string ) (source.Package , error ) {
87
- if imp := pkg .imports [packagePath (pkgPath )]; imp != nil {
88
- return imp , nil
89
- }
90
- // Don't return a nil pointer because that still satisfies the interface.
91
- return nil , errors .Errorf ("no imported package for %s" , pkgPath )
92
- }
93
-
94
86
// checkPackageHandle returns a source.CheckPackageHandle for a given package and config.
95
- func (imp * importer ) checkPackageHandle (ctx context.Context , m * metadata ) (* checkPackageHandle , error ) {
87
+ func (imp * importer ) checkPackageHandle (ctx context.Context , id packageID , s * snapshot ) (* checkPackageHandle , error ) {
88
+ m := s .getMetadata (id )
89
+ if m == nil {
90
+ return nil , errors .Errorf ("no metadata for %s" , id )
91
+ }
96
92
phs , err := imp .parseGoHandles (ctx , m )
97
93
if err != nil {
98
- log .Error (ctx , "no ParseGoHandles" , err , telemetry .Package .Of (m . id ))
94
+ log .Error (ctx , "no ParseGoHandles" , err , telemetry .Package .Of (id ))
99
95
return nil , err
100
96
}
101
97
cph := & checkPackageHandle {
@@ -104,12 +100,19 @@ func (imp *importer) checkPackageHandle(ctx context.Context, m *metadata) (*chec
104
100
config : imp .config ,
105
101
imports : make (map [packagePath ]* checkPackageHandle ),
106
102
}
107
- h := imp .view .session .cache .store .Bind (cph .key (), func (ctx context.Context ) interface {} {
103
+ h := imp .snapshot . view .session .cache .store .Bind (cph .key (), func (ctx context.Context ) interface {} {
108
104
data := & checkPackageData {}
109
105
data .pkg , data .err = imp .typeCheck (ctx , cph , m )
110
106
return data
111
107
})
108
+
112
109
cph .handle = h
110
+
111
+ // Cache the CheckPackageHandle in the snapshot.
112
+ for _ , ph := range cph .files {
113
+ uri := ph .File ().Identity ().URI
114
+ s .addPackage (uri , cph )
115
+ }
113
116
return cph , nil
114
117
}
115
118
@@ -190,16 +193,16 @@ func (cph *checkPackageHandle) key() checkPackageKey {
190
193
func (imp * importer ) parseGoHandles (ctx context.Context , m * metadata ) ([]source.ParseGoHandle , error ) {
191
194
phs := make ([]source.ParseGoHandle , 0 , len (m .files ))
192
195
for _ , uri := range m .files {
193
- f , err := imp .view .GetFile (ctx , uri )
196
+ f , err := imp .snapshot . view .GetFile (ctx , uri )
194
197
if err != nil {
195
198
return nil , err
196
199
}
197
- fh := f . Handle (ctx )
200
+ fh := imp . snapshot . Handle (ctx , f )
198
201
mode := source .ParseExported
199
202
if imp .topLevelPackageID == m .id {
200
203
mode = source .ParseFull
201
204
}
202
- phs = append (phs , imp .view .session .cache .ParseGoHandle (fh , mode ))
205
+ phs = append (phs , imp .snapshot . view .session .cache .ParseGoHandle (fh , mode ))
203
206
}
204
207
return phs , nil
205
208
}
@@ -236,7 +239,7 @@ func (imp *importer) typeCheck(ctx context.Context, cph *checkPackageHandle, m *
236
239
defer done ()
237
240
238
241
pkg := & pkg {
239
- view : imp .view ,
242
+ view : imp .snapshot . view ,
240
243
id : m .id ,
241
244
pkgPath : m .pkgPath ,
242
245
files : cph .Files (),
@@ -259,13 +262,17 @@ func (imp *importer) typeCheck(ctx context.Context, cph *checkPackageHandle, m *
259
262
}
260
263
// Set imports of package to correspond to cached packages.
261
264
cimp := imp .child (ctx , pkg , cph )
262
- for _ , child := range m .children {
263
- childHandle , err := cimp .checkPackageHandle (ctx , child )
265
+ for _ , depID := range m .deps {
266
+ dep := imp .snapshot .getMetadata (depID )
267
+ if dep == nil {
268
+ continue
269
+ }
270
+ depHandle , err := cimp .checkPackageHandle (ctx , depID , imp .snapshot )
264
271
if err != nil {
265
- log .Error (ctx , "no check package handle" , err , telemetry .Package .Of (child . id ))
272
+ log .Error (ctx , "no check package handle" , err , telemetry .Package .Of (depID ))
266
273
continue
267
274
}
268
- cph .imports [child .pkgPath ] = childHandle
275
+ cph .imports [dep .pkgPath ] = depHandle
269
276
}
270
277
var (
271
278
files = make ([]* ast.File , len (pkg .files ))
@@ -284,7 +291,7 @@ func (imp *importer) typeCheck(ctx context.Context, cph *checkPackageHandle, m *
284
291
285
292
for _ , err := range parseErrors {
286
293
if err != nil {
287
- imp .view .session .cache .appendPkgError (pkg , err )
294
+ imp .snapshot . view .session .cache .appendPkgError (pkg , err )
288
295
}
289
296
}
290
297
@@ -308,11 +315,11 @@ func (imp *importer) typeCheck(ctx context.Context, cph *checkPackageHandle, m *
308
315
309
316
cfg := & types.Config {
310
317
Error : func (err error ) {
311
- imp .view .session .cache .appendPkgError (pkg , err )
318
+ imp .snapshot . view .session .cache .appendPkgError (pkg , err )
312
319
},
313
320
Importer : cimp ,
314
321
}
315
- check := types .NewChecker (cfg , imp .view .session .cache .FileSet (), pkg .types , pkg .typesInfo )
322
+ check := types .NewChecker (cfg , imp .snapshot . view .session .cache .FileSet (), pkg .types , pkg .typesInfo )
316
323
317
324
// Type checking errors are handled via the config, so ignore them here.
318
325
_ = check .Files (files )
@@ -328,7 +335,7 @@ func (imp *importer) child(ctx context.Context, pkg *pkg, cph *checkPackageHandl
328
335
}
329
336
seen [pkg .id ] = struct {}{}
330
337
return & importer {
331
- view : imp .view ,
338
+ snapshot : imp .snapshot ,
332
339
ctx : ctx ,
333
340
config : imp .config ,
334
341
seen : seen ,
0 commit comments