Skip to content

Go imports broken with v3.2.5 #1041

@breml

Description

@breml

Description

Go imports are broken with v3.2.5 (with v3.2.4 it is OK, so I assume, that #1032 did break this).

Reproducer

I can not share the actual template I observed the issue with, but this stripped down template, produces the problem for me as well:

package {{ .PkgName }}

import (
{{ range .Imports -}}
	{{ .Alias }} "{{ .Path }}"
{{ end -}}
)


{{ range $i, $wrapper := .Interfaces }}

{{ $decorator := (or .TemplateData.DecoratorName (printf "%sWithSlog" $wrapper.Name)) }}

type {{$decorator}} struct {
	_log  *slog.Logger
	_base {{ $.SrcPkgQualifier }}{{ $wrapper.Name }}
}

func New{{$decorator}}(base {{ $.SrcPkgQualifier }}{{ $wrapper.Name }}, log *slog.Logger) {{$decorator}} {
	return {{$decorator}}{
		_base: base,
		_log: log,
	}
}

{{ end }}

Expected behavior

The above template is extracted from a wrapper (not a mock) around an interface using the log/slog package from the stdlib, but v3.2.5 fails to add the necessary import in this case.
The import is not present in the interface, that is mocked, so mockery needs to figure out, what additional packages are used by the generated code. In the actual package, there is an other internal package, which also is used in the wrapper (but not referenced by the interface), which is also not added.
Interestingly, since I removed all the actual wrapping functions, mockery added the context package even though, the now generated code does no longer require it.

In my opinion, this is exactly what goimports is for and I don't share the opinion, that this should be handled by mockery it self. In this case I would really recommend to rely on the package provided by the Go team.

Mockery version

v3.2.5

Installation Mechanism

  • go get

Go version

Go version: 1.23.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions