internal/report: use better heuristic for locating the source code #977
+319
−110
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current heuristic for locating the source code for profile functions doesn't work in the following cases when running pprof with default options:
It cannot find source code for standard Go packages. Users need to specify -source_path=
go env GOROOTas a workaround.It cannot find source code for external modules. Users need to specify -source_path=
go env GOMODCACHEas a workaround. But this workaround stops working if external module names contain uppercase chars. For example, github.com/FooBar/Baz, because Go stores sources for such module names into $GOMODCACHE/github.com/!foo!bar/!baz directory according to https://go.dev/ref/mod#module-cache , while pprof is unaware of such a conversion.It cannot find source code at the vendor directory. Users need to specify -source_path=
pwd/vendor as a workaround. But this workaround doesn't work for vendored modules, since their names contain module version in the form module_name@version, while the source code for the module_name is stored inside vendor/module_name directory.It cannot find source code for profiles obtained from Go executables built with -trimpath command-line flag, if their sources are put in the directory with the name other than the module name.
This significantly reduces pprof usability, since it is very hard to inspect sources with
listcommand. Users have to create horrible hacks with -source_path and -trim_path command-line flags such as VictoriaMetrics/VictoriaLogs#893 , but these hacks do not cover properly all the cases mentioned above.This commit covers all the cases mentioned above, so pprof properly detects source code with default settings, without the need to specify -trim_path and -source_path command-line flags.