The story so far: I'm updating Magento from 2.3 to 2.4 and in doing so I'm now getting an "Area code is not set" when running any CLI command. This did not happen in 2.3 and after looking at the following stack trace from bin/magento s:di:c --verbose I have the following stack trace:
In State.php line 153:
[Magento\Framework\Exception\LocalizedException]
Area code is not set
Exception trace:
at /var/www/OUR_COMPANY/vendor/magento/framework/App/State.php:153
Magento\Framework\App\State->getAreaCode() at /var/www/OUR_COMPANY/generated/code/Magento/Framework/App/State/Interceptor.php:50
Magento\Framework\App\State\Interceptor->getAreaCode() at /var/www/OUR_COMPANY/vendor/magento/module-theme/Model/Theme.php:272
Magento\Theme\Model\Theme->getArea() at /var/www/OUR_COMPANY/vendor/magento/framework/View/File/Collector/Base.php:68
Magento\Framework\View\File\Collector\Base->getFiles() at /var/www/OUR_COMPANY/vendor/magento/framework/View/File/Collector/Decorator/ModuleOutput.php:59
Magento\Framework\View\File\Collector\Decorator\ModuleOutput->getFiles() at /var/www/OUR_COMPANY/vendor/magento/framework/View/File/Collector/Decorator/ModuleDependency.php:63
Magento\Framework\View\File\Collector\Decorator\ModuleDependency->getFiles() at /var/www/OUR_COMPANY/vendor/magento/framework/View/Layout/File/Collector/Aggregated.php:88
Magento\Framework\View\Layout\File\Collector\Aggregated->getFiles() at /var/www/OUR_COMPANY/vendor/magento/framework/View/Element/UiComponent/Config/FileCollector/AggregatedFileCollector.php:76
Magento\Framework\View\Element\UiComponent\Config\FileCollector\AggregatedFileCollector->collectFiles() at /var/www/OUR_COMPANY/vendor/magento/module-ui/Config/Reader/FileResolver.php:43
Magento\Ui\Config\Reader\FileResolver->get() at /var/www/OUR_COMPANY/vendor/magento/module-ui/Config/Reader/Definition.php:28
Magento\Ui\Config\Reader\Definition->read() at /var/www/OUR_COMPANY/vendor/magento/module-ui/Config/Reader/Definition/Data.php:89
Magento\Ui\Config\Reader\Definition\Data->initData() at /var/www/OUR_COMPANY/vendor/magento/module-ui/Config/Reader/Definition/Data.php:75
Magento\Ui\Config\Reader\Definition\Data->__construct() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:121
Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:66
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:170
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:170
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:206
Magento\Framework\ObjectManager\Factory\AbstractFactory->parseArray() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:182
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:170
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:56
Magento\Framework\ObjectManager\ObjectManager->create() at /var/www/OUR_COMPANY/vendor/magento/framework/View/LayoutFactory.php:50
Magento\Framework\View\LayoutFactory->create() at /var/www/OUR_COMPANY/vendor/magento/module-payment/Helper/Data.php:79
Magento\Payment\Helper\Data->__construct() at /var/www/OUR_COMPANY/generated/code/Magento/Payment/Helper/Data/Interceptor.php:14
Magento\Payment\Helper\Data\Interceptor->__construct() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:121
Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:66
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:170
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:170
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:170
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:70
Magento\Framework\ObjectManager\ObjectManager->get() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:206
Magento\Framework\ObjectManager\Factory\AbstractFactory->parseArray() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:182
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:276
Magento\Framework\ObjectManager\Factory\AbstractFactory->getResolvedArgument() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:239
Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgumentsInRuntime() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:34
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:59
Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() at /var/www/OUR_COMPANY/vendor/magento/framework/ObjectManager/ObjectManager.php:56
Magento\Framework\ObjectManager\ObjectManager->create() at /var/www/OUR_COMPANY/vendor/magento/framework/Console/Cli.php:153
Magento\Framework\Console\Cli->getApplicationCommands() at /var/www/OUR_COMPANY/vendor/magento/framework/Console/Cli.php:134
Magento\Framework\Console\Cli->getDefaultCommands() at /var/www/OUR_COMPANY/vendor/symfony/console/Application.php:1274
Symfony\Component\Console\Application->init() at /var/www/OUR_COMPANY/vendor/symfony/console/Application.php:629
Symfony\Component\Console\Application->find() at /var/www/OUR_COMPANY/vendor/symfony/console/Application.php:237
Symfony\Component\Console\Application->doRun() at /var/www/OUR_COMPANY/vendor/magento/framework/Console/Cli.php:115
Magento\Framework\Console\Cli->doRun() at /var/www/OUR_COMPANY/vendor/symfony/console/Application.php:149
Symfony\Component\Console\Application->run() at /var/www/OUR_COMPANY/bin/magento:23
The stand out line is:
Magento\Theme\Model\Theme->getArea() at /var/www/OUR_COMPANY/vendor/magento/framework/View/File/Collector/Base.php:68
When we open up vendor/magento/framework/View/File/Collector/Base.php in 2.3 & 2.4 we see the following:
In 2.4
/**
* Retrieve files
*
* @param \Magento\Framework\View\Design\ThemeInterface $theme
* @param string $filePath
* @return \Magento\Framework\View\File[]
*/
public function getFiles(ThemeInterface $theme, $filePath)
{
$result = [];
$sharedFiles = $this->componentDirSearch->collectFilesWithContext(
ComponentRegistrar::MODULE,
"view/base/{$this->subDir}{$filePath}"
);
foreach ($sharedFiles as $file) {
$result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName(), null, true);
}
$area = $theme->getArea(); <-- This line is important
$themeFiles = $this->componentDirSearch->collectFilesWithContext(
ComponentRegistrar::MODULE,
"view/{$area}/{$this->subDir}{$filePath}"
);
foreach ($themeFiles as $file) {
$result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName());
}
return $result;
}
In 2.3:
/**
* Retrieve files
*
* @param \Magento\Framework\View\Design\ThemeInterface $theme
* @param string $filePath
* @return \Magento\Framework\View\File[]
*/
public function getFiles(ThemeInterface $theme, $filePath)
{
$result = [];
$sharedFiles = $this->componentDirSearch->collectFilesWithContext(
ComponentRegistrar::MODULE,
"view/base/{$this->subDir}{$filePath}"
);
foreach ($sharedFiles as $file) {
$result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName(), null, true);
}
$area = $theme->getData('area'); <-- This line is important
$themeFiles = $this->componentDirSearch->collectFilesWithContext(
ComponentRegistrar::MODULE,
"view/{$area}/{$this->subDir}{$filePath}"
);
foreach ($themeFiles as $file) {
$result[] = $this->fileFactory->create($file->getFullPath(), $file->getComponentName());
}
return $result;
}
The large difference are the corresponding lines:
2.3: $area = $theme->getData('area');
vs
2.4: $area = $theme->getArea();
getArea() will throw a \Magento\Framework\Exception\LocalizedException if the area code is not set, while getData('area') will return null if the area code is not set.
Now I imagine a correct solution is setting the area code appropriately elsewhere. But I haven't the slightest idea as to where.
My next hunch is to override the class and implement the default behavior of returning null which is what all our 2.3 code would expect. However this is almost certainly not the right path. In addition when I do override vendor/magento/framework/View/File/Collector/Base.php the CLI ignores the override.
Update October / 7 / 2021
So a solution is to skip the update to 2.4.3 and only update to 2.4.2 You can find the PR that caused the issue here. In additon the best solution I can recomend is the one mentioned here where you wrap that section in a try / catch block. Editing vendor files is not a long term solution but it will get you back on your feet untill a patch is made.