-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circular dependency in go 1.23 when mock is used in the package where it's defined #839
Comments
Oh gosh, this is a tricky one. Sadly, the behavior where mockery would resolve down to the underlying aliased type was incorrect for a long time. Mockery having been changed to refer to the alias name instead of the underlying type fixed a lot of deal-breaking issues. So this fix highlighted a latent problem with the way you generate mocks. This is generally why I recommend generating mocks in the same package as the tests because of this exact problem. But, I recognize I can't just ask you to change your codebase and ignore the issue altogether. I can offer a solution of adding a parameter that when enabled will tell mockery to resolve back to the underlying type. Otherwise, I don't know what else to do, we won't revert the change. |
Actually, another fix that might help you is to set |
Looks like mocks are generated in a separate package by default. If I understand correctly |
Not really no. It would just revert mockery to the old behavior. The only reason for us to add it is to get around the fact that your mock generation is now broken on the latest version of mockery. |
Could you please share links to issues that were related to old alias resolution behaviour? |
There's a whole bunch of them, I'll post some related pulls/issues:
This is fair. However, there are many cases where a mock does not have to import anything from the original package, in which case placing it in a separate package wouldn't result in circular dependencies. As far as considering this a breaking change, I think I agree. I'll look into adding this parameter. |
@max-melentyev can you confirm you are not using the |
I don't think I use it: here is a repo reproducing the issue, it doesn't have any custom config: https://github.com/max-melentyev/mockery-issue. Regarding the issues, for me it looks like #331 is the initial issue that required the change to not resolve aliases. I wonder if it's possible to make mockery resolve only local aliases. For example: package parent
type T1 = int
type T2 = int
/////////////////////////////////////
package app
import "parent"
type A1 = parent.T1
//go:generate mockery --quiet --name I
type I interface{ F(A1, parent.T2) }
///////////////////////////////////
// result:
func (_m *I) F(_a0 parent.T1, _a1 parent.T2) {
// So A1 is resolved because it's local alias (don't have package prefix).
// And it's resolved only in the scope of current package.
// parent.T2 is not resolved to `int` because it's not local (has package prefix). |
Though if we have a new type like So seems like putting all mocks to the current package rather than to |
@max-melentyev PR #843 adds a new parameter that by default will revert mockery to its previous behavior, which should fix your issue. I added a deprecation warning to mockery that this parameter needs to be set to I will double check that it fixes your reproducer at some point soon, but let me know if this looks good to you in the meantime. |
Description
Hey, looks like #808 results in a circular dependency if mock is used in the package where it's defined:
Here is a sample repo that reproduces the issue: https://github.com/max-melentyev/mockery-issue
Mockery Version
2.46.3
Go Version
1.23
Installation Method
Steps to Reproduce
git clone https://github.com/max-melentyev/mockery-issue
make test
Expected Behavior
Mock uses original type (
int
) rather than its local alias (p.T
) and this allows using mock in the same package (p
).Actual Behavior
p/mocks
uses typep.T
and this doesn't allow to use this mock from the packagep
:The text was updated successfully, but these errors were encountered: