Skip to content

Commit cc926a7

Browse files
authored
fix: tryFindGo doesn't trim repo path when absolute path is used (#4608)
When an absolute path is used like `/home/christian/github.com/grafana/pyroscope/[....]`, we are not matching the repository correctly and have to try quite a few files before we truncated enough folders. This change ensures that these cases are dealt with similar like a path starting with the repo name.
1 parent 10ca881 commit cc926a7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pkg/frontend/vcs/source/find_go.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,15 @@ func (ff FileFinder) tryFindGoFile(ctx context.Context, maxAttempts int) (*vcsv1
112112
if maxAttempts <= 0 {
113113
return nil, errors.New("invalid max attempts")
114114
}
115-
// Try to find the file in the repo.
116-
path := strings.TrimPrefix(ff.path, strings.Join([]string{ff.repo.GetHostName(), ff.repo.GetOwnerName(), ff.repo.GetRepoName()}, "/"))
115+
116+
// trim repo path (e.g. "github.com/grafana/pyroscope/") in path
117+
path := ff.path
118+
repoPath := strings.Join([]string{ff.repo.GetHostName(), ff.repo.GetOwnerName(), ff.repo.GetRepoName(), ""}, "/")
119+
if pos := strings.Index(path, repoPath); pos != -1 {
120+
path = path[len(repoPath)+pos:]
121+
}
122+
123+
// now try to find file in repo
117124
path = strings.TrimLeft(path, "/")
118125
attempts := 0
119126
for {

pkg/frontend/vcs/source/find_go_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Test_tryFindGoFile(t *testing.T) {
5757
expectedError: nil,
5858
},
5959
{
60-
name: "path with repository preffix",
60+
name: "path with relative repository prefix",
6161
searchedPath: "github.com/grafana/pyroscope/main.go",
6262
rootPath: "",
6363
repo: pyroscopeRepo,
@@ -66,6 +66,16 @@ func Test_tryFindGoFile(t *testing.T) {
6666
expectedSearchedPaths: []string{"/main.go"},
6767
expectedError: nil,
6868
},
69+
{
70+
name: "path with absolute repository prefix",
71+
searchedPath: "/Users/pyroscope/git/github.com/grafana/pyroscope/main.go",
72+
rootPath: "",
73+
repo: pyroscopeRepo,
74+
clientMock: &VCSClientMock{fileToFind: "/main.go"},
75+
attempts: 1,
76+
expectedSearchedPaths: []string{"/main.go"},
77+
expectedError: nil,
78+
},
6979
{
7080
name: "not found, attempts exceeded",
7181
searchedPath: "/var/service1/src/main.go",

0 commit comments

Comments
 (0)