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 #611
  • Loading branch information
odeke-em committed Mar 21, 2021
1 parent cbba55b commit 075f40d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion internal/report/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 075f40d

Please sign in to comment.