-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Log warnings on php artisan optimize
: Request macros already exist
#10
Comments
hi @onlime
|
Hi @bilfeldt thanks for your fast response! I can reproduce it with a fresh L11/Breeze/Inertia/Vue project, set up like this: $ laravel new laravel-correlation-id
┌ Would you like to install a starter kit?
│ Laravel Breeze
┌ Which Breeze stack would you like to install?
│ Vue with Inertia
┌ Would you like any optional features?
│ Dark mode
│ ESLint with Prettier
┌ Which testing framework do you prefer?
│ Pest
┌ Which database will your application use?
│ SQLite
┌ Would you like to run the default database migrations?
│ Yes
$ cd laravel-correlation-id
$ npm install && npm run build
$ herd link
$ herd secure
$ composer require bilfeldt/laravel-correlation-id
$ composer dev the problem does not yet pop up when accessing https://laravel-correlation-id.test
nothing is logged yet, but as soon as I run $ php artisan config:cache
$ tail -f storage/logs/laravel.log
[2024-12-10 23:34:45] local.WARNING: Request::getUniqueId() already exists, skipping macro registration.
[2024-12-10 23:34:45] local.WARNING: Request::getCorrelationId() already exists, skipping macro registration.
[2024-12-10 23:34:45] local.WARNING: Request::getClientRequestId() already exists, skipping macro registration. That's all I did, no single modification, just a fresh L11 project with a common stack. If you need this repo, I can publish it, but should be easy reproducible. Tested on: $ php -v
PHP 8.4.1 (cli) (built: Nov 21 2024 10:00:19) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.4.1, Copyright (c) Zend Technologies
with Zend OPcache v8.4.1, Copyright (c), by Zend Technologies
$ pa about
Environment .........................................................................
Application Name ............................................................ Laravel
Laravel Version ............................................................. 11.35.0
PHP Version ................................................................... 8.4.1
Composer Version .............................................................. 2.8.3
Environment ................................................................... local
Debug Mode .................................................................. ENABLED
URL ..................................................... laravel-correlation-id.test
Maintenance Mode ................................................................ OFF
Timezone ........................................................................ UTC
Locale ........................................................................... en
Cache ...............................................................................
Config ....................................................................... CACHED
Events ....................................................................... CACHED
Routes ....................................................................... CACHED
Views ........................................................................ CACHED
Drivers .............................................................................
Broadcasting .................................................................... log
Cache ...................................................................... database
Database ..................................................................... sqlite
Logs ................................................................. stack / single
Mail ............................................................................ log
Queue ...................................................................... database
Session .................................................................... database |
I still didn't get much further... If I explicitly disable auto-discovery of "extra": {
"laravel": {
"dont-discover": [
"bilfeldt/laravel-correlation-id"
]
}
}, the problem disappears. But when a manually register it in return [
App\Providers\AppServiceProvider::class,
\Bilfeldt\CorrelationId\CorrelationIdServiceProvider::class,
]; The
always testing with But now it gets interesting: It even registers the macros on To keep it short (can dig into it further tomorrow when working together with @pascalbaljet, as he's really deep into the framework and understands all the bootstrapping / service container internals): Just remove the |
@onlime thanks very much for the detailed explanation, much appreciated 👍 When I added the warnings, it was because I was under the assumption that the boot method would only ever get called once. But it seems that for some reason, beyond my initial understanding, the service providers are booted up twice when config is cached. There seems to be another issues that might be related to this, but need more time to test it out. If you wanna make a PR that simply removed the warnings, then I will happily merge that. I will still try to confirm that it is the framework that runs the boot method twice and not this package that does something wrong, but I will not have the time for that right now. As you have the example up and running already, can I ask you if the |
Update/correction: Even if I comment out the #[AsCommand(name: 'config:cache')]
class ConfigCacheCommand extends Command
{
// ...
public function handle()
{
// $this->callSilent('config:clear'); so there must be really something wrong with My first guess was to move the macro registration from So, maybe we should not even register the macros at all if |
No PR today from my side, if I don't fully understand the original cause. Will ask @pascalbaljet tomorrow. He usually finds such glitches in a couple of seconds.
tested. Yes, it's also booted twice (both I guess it's the fresh framework bootstrapping which gets called after protected function getFreshConfiguration()
{
$app = require $this->laravel->bootstrapPath('app.php');
$app->useStoragePath($this->laravel->storagePath());
$app->make(ConsoleKernelContract::class)->bootstrap();
return $app['config']->all();
} pretty uncool, but they must have had their reasons for this. So yes, you can probably not do anything against that double framework bootstrapping, so we should just remove the warnings. will PR tomorrow. |
In that case this is a framework issue. If that is the case, then we should just accept that the file can be booted multiple times and conditionally register the macros in the service provider. |
right! So let's not waste more time on it. Here's my PR: #11 I just find it a pity that there are no clear best practices for Laravel package maintainers. The Package Discovery and Service Providers section in the official docs is super short. |
also, it would not really harm to register a macro twice, as it just registers the callback on a static |
Completely right.
If the framework has decided to call the boot method twice, then it is expected behaviour and we should not log anything at all. The downside is that we do not get information if another macro with the same name exists. Keeping this open until I have the time to merge your PR. Thanks. |
agree!
Take your time until tomorrow at least, when I have another opinion about this topic. |
I have integrated bilfeldt/laravel-request-logger in the most standard way in my L11 project (latest Laravel v11.34.2), without any config modifications and only by adding the
LogRequestMiddleware
to some api routes.Request logging works fine, but whenever I run
php artisan optimize
on production, theCorrelationIdServiceProvider
(L84-L129) logs these warnings:It always logs twice for all 3 macros that it tries to register.
This is a problem for me, as I send out Slack notifications for every log level above WARNING.
Even if I run a
optimize:clear
right before in my deployment, it happens.php artisan optimize:clear php artisan optimize # => 6 warnings fired!
the following are the cause:
Honestly, I don't know if this is a general problem of some stale caching on
optimize
, as I don't have any other packages installed that log any warnings when registering any macros.Would very much appreciate if you could investigate this, as it might fill up other's application logs as well.
The text was updated successfully, but these errors were encountered: