Skip to content

Commit

Permalink
internal/report: make openSourceFile cognizant of Go modules
Browse files Browse the repository at this point in the history
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 google#611
  • Loading branch information
odeke-em committed Mar 21, 2021
1 parent cbba55b commit c889f8b
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion internal/report/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c889f8b

Please sign in to comment.