I am creating a laravel package for production and removing the composer.json and composer. lock once the dependencies are installed. But when I ran any artisan command I get the below error
In Application.php line 1399:
file_get_contents(/var/www/project_dir/composer.json): Failed to open stream
: No such file or directory
I am excluding the composer.json from the production environment as the autoloading is done through vendor/autoload.php.
I was doing the same thing previously as well but not sure the issue appeared suddenly when I am deploying the new release.
2
Answers
After investigating the issue I have found that the issue is causing due to
$this->load(__DIR__.'/Commands');
code called inAppConsoleKernel
class incommands()
function.Basically, we have registered the console commands using
php artisan make:command
, and it created the file inside the commands directory which is fine. But the load method fromvendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php
try to get the application namespace that fails due to a missing composer.json file.So, to handle this I have removed the
$this->load(__DIR__.'/Commands');
line fromcommands()
function inside theAppConsoleKernel
class and registered the commands manually using$commands
property withinAppConsoleKernel
class.https://laravel.com/docs/8.x/artisan#registering-commands
Laravel’s class
IlluminateFoundationApplication
loads the root application namespace from yourcomposer.json
. This is nothing new, I can find such code even in Laravel v6 from 2015.If your code was working well in any prior version of Laravel without having a
composer.json
deployed, but is no longer, you should check what has changed in your application