Skip to content

Generated sub-package contains circular import #43

@treyburn

Description

@treyburn

Perhaps I am missing a command line arg - but I do not see anything in the documentation that addresses this.

Using go-mockgen@v1.3.7 I see the following error related to generating a sub-package and circular imports to the parent package.

Using the following code and go:generate directive:

package foo

//go:generate go-mockgen -f github.com/user/mod/pkg/foo -i Fooer -o ./mock/mock_fooer.go
type Fooer interface {
	Foo()
}

Generates the following code:

package mock

import foo "github.com/user/mod/pkg/foo"

// NewMockFooerFrom creates a new mock of the MockFooer interface. All
// methods delegate to the given implementation, unless overwritten.
func NewMockFooerFrom(i foo.Fooer) *MockFooer {
	return &MockFooer{
		FooFunc: &FooerFooFunc{
			defaultHook: i.Foo,
		},
	}
}

This causes a circular import error where the intended purpose is to import from the mock package in the parent foo package.

I would expect that the generated code would create a surrogate interface when the generate code is placed in a sub-package. Along the lines of:

package mock

// surrogateMockFooer is a copy of the Fooer interface (from the package
// github.com/user/mod/pkg/foo). It is redefined here to avoid
// circular imports in the parent package.
type surrogateMockFooer interface {
	Foo()
}

// NewMockFooerFrom creates a new mock of the MockFooer interface. All
// methods delegate to the given implementation, unless overwritten.
func NewMockFooerFrom(i surrogateMockFooer) *MockFooer {
	return &MockFooer{
		FooFunc: &FooerFooFunc{
			defaultHook: i.Foo,
		},
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions