Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/report: make openSourceFile cognizant of Go modules #612

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to somehow make sure that the SourcePath field in options is set correctly earlier?

And if it is the case that all/most Go users are using "go tool pprof" then perhaps we can just arrange for the Go invocation code to pass the right source path settings? Or are you worried about "pprof" invocations that are not via "go tool pprof"?

// 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"))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps nitpicking slightly, but this is not entirely correct; the only way to reliably find these out would be to run go env -json GOMODCACHE GOROOT.

Using just os.Getenv("GOPATH") will not see if the user set up their own GOMODCACHE, nor will it work with env vars set with go env -w.

// 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