Skip to content

Commit d6a7932

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 d6a7932

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

internal/report/source.go

Lines changed: 13 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,19 @@ 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:
924+
// * $GOPATH/pkg/mod
925+
// * runtime.GOROOT()/src
926+
// in case the file originates from Go modules.
927+
//See https://github.com/google/pprof/issues/611.
928+
possibleBases = append(possibleBases,
929+
filepath.Join(gopath, "pkg", "mod"),
930+
filepath.Join(runtime.GOROOT(), "src"))
931+
}
920932
// Scan each component of the path.
921-
for _, dir := range filepath.SplitList(searchPath) {
933+
for _, dir := range possibleBases {
922934
// Search up for every parent of each possible path.
923935
for {
924936
filename := filepath.Join(dir, path)

0 commit comments

Comments
 (0)