From d11e9de3d540a2154a5494e0bf9b32c3ecbcd019 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sat, 20 Mar 2021 22:18:40 -0700 Subject: [PATCH] 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 --- internal/report/source.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/report/source.go b/internal/report/source.go index 4f841eff5..3d25e194c 100644 --- a/internal/report/source.go +++ b/internal/report/source.go @@ -25,6 +25,7 @@ import ( "os" "path/filepath" "regexp" + "runtime" "sort" "strconv" "strings" @@ -917,8 +918,14 @@ func openSourceFile(path, searchPath, trim string) (*os.File, error) { f, err := os.Open(path) return f, err } + possibleBases := filepath.SplitList(searchPath) + if gopath := os.Getenv("GOPATH"); gopath != "" { + // We can also look through the $GOPATH/pkg/mod in case the file originates + // from Go modules. See https://github.com/google/pprof/issues/611. + possibleBases = append(possibleBases, filepath.Join(gopath, "pkg", "mod"), filepath.Join(runtime.GOROOT(), "src")) + } // Scan each component of the path. - for _, dir := range filepath.SplitList(searchPath) { + for _, dir := range possibleBases { // Search up for every parent of each possible path. for { filename := filepath.Join(dir, path)