5

I have a third party extension that i'm trying to install on a Magento 2 site i'm building. When i try and run setup:upgrade i get the error:

[Magento\Framework\Exception\SessionException] Area code not set: Area code must be set before starting a session.

and

[Magento\Framework\Exception\LocalizedException] Area code is not set

But here is the tricky part, the module is not using any customer CLI commands. So i'm not sure why it's kicking this error, since all the fixes Here and Here only apply to the setting up of a customer CLI command. I found one more mention Here where p3mbo found that he was using the Object Manager to call in a class. The module does use the OM in spades, but i'm not convinced that this is cause my error.

What other way's can this error be tripped and other places i could look to figure out what's happening?

  • 1
    Anything in the module that references sessions (particularly in the Setup scripts) could cause such an error. Did you contact the vendor about it? – Ryan Hoerr Aug 2 '16 at 23:52
  • They are not getting back to me, and we are on a deadline. So the hope is they will get back to me, but plan for the worst. Thanks for the clue, i'll start digging. – circlesix Aug 3 '16 at 1:48
  • 1
    Try to run setup:upgrade with -vvv option. It should show you the back trace, which should help identify the extension's classes involved. – BuskaMuza Aug 3 '16 at 5:53
  • @BuskaMuza So how to identify the extension's classes? -vvv shows only Magento and Symfony, bcause setup:upgrade doesn't run code from 3-rd party modules in main stream. This is a nightmare, magento. I've updated a lot of modules and now I get this error... -_- – spiil Oct 27 '17 at 10:14
9

With some kind help i was able to track down the offending code.

By using BuskaMuza's suggestion of adding verbose to bin/magento setup:upgrade -vvv i was able to track down the file where the error was being kicked. It's super common in M1 to just look at the first line of the error and work with that to figure out the solution, but in this case it was buried in the stack trace. While my exact solution might not work for everyone, i wanted to post it here so anyone searching might be able to track down their error.

In the stack trace, there was a line that referenced the module that was kicking the error when i tried to compile. I know that it was this module because disabling just that module would compile fine. Once enabled, the error would kick when it got to processing that one (you can see that module name as the last one called before the error).

That line in my case is {{vendor}}\{{module}}\Setup\InstallData->install() giving me the clue that Ryan H suggested. By looking at the line in the stack trace right above it calls out the file in the module and the line that has the error Magento\SalesSequence\Model\Builder->create() at /app/code/{{vendor}}/{{module}}/Setup/InstallData.php:86. So in this file there is the creation of data into the DB tables, and when it calls create() at some point in there, there is an error. There is a bit of a rabbit hole to follow to get to the end of why the error was hit, and i wont post it here.

But the fix for it is to have the store area set in this offending file with this suggestion here.

public function __construct(
    \Magento\Framework\App\State $state
) {
    $state->setAreaCode('frontend'); // or 'adminhtml', depending on your needs
}

In the case of this file, there was already a __construct(), so i just had to add in the code and the module compiled fine. It would seem that there are a lot of differnet ways that Magento will compile, and when using the CLI, the module developers have to careful to make sure that the area is set when calling any method that might depend on it. What all those methods are.........the world may never know........

  • 1
    Thank you. Was able to resolve issue with other modules using an adaptation of this code. – dawhoo Dec 7 '17 at 23:13
4

in my case, I had to use :

 $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);

hope it helps

2

Further to circlesix's thorough response, this can occur with CLI and CRON but be sure to set the area code in the construct before a session is created. Otherwise you're likely to get further errors of 'area code already set'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.