Repository navigation
Fix fatal error in --prerun/--postrun event hooks (namespaced seeders) - #264
Merged
Merged
Conversation
The --prerun/--postrun event-injection code emits unqualified `Event`, `Exception` and event-class references. Since commit 4599ed3 (Oct 2020) added `namespace Database\Seeders;` to the seed stub, those names resolve relative to that namespace (e.g. `Database\Seeders\Exception`) and fatal at runtime with "Class not found". The injection code dates from 2016, when seeders lived in the global namespace, so it was never updated for the namespace migration. Prefix the injected `Event` facade, base `Exception`, and the user-supplied event class with a leading backslash so they resolve from the global namespace, matching the already-correct `\DB::` calls in the same stub. Adds a regression test that asserts the qualified output and actually loads + runs the generated seeder to prove it no longer fatals.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Seeders generated with
--prerun/--postrunevent hooks fatal at runtime withError: Class "Database\Seeders\Exception" not found(and the same forEventand the user's event class).The injected hook code uses unqualified class names:
Since these are emitted inside the stub's
namespace Database\Seeders;, PHP resolves them relative to that namespace (Database\Seeders\Event,Database\Seeders\Exception,Database\Seeders\App\Events\SomeEvent) — none of which exist — so the seeder fatals.Why it went unnoticed
This is a regression from commit
4599ed3(Oct 2020), which addednamespace Database\Seeders;to the seed stub but touched only the.stubfiles. The event-injection code dates from 2016, when seeders lived in the global namespace whereEvent/Exceptionresolved correctly. The combination of the namespaced stub and the rarely-used--prerun/--postrunoptions is uncommon enough that nobody hit it.Fix
Prefix the injected
Eventfacade, baseException, and the user-supplied event class with a leading backslash so they resolve from the global namespace — matching the already-correct\DB::calls in the same stub.Tests
testPrerunPostrunEventsAreNamespaceSafe, which asserts the fully-qualified output and loads + executes the generated seeder to prove it no longer fatals. (Fails on the old code, passes on the fix.)Manual verification (Laravel 10.48.28, PHP 8.3, MySQL)
iseed users— generates, lints,db:seedruns. ✅iseed users --prerun='App\Events\SomePrerunEvent' --postrun='App\Events\SomePostrunEvent'— emits\Event::until(new \App\Events\SomePrerunEvent()), lints, runs without fatal, both hooks fire. ✅