Code fix: check if the area code is set before setting it
if (!$this->appState->getAreaCode()) {
$this->appState->setAreaCode('catalog');
}
AFAIK by default, no area code is set when running a console command - unless it sets one itself. Do you happen to have some customized or 3rd-party code which might be setting the area code (via an observer perhaps) before the execution reaches the Magento\Catalog\Console\CommandImagesResizeCommand ?
I am pretty sure you're missing some important preconditions here, I doubt this could be reproduced on a clean installation.
@cherreman happened to me also while creating some very own cli commands. so check you're code, it isn't Magento2 for sure! (and close this ticket...)
@cherreman yes in my case it was already set by Iazel\RegenProductUrl\Console\Command extension.
To help you tracking down this Exception, the quickest way is to add a debug_print_backtrace in the method setAreaCode.
In the file : Magento\Framework\App\State::setAreaCode (line 119 (Magento 2.1.6)
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 32);
die();
has @developer-lindner said, this issue should be closed.
@cherreman, thank you for your report.
We were not able to reproduce this issue by following the steps you provided. If you'd like to update it, please reopen the issue.
We tested the issue on 2.3.0-dev, 2.2.0, 2.1.9
Apparently Magento2 instantiates (and thus - calls constructors of) all console commands when executing any console command. So if by chance you have a call to $appState->setAreaCode()
in the constructor of any console command, you will most likely get this exception when executing a console command that calls setAreaCode()
on its own.
Hey guys I investigated this a while ago so wrote some information about app state here:
2.3.0 same error remains.
I added an if statement to Magento/MediaStorage/Console/Command/ImagesResizeCommand.php:66
.
Changed:
$this->appState->setAreaCode(Area::AREA_GLOBAL);
To:
if (!$this->appState->getAreaCode()) {
$this->appState->setAreaCode(Area::AREA_GLOBAL);
}
Error is persistent on 2.3.3, I've attached a patch from @maartenwolfsen's fix that worked.
CatalogImagesResize.patch.txt
still evident on 2.3.5-p2 via standard composer install
the two comments (fix and patch file) resolve the problem
it would be great if problems like this could be incorporated into the offical releases/code!
Still evident in 2.4.0
still evident int 2.3.6
Most helpful comment
@cherreman yes in my case it was already set by Iazel\RegenProductUrl\Console\Command extension.
To help you tracking down this Exception, the quickest way is to add a debug_print_backtrace in the method setAreaCode.
In the file : Magento\Framework\App\State::setAreaCode (line 119 (Magento 2.1.6)
has @developer-lindner said, this issue should be closed.