Skip to content

Commit 12a0517

Browse files
adonovanfindleyr
authored andcommitted
internal/gcimporter: improve error handling
This change: - updates the error message reported when the importer recovers from a panic. - updates the set of test input files to include examples of the formats used in go1.16-go1.20. - adds a recover handler to UImportData, for symmetry with IImportData. This was exposed by the new test case. - fixes an accidental shadowing bug that suppressed the bundle format version check. Fixes golang/go#59179 Change-Id: Ib6c20fc15e2051481fccba593607a7df0e01bc74 Reviewed-on: https://go-review.googlesource.com/c/tools/+/494676 Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Alan Donovan <adonovan@google.com> gopls-CI: kokoro <noreply+kokoro@google.com>
1 parent 5eb1eb9 commit 12a0517

File tree

10 files changed

+15
-22
lines changed

10 files changed

+15
-22
lines changed

internal/gcimporter/gcimporter.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ func Import(packages map[string]*types.Package, path, srcDir string, lookup func
230230
// Or, define a new standard go/types/gcexportdata package.
231231
fset := token.NewFileSet()
232232

233-
// The indexed export format starts with an 'i'; the older
234-
// binary export format starts with a 'c', 'd', or 'v'
235-
// (from "version"). Select appropriate importer.
233+
// Select appropriate importer.
236234
if len(data) > 0 {
237235
switch data[0] {
238236
case 'v', 'c', 'd': // binary, till go1.10

internal/gcimporter/gcimporter_test.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,6 @@ func TestVersionHandling(t *testing.T) {
314314
// test that export data can be imported
315315
_, err := Import(make(map[string]*types.Package), pkgpath, dir, nil)
316316
if err != nil {
317-
// ok to fail if it fails with a newer version error for select files
318-
if strings.Contains(err.Error(), "newer version") {
319-
switch name {
320-
case "test_go1.11_999b.a", "test_go1.11_999i.a":
321-
continue
322-
}
323-
// fall through
324-
}
325317
t.Errorf("import %q failed: %v", pkgpath, err)
326318
continue
327319
}
@@ -351,7 +343,7 @@ func TestVersionHandling(t *testing.T) {
351343
_, err = Import(make(map[string]*types.Package), pkgpath, corruptdir, nil)
352344
if err == nil {
353345
t.Errorf("import corrupted %q succeeded", pkgpath)
354-
} else if msg := err.Error(); !strings.Contains(msg, "version skew") {
346+
} else if msg := err.Error(); !strings.Contains(msg, "internal error") {
355347
t.Errorf("import %q error incorrect (%s)", pkgpath, msg)
356348
}
357349
}

internal/gcimporter/iimport.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func iimportCommon(fset *token.FileSet, getPackage GetPackageFunc, data []byte,
131131
} else if version > currentVersion {
132132
err = fmt.Errorf("cannot import %q (%v), export data is newer version - update tool", path, e)
133133
} else {
134-
err = fmt.Errorf("cannot import %q (%v), possibly version skew - reinstall package", path, e)
134+
err = fmt.Errorf("internal error while importing %q (%v); please report an issue", path, e)
135135
}
136136
}
137137
}()
@@ -140,11 +140,8 @@ func iimportCommon(fset *token.FileSet, getPackage GetPackageFunc, data []byte,
140140
r := &intReader{bytes.NewReader(data), path}
141141

142142
if bundle {
143-
bundleVersion := r.uint64()
144-
switch bundleVersion {
145-
case bundleVersion:
146-
default:
147-
errorf("unknown bundle format version %d", bundleVersion)
143+
if v := r.uint64(); v != bundleVersion {
144+
errorf("unknown bundle format version %d", v)
148145
}
149146
}
150147

internal/gcimporter/testdata/versions/test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
//
1414
// go build -o test_go1.$X_$Y.a test.go
1515
//
16-
// with $X = Go version and $Y = export format version
17-
// (add 'b' or 'i' to distinguish between binary and
18-
// indexed format starting with 1.11 as long as both
19-
// formats are supported).
16+
// with $X = Go version and $Y = export format version (e.g. 'i', 'u').
2017
//
2118
// Make sure this source is extended such that it exercises
2219
// whatever export format change has taken place.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

internal/gcimporter/ureader_yes.go

+9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package gcimporter
1111

1212
import (
13+
"fmt"
1314
"go/token"
1415
"go/types"
1516
"sort"
@@ -63,6 +64,14 @@ type typeInfo struct {
6364
}
6465

6566
func UImportData(fset *token.FileSet, imports map[string]*types.Package, data []byte, path string) (_ int, pkg *types.Package, err error) {
67+
if !debug {
68+
defer func() {
69+
if x := recover(); x != nil {
70+
err = fmt.Errorf("internal error in importing %q (%v); please report an issue", path, x)
71+
}
72+
}()
73+
}
74+
6675
s := string(data)
6776
s = s[:strings.LastIndex(s, "\n$$\n")]
6877
input := pkgbits.NewPkgDecoder(path, s)

0 commit comments

Comments
 (0)