From 1a87bd51fed86bdabb47a5b6072a9cbfd87b5fdd Mon Sep 17 00:00:00 2001 From: Boston Cartwright Date: Tue, 22 Oct 2024 10:59:33 -0600 Subject: [PATCH] fix: import and arg collision --- .../import_collision/internalpackage/foo.go | 3 + .../tests/import_collision/p2/mocks/mocks.go | 55 +++++++++++++++++++ .../internal/tests/import_collision/p2/p2.go | 12 ++++ mockgen/mockgen.go | 18 +++++- 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 mockgen/internal/tests/import_collision/internalpackage/foo.go create mode 100644 mockgen/internal/tests/import_collision/p2/mocks/mocks.go create mode 100644 mockgen/internal/tests/import_collision/p2/p2.go diff --git a/mockgen/internal/tests/import_collision/internalpackage/foo.go b/mockgen/internal/tests/import_collision/internalpackage/foo.go new file mode 100644 index 0000000..eaedd5b --- /dev/null +++ b/mockgen/internal/tests/import_collision/internalpackage/foo.go @@ -0,0 +1,3 @@ +package internalpackage + +type FooExported struct{} diff --git a/mockgen/internal/tests/import_collision/p2/mocks/mocks.go b/mockgen/internal/tests/import_collision/p2/mocks/mocks.go new file mode 100644 index 0000000..3b8fca6 --- /dev/null +++ b/mockgen/internal/tests/import_collision/p2/mocks/mocks.go @@ -0,0 +1,55 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: go.uber.org/mock/mockgen/internal/tests/import_collision/p2 (interfaces: Mything) +// +// Generated by this command: +// +// mockgen -destination=mocks/mocks.go -package=internalpackage . Mything +// + +// Package internalpackage is a generated GoMock package. +package internalpackage + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + internalpackage "go.uber.org/mock/mockgen/internal/tests/import_collision/internalpackage" +) + +// MockMything is a mock of Mything interface. +type MockMything struct { + ctrl *gomock.Controller + recorder *MockMythingMockRecorder + isgomock struct{} +} + +// MockMythingMockRecorder is the mock recorder for MockMything. +type MockMythingMockRecorder struct { + mock *MockMything +} + +// NewMockMything creates a new mock instance. +func NewMockMything(ctrl *gomock.Controller) *MockMything { + mock := &MockMything{ctrl: ctrl} + mock.recorder = &MockMythingMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockMything) EXPECT() *MockMythingMockRecorder { + return m.recorder +} + +// DoThat mocks base method. +func (m *MockMything) DoThat(arg0 int) internalpackage.FooExported { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DoThat", arg0) + ret0, _ := ret[0].(internalpackage.FooExported) + return ret0 +} + +// DoThat indicates an expected call of DoThat. +func (mr *MockMythingMockRecorder) DoThat(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DoThat", reflect.TypeOf((*MockMything)(nil).DoThat), arg0) +} diff --git a/mockgen/internal/tests/import_collision/p2/p2.go b/mockgen/internal/tests/import_collision/p2/p2.go new file mode 100644 index 0000000..e6200b7 --- /dev/null +++ b/mockgen/internal/tests/import_collision/p2/p2.go @@ -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 +} diff --git a/mockgen/mockgen.go b/mockgen/mockgen.go index b5365de..4353fb8 100644 --- a/mockgen/mockgen.go +++ b/mockgen/mockgen.go @@ -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)