craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.22k stars 626 forks source link

[3.x]: `php craft` console command fails with `Invalid path alias: @` error #11492

Closed davola closed 2 years ago

davola commented 2 years ago

What happened?

Description

When trying to see the available list of commands we have in craft, it fails with the following error:

Exception 'yii\base\InvalidArgumentException' with message 'Invalid path alias: @' in /app/vendor/yiisoft/yii2/BaseYii.php:154

According with the \yii\base\Module::getControllerPath method help docs

# vendor/yiisoft/yii2/base/Module.php

    /**
     * Returns the directory that contains the controller classes according to [[controllerNamespace]].
     * Note that in order for this method to return a value, you must define
     * an alias for the root namespace of [[controllerNamespace]].
     * @return string the directory that contains the controller classes.
     * @throws InvalidArgumentException if there is no alias defined for the root namespace of [[controllerNamespace]].
     */
    public function getControllerPath()

it seems we fail to provide the needed alias namespace for this to work properly.

Steps to reproduce

  1. goto your project container
  2. goto craft root directory
  3. type php craft and hit enter

Expected behavior

It should list all the available commands that Craft has

Actual behavior

It shows the following YII error:

Exception 'yii\base\InvalidArgumentException' with message 'Invalid path alias: @'

in /app/vendor/yiisoft/yii2/BaseYii.php:154

Stack trace:
#0 /app/vendor/yiisoft/yii2/base/Module.php(261): yii\BaseYii::getAlias('@')
#1 /app/vendor/yiisoft/yii2/console/controllers/HelpController.php(245): yii\base\Module->getControllerPath()
#2 /app/vendor/yiisoft/yii2/console/controllers/HelpController.php(240): yii\console\controllers\HelpController-> >getModuleCommands(Object(modules\customRedactorConfig\Module))
#3 /app/vendor/yiisoft/yii2/console/controllers/HelpController.php(172): yii\console\controllers\HelpController->getModuleCommands(Object(craft\console\Application))
#4 /app/vendor/yiisoft/yii2/console/controllers/HelpController.php(192): yii\console\controllers\HelpController->getCommands()
#5 /app/vendor/yiisoft/yii2/console/controllers/HelpController.php(78): yii\console\controllers\HelpController->getCommandDescriptions()
#6 [internal function]: yii\console\controllers\HelpController->actionList()
#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#8 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#9 /app/vendor/yiisoft/yii2/console/Controller.php(182): yii\base\Controller->runAction('list', Array)
#10 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\console\Controller->runAction('list', Array)
#11 /app/vendor/yiisoft/yii2/console/Application.php(180): yii\base\Module->runAction('help/list', Array)
#12 /app/vendor/craftcms/cms/src/console/Application.php(89): yii\console\Application->runAction('help/list', Array)
#13 /app/vendor/yiisoft/yii2/console/Application.php(147): craft\console\Application->runAction('help/list', Array)
#14 /app/vendor/craftcms/cms/src/console/Application.php(118): yii\console\Application->handleRequest(Object(craft\console\Request))
#15 /app/vendor/yiisoft/yii2/base/Application.php(384): craft\console\Application->handleRequest(Object(craft\console\Request))
#16 /app/craft(27): yii\base\Application->run()
#17 {main}

Craft CMS version

Craft Pro 3.7.44

PHP version

8.0.20

Operating system and version

Linux 4.19.104-microsoft-standard

Database type and version

postgres

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.0-35)

Installed plugins and versions

brandonkelly commented 2 years ago

From the stack trace, it looks like the issue is coming from modules\customRedactorConfig\Module. Can you post that module’s code?

davola commented 2 years ago

oh! That module trace skipped my eyes.

here is the code

# modules\customRedactorConfig\Module.php

<?php
namespace modules\customRedactorConfig;

use craft\redactor\events\RegisterLinkOptionsEvent;
use craft\redactor\Field as RedactorField;
use yii\base\Event;

class Module extends \yii\base\Module
{
    /**
     * Initializes the module.
     */
    public function init()
    {
        // Remove "Link to..." Redactor links
        Event::on(
            RedactorField::class,
            RedactorField::EVENT_REGISTER_LINK_OPTIONS,
            function(RegisterLinkOptionsEvent $event) {

                // Only apply to a specific Redactor config
                if ('OnlyDefinedLinks.json' == $event->sender->redactorConfig) {

                    // Remove Craft's injected links
                    $event->linkOptions = [];

                }

            }
        );
    }
}

Thank you!

davola commented 2 years ago

Oh! You are a genius @brandonkelly. You were right, the controller namespace initialization was missing on this Module.

I have added the following on top and it works perfect now:

        if (\Craft::$app->getRequest()->getIsConsoleRequest()) {
            $this->controllerNamespace = 'modules\\console\\controllers';
        } else {
            $this->controllerNamespace = 'modules\\controllers';
        }

Sorry for the inconvenience and thank you so much for dedicating the time to see this. Your product Craft is awesome! 💪

Closing this now, Thanks! 🙏

brandonkelly commented 2 years ago

Glad you got it sorted!