Skip to content
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

[11.x] After Laravel 11.24.0 commands in app/Console/Commands are not auto-registered #52902

Closed
rodrigopedra opened this issue Sep 24, 2024 · 17 comments · Fixed by #52903
Closed
Labels

Comments

@rodrigopedra
Copy link
Contributor

Laravel Version

11.24.0

PHP Version

8.2.23

Database Driver & Version

mysql Ver 8.0.36 for Linux on x86_64 (MySQL Community Server - GPL)

Description

PR #52867 introduced a change to verify if the routes/console.php file exists. After this change, the commands in app/Console/Commands are not auto-registered anymore, if both the routes/console.php, and commands in app/Console/Commands exist.

Steps To Reproduce

Public repo:

https://github.com/rodrigopedra/bug-report-laravel-11-24

Instructions are in the README

Previously, I was manually registering the routes/console.php file

@rodrigopedra
Copy link
Contributor Author

One solution, is to always auto-register commands in app/Console/Commands:

public function withCommands(array $commands = [])
{
    if (empty($commands) && is_file($this->app->basePath('routes/console.php'))) {
        $commands = [...$commands, $this->app->basePath('routes/console.php')];
    }

    // CHANGED: remove the empty() check in this if clause
    if (is_dir($this->app->path('Console/Commands'))) {
        $commands = [...$commands, $this->app->path('Console/Commands')];
    }

    $this->app->afterResolving(ConsoleKernel::class, function ($kernel) use ($commands) {
        [$commands, $paths] = collect($commands)->partition(fn ($command) => class_exists($command));
        [$routes, $paths] = $paths->partition(fn ($path) => is_file($path));

        $this->app->booted(static function () use ($kernel, $commands, $paths, $routes) {
            $kernel->addCommands($commands->all());
            $kernel->addCommandPaths($paths->all());
            $kernel->addCommandRoutePaths($routes->all());
        });
    });

    return $this;
}

@rodrigopedra
Copy link
Contributor Author

ping @taylorotwell @driesvints

@SamuelNitsche
Copy link
Contributor

@rodrigopedra Sorry, that's my bad. I created a PR to fix this.

@rodrigopedra
Copy link
Contributor Author

Don't worry =)

No production servers were harmed. At least not on my deployment =)

I rushed to let them know as I imagine it could break some people's apps that depend on scheduled commands, or have a crontab calling any custom command directly

@NightingaleWK
Copy link

After the release of the new version, I found that my Commands were invalid, I wrote them all in App\Console\Commands, and it was all such a coincidence

My own temporary solution: register commands by providing the command's class name to the withCommands method

<?php

use App\Console\Commands\CalculateFee;
use App\Console\Commands\DropAllTables;
use App\Console\Commands\PayFee;
use App\Console\Commands\RefreshYearAnalyse;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })
    ->withCommands([
        CalculateFee::class,
        DropAllTables::class,
        PayFee::class,
        RefreshYearAnalyse::class,
    ])
    ->create();

Fortunately, everything found in the development environment.Wait for specific solutions ^.^

@guille1988
Copy link

My project has also problems with the same thing.

@usesorane
Copy link

You can add the following to bootstrap/app.php to re-enable normal behavior until this is fixed.

->withCommands([ __DIR__.'/../app/Console/Commands', ])

@SRWieZ
Copy link

SRWieZ commented Sep 24, 2024

Can confirm this 👍

@ziming
Copy link
Contributor

ziming commented Sep 24, 2024

yup got this issue

@kminek
Copy link
Contributor

kminek commented Sep 24, 2024

@SamuelNitsche shoudn't a fix contain a test case for this bug?

@SamuelNitsche
Copy link
Contributor

@SamuelNitsche shoudn't a fix contain a test case for this bug?

Ideally the whole ApplicationBuilder itself would be covered by tests. However this isn't the case and I unfortunately wasn't sure how to properly test this. I guess Taylor wasn't sure either while developing it.

@kminek
Copy link
Contributor

kminek commented Sep 24, 2024

@SamuelNitsche shoudn't a fix contain a test case for this bug?

Ideally the whole ApplicationBuilder itself would be covered by tests. However this isn't the case and I unfortunately wasn't sure how to properly test this. I guess Taylor wasn't sure either while developing it.

i think this case should be covered with integration test somewhere in https://github.com/laravel/framework/tree/11.x/tests/Integration/Console

@tsakib360
Copy link

Add this in bootstrap/app:
->withCommands([
DIR.'/../routes/console.php',
DIR.'/../app/Console/Commands',
])

@Airgerr
Copy link

Airgerr commented Oct 17, 2024

So... it was reverted and it is not fixed?? Should this be reopend?

@guille1988
Copy link

Yes it was fixed

@Airgerr
Copy link

Airgerr commented Oct 22, 2024

Yes it was fixed

I think not: click on the PR, scroll down... read: "this PR has been reverted due to breaking change with existing application."
I'm still manually bootstrapping at the moment.

@Rafnexx
Copy link

Rafnexx commented Oct 25, 2024

Any update on this? I've tried couple of solutions for this problem suggested here but I can't make it work, my commands are still not discovered by Laravel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet