From 075f40d2b9dc0225aa87a7a29af421abb2cf49a8 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 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/report/source.go b/internal/report/source.go index 4f841eff..8d920ed5 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,19 @@ 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: + // * $GOPATH/pkg/mod + // * runtime.GOROOT()/src + // 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)