Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit ecd7b87

Browse files
committed
gps: Fix reflection handling in PackageTree.Copy()
1 parent e4909ed commit ecd7b87

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

internal/gps/pkgtree/pkgtree.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,21 @@ func (t PackageTree) Copy() PackageTree {
624624
var poe2 PackageOrErr
625625

626626
if poe.Err != nil {
627-
poe2.Err = reflect.New(reflect.ValueOf(poe.Err).Elem().Type()).Interface().(error)
627+
refl := reflect.ValueOf(poe.Err)
628+
switch refl.Kind() {
629+
case reflect.Ptr:
630+
poe2.Err = reflect.New(refl.Elem().Type()).Interface().(error)
631+
case reflect.Slice:
632+
err2 := reflect.MakeSlice(refl.Type(), refl.Len(), refl.Len())
633+
reflect.Copy(err2, refl)
634+
poe2.Err = err2.Interface().(error)
635+
default:
636+
// This shouldn't be too onerous to maintain - the set of errors
637+
// we can get here is restricted by what ListPackages() allows.
638+
// So just panic if one is outside the expected kinds of ptr or
639+
// slice, as that would mean we've missed something notable.
640+
panic(fmt.Sprintf("unrecognized PackgeOrErr error type, %T", poe.Err))
641+
}
628642
} else {
629643
poe2.P = poe.P
630644
il, til := len(poe.P.Imports), len(poe.P.TestImports)

0 commit comments

Comments
 (0)