getsentry / sentry-laravel

The official Laravel SDK for Sentry (sentry.io)
https://sentry.io
MIT License
1.24k stars 188 forks source link

There are no commands defined in the "sentry" namespace. #498

Closed hfuccillo closed 3 years ago

hfuccillo commented 3 years ago

I'm having issues installing and configuring Sentry in my Laravel 8 local environment. After installing the package I'm getting the "There are no commands defined in the "sentry" namespace when running the publish command: php artisan sentry:publish --dsn=https://b62b12a810ea43839e8d7a1c8cbfb704@o297060.ingest.sentry.io/1548569

This is my composer.json

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "ReportMyAds",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^7.3|^8.0",
        "abraham/twitteroauth": "2.0.1",
        "facebook/php-business-sdk": "^10.0",
        "fideloper/proxy": "^4.4",
        "fruitcake/laravel-cors": "^2.0",
        "google/apiclient": "^2.9",
        "google/auth": "^1.15",
        "googleads/google-ads-php": "^9.0",
        "googleads/googleads-php-lib": "^53.0",
        "guzzlehttp/guzzle": "^7.0.1",
        "hborras/twitter-php-ads-sdk": "7.0.0",
        "intervention/image": "^2.3",
        "jenssegers/mongodb": "^3.8.3",
        "laravel/framework": "^8.40",
        "laravel/tinker": "^2.5",
        "league/flysystem-aws-s3-v3": "1.0.22",
        "maatwebsite/excel": "^3.1",
        "pusher/pusher-php-server": "^4.0",
        "rap2hpoutre/laravel-log-viewer": "^1.7",
        "romanzipp/laravel-queue-monitor": "^2.1",
        "sentry/sdk": "^3.1",
        "tymon/jwt-auth": "^1.0",
        "webpatser/laravel-uuid": "4.*"
    },
    "require-dev": {
        "facade/ignition": "^2.5",
        "fakerphp/faker": "^1.9.1",
        "laravel/sail": "^1.7",
        "mockery/mockery": "^1.4.2",
        "nunomaduro/collision": "^5.0",
        "phpunit/phpunit": "^9.3.3"
    },
    "autoload": {
        "classmap": [
            "database",
            "app/Helpers"

        ],
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "FacebookAds\\":"vendor/facebook/php-ads-sdk/src/FacebookAds/",
            "AdwordsApi\\":"vendor/googleads/googleads-php-lib/src/Google/AdsApi/AdWords"
        },        
        "files": [
                   "app/Helpers/PathHelper.php"
                ]

    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}

I can also see the sentry package in my vendor folder.

Any thoughts?

stayallive commented 3 years ago

Yeah I think you got mistaken by the documentation, for Laravel we have a dedicated package you need to install but you installed sentry/sdk which does not include the neede Laravel bits. Remove that package and install the Laravel SDK and you should be good to go:

composer remove sentry/sdk
composer require sentry/sentry-laravel
fabioselau077 commented 3 years ago

Here we had the same problem and it was because Laravel didn't discover the package on its own. We had to add it to the provider manually. Was it a bug? We use Laravel 6.

stayallive commented 3 years ago

Laravel runs a few commands after running composer so maybe they failed to run the package discovery or you installed Sentry with the --no-scripts option?

Try to run composer update again and make sure there are no errors and Sentry is listed in the discovered packages.

As far as we know there is no bug here.

fabioselau077 commented 3 years ago

Laravel runs a few commands after running composer so maybe they failed to run the package discovery or you installed Sentry with the --no-scripts option?

Try to run composer update again and make sure there are no errors and Sentry is listed in the discovered packages.

As far as we know there is no bug here.

Follow oficial documentation, only 'require....'. But same run 'php artisan discovery', not show sentry package, only orthers. My solution is add it to the provider manually, like official documentation for laravel < 5.

stayallive commented 3 years ago

Are you able to share your composer.json here so I can try to replicate it?

m1ch3lp3r3z commented 3 years ago

We are having the same problem, in my case we are calling Artisan::call(...) in one of our ServiceProviders->boot() method which was triggering Console/Application instantiation before SentryProvider would register it's own commands. Not related to auto discovery, which is happening correctly for me.

stayallive commented 3 years ago

That is an interesting case... I'm not sure there is much we can do here to "fix" that.

Laravel recommends registering the commands from inside the boot method in our service provider: https://laravel.com/docs/8.x/packages#commands.

And you probably have a good reason but running artisan command from the boot method of your service providers seems like a bad idea but then again I don't know you use case or problem you are solving here :)

If you really must run artisan command in your service providers do it like this:

$this->app->booted(static function () {
    Artisan::call(...);
});

This way everything is booted and all service providers had the chance to register their commands.

m1ch3lp3r3z commented 3 years ago

Hi @stayallive, thank you for the tips!, definitely not something we intended to do, having artisan commands running on boot. It's code from a lrv package that is responsible for automating another lib integration, etc... In any case it should probably not be there and there is nothing to do on Sentry SDK side. Thanks!

stayallive commented 3 years ago

I think we close this one, if anyone still has problems where the artisan command is not registered feel free to open up a new issue with the relevant information about your environment so we can investigate further.

rosamarsky commented 3 years ago

@hfuccillo Add this to your config/app.php

    'providers' => [
        // ...
        Sentry\Laravel\ServiceProvider::class,
    ],

@stayallive perhaps this part also needs to be added to the documentation

stayallive commented 3 years ago

I'm assuming you are using a version older than 8.x, in that case it is added to the documentation: https://docs.sentry.io/platforms/php/guides/laravel/other-versions/laravel5-6/#install. We removed that bit from the 8.x documentation since auto-discovery is available since Laravel 5.5.

Again this should be only needed when you are on Laravel 5.4 or older... versions after that should use auto discovery and have no need to add them to the providers array manually.

tuanhaviet22 commented 1 year ago

@hfuccillo Add this to your config/app.php

    'providers' => [
        // ...
        Sentry\Laravel\ServiceProvider::class,
    ],

@stayallive perhaps this part also needs to be added to the documentation

No need to define in provider, composer auto discover handle it

Please make sure you don't ignore it in composer.json

image