Skip to content

Commit b869422

Browse files
committed
mockgen: Sanitize the "any" package name
When a package name is "any" mockgen will use that as a package name in the generated code which causes issues due to colliding with Go's "any" type. Update the sanitization logic to prevent this collision.
1 parent 6dd8fe5 commit b869422

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package any
2+
3+
// Any is a type of a package that tests the sanitization of imported packages
4+
// named any.
5+
type Any struct {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package sanitization
2+
3+
import (
4+
"go.uber.org/mock/mockgen/internal/tests/sanitization/any"
5+
)
6+
7+
//go:generate mockgen -destination mock.go . AnyMock
8+
9+
type AnyMock interface {
10+
Do(a *any.Any, b int)
11+
}

mockgen/internal/tests/sanitization/mock.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mockgen/mockgen.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
378378
}
379379

380380
i := 0
381-
for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() {
381+
for localNames[pkgName] || token.Lookup(pkgName).IsKeyword() || pkgName == "any" {
382382
pkgName = base + strconv.Itoa(i)
383383
i++
384384
}

0 commit comments

Comments
 (0)