Skip to content

Commit 10ca881

Browse files
authored
fix: Source code module path parsing (#4609)
If a folder contained a dot (e.g. /home/christian/.golang/[...]), it would be wrongly picked up as module path domain. This will ensure we trim this part of.
1 parent d9d6a1d commit 10ca881

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

pkg/frontend/vcs/source/golang/modules.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func ParseModuleFromPath(path string) (Module, bool) {
4343
}
4444
filePath := parts[1][first+1:]
4545
modulePath := parts[0]
46+
47+
// The go mod folder typically starts with "pkg/mod". If that segment can be found, shorten the module path, so no other folders with dots get accidentally picked up.
48+
if pos := strings.Index(modulePath, "/pkg/mod/"); pos > 0 {
49+
modulePath = modulePath[pos:]
50+
}
51+
4652
// searching for the first domain name
4753
domainParts := strings.Split(modulePath, "/")
4854
for i, part := range domainParts {

pkg/frontend/vcs/source/golang/modules_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,28 @@ func TestParseModulePath(t *testing.T) {
104104
false,
105105
Module{},
106106
},
107+
{
108+
"/Users/pyroscope/.golang/packages/pkg/mod/golang.org/toolchain@v0.0.1-go1.24.6.darwin-arm64/src/net/http/server.go",
109+
true,
110+
Module{
111+
Version: module.Version{
112+
Path: "golang.org/toolchain",
113+
Version: "v0.0.1-go1.24.6.darwin-arm64",
114+
},
115+
FilePath: "src/net/http/server.go",
116+
},
117+
},
118+
{
119+
"/Users/pyroscope/.golang/packages/pkg/mod/github.com/opentracing-contrib/go-stdlib@v1.0.0/nethttp/server.go",
120+
true,
121+
Module{
122+
Version: module.Version{
123+
Path: "github.com/opentracing-contrib/go-stdlib",
124+
Version: "v1.0.0",
125+
},
126+
FilePath: "nethttp/server.go",
127+
},
128+
},
107129
} {
108130
t.Run(tt.input, func(t *testing.T) {
109131
mod, ok := ParseModuleFromPath(tt.input)

0 commit comments

Comments
 (0)