Skip to content

Change composer post-autoload-dump script to Artisan command #6647

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

Merged
merged 2 commits into from
Aug 3, 2025

Conversation

lmjhs
Copy link
Contributor

@lmjhs lmjhs commented Jul 31, 2025

This is a suggestion to help solve an issue with the OpenTelemetry PHP SDK package but may also help other packages from falling into the same trap. There is an open issue here: open-telemetry/opentelemetry-php#1673

Laravel adds a PHP callback script to the post-autoload-dump event in composer.json

https://github.com/laravel/laravel/blob/12.x/composer.json#L36

"scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",

https://getcomposer.org/doc/articles/scripts.md#scripts
When Composer runs PHP callback scripts it means you are inheriting all the dependencies of the composer.phar. This is not normally an issue if the script is fairly simple however the Laravel postAutoloadDump function calls the Composer autoload.php to load all dependencies necessary for Laravel to run. This means any packages that include any code in autoloaded files also gets run. In the case of the OpenTelemetry SDK it is instantiating a v3 Psr/Log/NullLogger instance in the autoload however Composer is using an older version of the Psr/Log package causing the script to fail.

If the intention is for the script to call Laravel with the correct dependencies it would make more sense to call it as an artisan command as this will create a dedicated PHP process and ensure the correct dependencies are being loaded for the command.

My suggested change is to swap the postAutoloadDump to use the config:clear and clear-compiled commands as they are a direct replacement to the original script.

I'm open to suggestions on a better command that would be more appropriate or if there are undesired consequences to this change. I initially tried optimize:clear however the tests failed as it assumes the cache layer exists using sqlite but the sqlite database doesn't exist unless you create a project instead so the command fails. Should optimize:clear fail if the cache layer is not available? Alternatively we could consider adding the config:clear to the clear-compiled command so we only have the one command to run?

Thanks

@lmjhs lmjhs marked this pull request as draft July 31, 2025 13:04
@lmjhs lmjhs changed the title Change composer post-autoload-dump script to optimize:clear Change composer post-autoload-dump script to Artisan command Jul 31, 2025
@lmjhs lmjhs marked this pull request as ready for review July 31, 2025 13:52
@taylorotwell taylorotwell merged commit a4d121f into laravel:12.x Aug 3, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants