Skip to content

Commit d11e9de

Browse files
committed
internal/report: make openSourceFile cognizant of Go modules
This change adds $GOPATH/pkg/mod as a possible base to search from given that Go modules have been the norm since Go1.11 and we are currently at Go1.16/1.17, hence support for Go modules. Fixes #611
1 parent cbba55b commit d11e9de

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

internal/report/source.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"path/filepath"
2727
"regexp"
28+
"runtime"
2829
"sort"
2930
"strconv"
3031
"strings"
@@ -917,8 +918,14 @@ func openSourceFile(path, searchPath, trim string) (*os.File, error) {
917918
f, err := os.Open(path)
918919
return f, err
919920
}
921+
possibleBases := filepath.SplitList(searchPath)
922+
if gopath := os.Getenv("GOPATH"); gopath != "" {
923+
// We can also look through the $GOPATH/pkg/mod in case the file originates
924+
// from Go modules. See https://github.com/google/pprof/issues/611.
925+
possibleBases = append(possibleBases, filepath.Join(gopath, "pkg", "mod"), filepath.Join(runtime.GOROOT(), "src"))
926+
}
920927
// Scan each component of the path.
921-
for _, dir := range filepath.SplitList(searchPath) {
928+
for _, dir := range possibleBases {
922929
// Search up for every parent of each possible path.
923930
for {
924931
filename := filepath.Join(dir, path)

0 commit comments

Comments
 (0)