diff --git a/CHANGELOG.md b/CHANGELOG.md index 576f390..7ea05e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ * Start showing inlined functions in stack trace [#208](https://github.com/bugsnag/bugsnag-go/pull/208) +* Stop trimming everything before "main.go" on main packages + [#217](https://github.com/bugsnag/bugsnag-go/pull/217) + [Chris Duncan](https://github.com/veqryn) + ## 2.2.0 (2022-10-12) ### Enhancements diff --git a/event.go b/event.go index 97ea844..4241282 100644 --- a/event.go +++ b/event.go @@ -198,7 +198,7 @@ func generateStacktrace(err *errors.Error, config *Configuration) []StackFrame { inProject := config.isProjectPackage(frame.Package) // remove $GOROOT and $GOHOME from other frames - if idx := strings.Index(file, frame.Package); idx > -1 && frame.Package != "main" { + if idx := strings.Index(file, frame.Package); idx > -1 { file = file[idx:] } if inProject { diff --git a/v2/configuration.go b/v2/configuration.go index 6531e9e..48680bb 100644 --- a/v2/configuration.go +++ b/v2/configuration.go @@ -232,10 +232,7 @@ func (config *Configuration) isProjectPackage(_pkg string) bool { } func (config *Configuration) stripProjectPackages(file string) string { - trimmedFile := file - if strings.HasPrefix(trimmedFile, config.SourceRoot) { - trimmedFile = strings.TrimPrefix(trimmedFile, config.SourceRoot) - } + trimmedFile := strings.TrimPrefix(file, config.SourceRoot) for _, p := range config.ProjectPackages { if len(p) > 2 && p[len(p)-2] == '/' && p[len(p)-1] == '*' { p = p[:len(p)-1] diff --git a/v2/event.go b/v2/event.go index f70a061..742084e 100644 --- a/v2/event.go +++ b/v2/event.go @@ -197,10 +197,19 @@ func generateStacktrace(err *errors.Error, config *Configuration) []StackFrame { file := frame.File inProject := config.isProjectPackage(frame.Package) - // remove $GOROOT and $GOHOME from other frames + // This will trim path before package name for external packages and golang default packages + // Excluding main package as it's special case + // This will NOT trim paths for packages in current module because path won't contain the package name + // Example: path is "/user/name/work/internal/internal.go" and module package name is "example.com/mymodule/internal" if idx := strings.Index(file, frame.Package); idx > -1 && frame.Package != "main" { file = file[idx:] } + + // This should trim path for main and other current module packages with correct config + // If input path is "/user/name/work/internal/internal.go" + // SourceRoot is "/user/name/work" and ProjectPackages are []string{"main*", "example.com/mymodule/**"} + // Then when package name is "example.com/mymodule/internal" + // The path will be trimmed to "/internal/internal.go" if inProject { file = config.stripProjectPackages(file) }