Skip to content

Commit

Permalink
fix: go_repository: never shadow a module with a compatibility mappin…
Browse files Browse the repository at this point in the history
…g for major versions. (#1608)

Fixes #1595.

Co-authored-by: Fabian Meumertzheim <[email protected]>
  • Loading branch information
reltuk and fmeum committed Aug 19, 2023
1 parent d1b5207 commit 96f4d66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
23 changes: 18 additions & 5 deletions repo/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,28 @@ func NewRemoteCache(knownRepos []Repo) (r *RemoteCache, cleanup func() error) {
// allowed to use them. However, we'll return the same result nearly all
// the time, and simpler is better.
for _, repo := range knownRepos {
path := pathWithoutSemver(repo.GoPrefix)
if path == "" || r.root.cache[path] != nil {
newPath := pathWithoutSemver(repo.GoPrefix)
if newPath == "" {
continue
}
r.root.cache[path] = r.root.cache[repo.GoPrefix]
// Avoid adding the semver-less path for this module if there
// is another known module which already covers this path.
// See https://github.com/bazelbuild/bazel-gazelle/issues/1595.
found := false
for prefix := newPath; prefix != "." && prefix != "/"; prefix = path.Dir(prefix) {
if _, ok := r.root.cache[prefix]; ok {
found = true
break
}
}
if found {
continue
}
r.root.cache[newPath] = r.root.cache[repo.GoPrefix]
if e := r.remote.cache[repo.GoPrefix]; e != nil {
r.remote.cache[path] = e
r.remote.cache[newPath] = e
}
r.mod.cache[path] = r.mod.cache[repo.GoPrefix]
r.mod.cache[newPath] = r.mod.cache[repo.GoPrefix]
}

return r, r.cleanup
Expand Down
12 changes: 12 additions & 0 deletions repo/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,18 @@ func TestMod(t *testing.T) {
}},
wantModPath: "example.com/known",
wantName: "known",
}, {
desc: "semver_less_path_is_safe",
importPath: "example.com/known/internal/endpoints",
repos: []Repo{{
Name: "known",
GoPrefix: "example.com/known",
}, {
Name: "known_internal_endpoints_v2",
GoPrefix: "example.com/known/internal/endpoints/v2",
}},
wantModPath: "example.com/known",
wantName: "known",
}, {
desc: "lookup",
importPath: "example.com/stub/v2/foo",
Expand Down

0 comments on commit 96f4d66

Please sign in to comment.