-
Notifications
You must be signed in to change notification settings - Fork 413
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
error when loading package, can only [...] in module-aware mode #706
Comments
I was thinking that maybe the logging could be improved, but it will be hard to say which package failed to parse because we pass all the packages you specified in It's hard to say what's wrong with your config without looking at it. I'm guessing |
The package name in the mockery config exactly matches the package name in the go.mod file. I've tried using the exact same config in a barebones project with one interface but didn't get the same issue (but also it didn't actually do what I was expecting it to do, I somehow need to trim the module package prefix off the directory template). In my barebones project the config is
|
Changed dir to |
Looking at the code,
It looks like the error could include |
I've forked mockery to make above change to logging. The "package" causing the issue was actually a node_modules directory containing a directory starting with |
@devnev very interesting, thanks for taking the time to dig into it. This is a strange edge case I've never seen before. Perhaps the |
Weirdly, |
So, the only way I can envision this would happen is if |
Confirmed, it contains a go module (go.mod/go.sum) and a main.go file. Maybe the presence of the go module is why |
If I rename the |
The exclude parameter from #709 certainly seems like a sufficient and straightforward way of solving this particular problem, much simpler than trying to answer whether the recursive option should match its behaviour with It'll be interesting to see if it ends up growing into something very gitignore-like, or if there's interest in something like an exclude-file option such that .gitignore can be used directly. |
Just trying |
How does (or should) mockery treat directories named |
I just realised the exclude from #709 is entirely prefix-based - not quite what I was expecting. So I can't just add |
Well, we already have an I would have to think about the idea of parsing |
TBF, I'm still trying to work out how best to migrate to the config mode, so the approach might change. I can't even figure out if its better to have as much as possible in the central config and run mockery centrally, or have configs more distributed and close to the interfaces/packages/code and run mockery for each config/package. |
Well that's not a totally crazy way of doing it (one config for each package). It won't be as efficient as one config for the entire project because otherwise mockery would have to redundantly parse all of your deps multiple times. Calling out to |
If a config file for the entire project is preferable, I definitely need a fix for this issue to continue |
regex and gitignores are both sources of complexity, matching the |
So just to recap, it seems like the source of your problem is that mockery is recursing into paths that contain go.mod files. I think we can solve your goal in two ways:
Both of these will be extremely easy to add, it's probably line 8 lines of code. Let me know if you think this would help solve your problem. |
Does a .mockery.yaml in a subdirectory override behaviour when it is found via recursion? If not, then yes I think it has to stop when a .mockery.yaml is found. If the recursion takes these discovered .mockery.yamls into account, I think it should continue as per the resulting configuration. The logic for stopping at directories with go.mod seems sound as it clearly has potential to break when one is present. |
Theoretically, mockery could be modified to override its config in-place (in fact, it already does this when discovering recursive packages. The discovered packages are injected into the config map just as if you had manually specified them). But, merging together multiple yaml files is really tricky and hasn't been implemented anyway. So the safest thing is to probably just stop the recursion. The subdirectory exclusion logic would need to be added here: https://github.com/vektra/mockery/blob/v2.36.0/pkg/config/config.go#L548-L574 . It would be very easy. If you have the time for this, I'd be happy to accept a PR. Otherwise I'll leave this for someone else to implement (or for myself, if I find myself having spare time). |
I'm giving it a go, but using pathlib.Walker is making it more complex as unlike stdlib walk it visits children before visiting the directory, so there's no easy way of preventing recursion. The pathlib walker just generally seems overcomplicated, would it be OK to drop in in favour of stdlib filepath.WalkDir? |
Hmm, I understand why pathlib doesn't meet the requirements here. The Basic walker doesn't guarantee any kind of order but in this case we need an explicit BFS. For the sake of getting this fixed, yes feel free to swap with the filepath walker. |
We don't even need BFS, just pre-order DFS that allows culling instead of unconditional post-order DFS. |
Raised a PR for the submodules. A general exclude mechanism (that doesn't require the full prefix) would still be of interest, as a repository-wide mockery run will encounter directories it just isn't intended to recurse into (like node_modules). |
This PR changes mockery to not recurse into paths that contain a `go.mod` submodule. This is to help support monorepos that contain many modules. In this case, mockery should not be recursing into these because it causes issues, like in issue vektra#706.
This PR changes mockery to not recurse into paths that contain a `go.mod` submodule. This is to help support monorepos that contain many modules. In this case, mockery should not be recursing into these because it causes issues, like in issue vektra#706. fix deprecations in mkdocs
This PR changes mockery to not recurse into paths that contain a `go.mod` submodule. This is to help support monorepos that contain many modules. In this case, mockery should not be recursing into these because it causes issues, like in issue vektra#706. fix deprecations in mkdocs remove explicit lint step
Description
Getting an error about loading packages and module-aware mode when running mockery, see below
Mockery Version
Golang Version
Installation Method
Steps to Reproduce
I'm just trying out mockery's config mode and created the following config in the root of my repository, next to the
go.mod
fileI've run
go get -u github.com/vektra/mockery/v2
, and when I rungo run github.com/vektra/mockery/v2
I get the following error:Unfortunately I don't even know which package it was trying to load, so I'm having difficulty narrowing down the reproduction steps
Expected Behavior
No error
Actual Behavior
See above
The text was updated successfully, but these errors were encountered: