Skip to content

Commit edddc5f

Browse files
adonovangopherbot
authored andcommitted
go/packages: don't discard errors loading export data
In the course of debugging something unrelated, I noticed that loadFromExportData's error result was always discarded. This change appends it to the set of package errors. Change-Id: I32a7cbceb7cffd29cedac19eeff8b092663813f0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/466075 Run-TryBot: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]>
1 parent a762c82 commit edddc5f

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

go/packages/packages.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -878,12 +878,19 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
878878
// never has to create a types.Package for an indirect dependency,
879879
// which would then require that such created packages be explicitly
880880
// inserted back into the Import graph as a final step after export data loading.
881+
// (Hence this return is after the Types assignment.)
881882
// The Diamond test exercises this case.
882883
if !lpkg.needtypes && !lpkg.needsrc {
883884
return
884885
}
885886
if !lpkg.needsrc {
886-
ld.loadFromExportData(lpkg)
887+
if err := ld.loadFromExportData(lpkg); err != nil {
888+
lpkg.Errors = append(lpkg.Errors, Error{
889+
Pos: "-",
890+
Msg: err.Error(),
891+
Kind: UnknownError, // e.g. can't find/open/parse export data
892+
})
893+
}
887894
return // not a source package, don't get syntax trees
888895
}
889896

@@ -970,7 +977,8 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
970977
// The config requested loading sources and types, but sources are missing.
971978
// Add an error to the package and fall back to loading from export data.
972979
appendError(Error{"-", fmt.Sprintf("sources missing for package %s", lpkg.ID), ParseError})
973-
ld.loadFromExportData(lpkg)
980+
_ = ld.loadFromExportData(lpkg) // ignore any secondary errors
981+
974982
return // can't get syntax trees for this package
975983
}
976984

@@ -1194,9 +1202,10 @@ func sameFile(x, y string) bool {
11941202
return false
11951203
}
11961204

1197-
// loadFromExportData returns type information for the specified
1205+
// loadFromExportData ensures that type information is present for the specified
11981206
// package, loading it from an export data file on the first request.
1199-
func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error) {
1207+
// On success it sets lpkg.Types to a new Package.
1208+
func (ld *loader) loadFromExportData(lpkg *loaderPackage) error {
12001209
if lpkg.PkgPath == "" {
12011210
log.Fatalf("internal error: Package %s has no PkgPath", lpkg)
12021211
}
@@ -1207,8 +1216,8 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error
12071216
// must be sequential. (Finer-grained locking would require
12081217
// changes to the gcexportdata API.)
12091218
//
1210-
// The exportMu lock guards the Package.Pkg field and the
1211-
// types.Package it points to, for each Package in the graph.
1219+
// The exportMu lock guards the lpkg.Types field and the
1220+
// types.Package it points to, for each loaderPackage in the graph.
12121221
//
12131222
// Not all accesses to Package.Pkg need to be protected by exportMu:
12141223
// graph ordering ensures that direct dependencies of source
@@ -1217,18 +1226,18 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error
12171226
defer ld.exportMu.Unlock()
12181227

12191228
if tpkg := lpkg.Types; tpkg != nil && tpkg.Complete() {
1220-
return tpkg, nil // cache hit
1229+
return nil // cache hit
12211230
}
12221231

12231232
lpkg.IllTyped = true // fail safe
12241233

12251234
if lpkg.ExportFile == "" {
12261235
// Errors while building export data will have been printed to stderr.
1227-
return nil, fmt.Errorf("no export data file")
1236+
return fmt.Errorf("no export data file")
12281237
}
12291238
f, err := os.Open(lpkg.ExportFile)
12301239
if err != nil {
1231-
return nil, err
1240+
return err
12321241
}
12331242
defer f.Close()
12341243

@@ -1240,7 +1249,7 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error
12401249
// queries.)
12411250
r, err := gcexportdata.NewReader(f)
12421251
if err != nil {
1243-
return nil, fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
1252+
return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
12441253
}
12451254

12461255
// Build the view.
@@ -1284,7 +1293,7 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error
12841293
// (May modify incomplete packages in view but not create new ones.)
12851294
tpkg, err := gcexportdata.Read(r, ld.Fset, view, lpkg.PkgPath)
12861295
if err != nil {
1287-
return nil, fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
1296+
return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
12881297
}
12891298
if _, ok := view["go.shape"]; ok {
12901299
// Account for the pseudopackage "go.shape" that gets
@@ -1297,8 +1306,7 @@ func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error
12971306

12981307
lpkg.Types = tpkg
12991308
lpkg.IllTyped = false
1300-
1301-
return tpkg, nil
1309+
return nil
13021310
}
13031311

13041312
// impliedLoadMode returns loadMode with its dependencies.

0 commit comments

Comments
 (0)