Skip to content

Commit fe8854a

Browse files
committed
Update package name parsing to match Go 1.20+ behavior
1 parent 5f86090 commit fe8854a

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

stacktrace.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sentry
22

33
import (
4+
"errors"
5+
"fmt"
46
"go/build"
57
"reflect"
68
"runtime"
@@ -350,14 +352,44 @@ func callerFunctionName() string {
350352
return baseName(callerFrame.Function)
351353
}
352354

355+
func goMajorMinorVersion() (major, minor int, err error) {
356+
releaseTags := build.Default.ReleaseTags
357+
if len(releaseTags) == 0 {
358+
return -1, -1, errors.New("no release tags")
359+
}
360+
361+
// The last release tag is presumed to be the current release. The current
362+
// release will be in the form 'go1.2.3'.
363+
//
364+
// See https://pkg.go.dev/go/build#Context
365+
if _, err := fmt.Sscanf(releaseTags[len(releaseTags)-1], "go%d.%d", &major, &minor); err != nil {
366+
return -1, -1, fmt.Errorf("sscanf: %w", err)
367+
}
368+
369+
return major, minor, nil
370+
}
371+
353372
// packageName returns the package part of the symbol name, or the empty string
354373
// if there is none.
355374
// It replicates https://golang.org/pkg/debug/gosym/#Sym.PackageName, avoiding a
356375
// dependency on debug/gosym.
357376
func packageName(name string) string {
358-
// A prefix of "type." and "go." is a compiler-generated symbol that doesn't belong to any package.
359-
// See variable reservedimports in cmd/compile/internal/gc/subr.go
360-
if strings.HasPrefix(name, "go.") || strings.HasPrefix(name, "type.") {
377+
// Since go1.20, a prefix of "type:" and "go:" is a compiler-generated symbol,
378+
// they do not belong to any package.
379+
//
380+
// See cmd/compile/internal/base/link.go:ReservedImports variable.
381+
major, minor, err := goMajorMinorVersion()
382+
if err != nil {
383+
return ""
384+
}
385+
goVersionGreaterThanOrEqualTo120 := major > 1 || (major == 1 && minor >= 20)
386+
387+
if goVersionGreaterThanOrEqualTo120 && (strings.HasPrefix(name, "go:") || strings.HasPrefix(name, "type:")) {
388+
return ""
389+
}
390+
391+
// For go1.18 and below, the prefix are "type." and "go." instead.
392+
if !goVersionGreaterThanOrEqualTo120 && (strings.HasPrefix(name, "go.") || strings.HasPrefix(name, "type.")) {
361393
return ""
362394
}
363395

0 commit comments

Comments
 (0)