diff --git a/internal/report/source.go b/internal/report/source.go index 4f841eff5..7bc1bb709 100644 --- a/internal/report/source.go +++ b/internal/report/source.go @@ -917,8 +917,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")) + } // 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)