Magento2: "Area code is already set" exception when executing catalog:images:resize command

Created on 2 Mar 2017  路  12Comments  路  Source: magento/magento2


Preconditions


  1. Magento 2.1.2

Steps to reproduce

  1. Execute the catalog:images:resize command

Expected result

  1. No exception

Actual result

  1. [Magento\Framework\Exception\LocalizedException] Area code is already set, exception

Code fix: check if the area code is set before setting it

if (!$this->appState->getAreaCode()) {
  $this->appState->setAreaCode('catalog');
}

Cannot Reproduce Format is valid bug report

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)

debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 32);
die();

has @developer-lindner said, this issue should be closed.

All 12 comments

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PushEngineering picture PushEngineering  路  3Comments

BenSpace48 picture BenSpace48  路  3Comments

kirashet666 picture kirashet666  路  3Comments

xi-ao picture xi-ao  路  3Comments

denis-g picture denis-g  路  3Comments