Imports in mockery generated files do not comply with goimports standard when package name does not match directoryname
Author: ArnoSenCreated Jun 5, 2026Updated Jun 11, 2026
Description
At our company we use mockery extensively and it is a great tool. Thanks a lot for all the effort that you have put in!
When you import a package whose package name does not match the directory name, mockery generates go files that are changed after running 'goimports'. In our case, this makes our pipelines fail if I do not 'post process' generated files with 'goimports'
Reproducer
- Install mockery 3.7.0 .
- Create a new 'mockery' go project in an empty directory with 'go mod init mockery'
- Create the below directories and files: ├── foo │ └── bar.go ├── .mockery └── pkg └── pkg.go
foo/bar.go:
package bar // NB: note that the package name is different from the directory name
type Bar struct{}pkg/pkg.go:
package pkg
import (
bar "mockery/foo"
)
type Getter interface {
Get() *bar.Bar
}.mockery:
packages:
mockery/pkg:
interfaces:
Getter:
config:
filename: "pkg_mock_test.go"- Run 'mockery'
- You should see a new file pkg/pkg_mock_test.go and get the first lines:
$ head pkg/pkg_mock_test.go
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: testify
package pkg
import (
"mockery/foo"
mock "github.com/stretchr/testify/mock"- Run 'goimports' on the generated file:
$ goimports pkg/pkg_mock_test.go | head
// Code generated by mockery; DO NOT EDIT.
// github.com/vektra/mockery
// template: testify
package pkg
import (
bar "mockery/foo"
mock "github.com/stretchr/testify/mock"- Note that there is a change:
Mockery generated
"mockery/foo"Goimports changed that tobar "mockery/foo"
Expected behavior
I would expect after generation of the mocks with mockery, the format complies with 'goimports'.
In this specific example I would expect the import line to be bar "mockery/foo" instead of "mockery/foo"
- If you think this could be improved/fixed, I would be happy to create a PR for it.
Mockery version
v3.7.0
Installation Mechanism
-
go get - Pre-built release
- homebrew
- Other: [please describe]
Go version
Version of Go used to build mockery from source (if applicable).
Source: vektra/mockery