This is caused by a specific interaction between two steps:
When parsing the import statements of each file in the requested package to construct the alias map, they are processed in alphabetical filename order.
When adding an alias to a map, it overwrites any existing alias for that import path.
Combined, the above means the valid bar alias is overwritten by the invalid _ that gets processed later, and applied to the imported types in the generated functions.
Solution
Ignore anonymous imports when constructing the alias map. This means a valid alias will never be overwritten, and as anonymous imports will always be unused by a mock, they will always be safe to ignore.
Problem
Given the following two files in a package:
file1.go:
file2.go:
, suppose you want to generate a mock of
Barrier
.Today, running
moq -out barrier.mock.go . Barrier
would produce the following:barrier.mock.go
, which is invalid Go :cry: .
Why
This is caused by a specific interaction between two steps:
Combined, the above means the valid
bar
alias is overwritten by the invalid_
that gets processed later, and applied to the imported types in the generated functions.Solution
Ignore anonymous imports when constructing the alias map. This means a valid alias will never be overwritten, and as anonymous imports will always be unused by a mock, they will always be safe to ignore.