tl;dr: This is a PSA. Double-check your generated ModuleServiceProvider.php, and remove methods that you aren't using. (Also remove calls to them from the boot method.) Specifically, removing registerFactories fixed this for me.
This issue happens when your paths.generator.factory setting in config/modules.php is missing or set to false. This might happen during an upgrade to v.2.5.0 due to commit 9eed5d3, which addressed issue #364.
If you _are_ using factories in your modules, adding the factory setting before generating modules will also fix the issue, for example:
'generator' => [
'factory' => ['path' => 'Database/factories', 'generate' => true],
]
This follows the new conventions for generator definitions as described in #368. Alternatively, you can use the old convention:
'generator' => [
'factory' => 'Database/factories',
]
Behind the scenes: If the factory setting is set correctly, you should have a line in your ModuleServiceProvider that looks something like this:
app(Factory::class)->load(__DIR__ . '/../Database/factories');
If it's missing, that part of the path will be omitted:
app(Factory::class)->load(__DIR__ . '/../');
...which causes Laravel to require all *.php files in that path (Factory.php#L202), including the newly-generated ServiceProvider. Since it's already autoloaded via Composer, we get our error.
Factories are weird in that they are defined (by default) via calls on the $factory variable, rather than being registered individually in some superclass (cf. $commands in app/Console/Kernel.php). I'm still working on understanding their loading process, but Laravel's suggested approach clearly causes more issues than it solves.
Please feel free to close this issue. This is mostly for search and linking purposes, in case someone else runs into it while upgrading.
Commenting out the $this->registerFactories(); fixed it for me, since I don't have any factories set up in this project right now but plan to in the future.
Do we need to update our config files in previously created modules to account for these new functions?
I don't believe so... specifically, you won't need to update the ServiceProviders in previously created modules, but to avoid the error for new modules, you should add the paths.generator.factory setting to config/modules.php as described above.
I've updated the first post to suggest the new generator setting format.
@MrJoelKelly I see that on method registerFactories() on [Module]ServiceProvider the directory to load factories is wrong: app(Factory::class)->load(__DIR__ . '/../');. I changed to app(Factory::class)->load(__DIR__ . '/../Database/factories'); and it works!
I hope it helps.
@sfelix-martins Perhaps PR? :)
@Kyslik I updated my config/modules.php file with the content of vendor/nwidart/laravel-modules/config/config.php and this and another problems was solved!
The package owner @nWidart says in issue:#372: "This is sadly because laravel doesn't merge configurations recursivelly."
You can also re-publish the config with the --force flag to automate it a bit.
Most helpful comment
@MrJoelKelly I see that on method
registerFactories()on[Module]ServiceProviderthe directory to load factories is wrong:app(Factory::class)->load(__DIR__ . '/../');. I changed toapp(Factory::class)->load(__DIR__ . '/../Database/factories');and it works!I hope it helps.