Skip to content

Commit

Permalink
fix: import and arg collision
Browse files Browse the repository at this point in the history
  • Loading branch information
bstncartwright committed Oct 22, 2024
1 parent b8222fa commit 1a87bd5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package internalpackage

type FooExported struct{}
55 changes: 55 additions & 0 deletions mockgen/internal/tests/import_collision/p2/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions mockgen/internal/tests/import_collision/p2/p2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package p2

//go:generate mockgen -destination=mocks/mocks.go -package=internalpackage . Mything

import (
"go.uber.org/mock/mockgen/internal/tests/import_collision/internalpackage"
)

type Mything interface {
// issue here, is that the variable has the same name as an imported package.
DoThat(internalpackage int) internalpackage.FooExported
}
18 changes: 16 additions & 2 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,16 +766,30 @@ func (g *generator) getArgNames(m *model.Method, in bool) []string {
params = m.Out
}
argNames := make([]string, len(params))

// check if the name already exists as an imported package name
// which would cause a name collision
nameExistsAsPackage := func(name string) bool {
for _, symbolName := range g.packageMap {
if symbolName == name {
return true
}
}
return false
}

for i, p := range params {
name := p.Name
if name == "" || name == "_" {

if name == "" || name == "_" || nameExistsAsPackage(name) {
name = fmt.Sprintf("arg%d", i)
}
argNames[i] = name
}
if m.Variadic != nil && in {
name := m.Variadic.Name
if name == "" {

if name == "" || nameExistsAsPackage(name) {
name = fmt.Sprintf("arg%d", len(params))
}
argNames = append(argNames, name)
Expand Down

0 comments on commit 1a87bd5

Please sign in to comment.