Magento2: Error: "Area code is already set" after upgrading from 2.0.7 to 2.1.0

Created on 27 Jun 2016  路  5Comments  路  Source: magento/magento2

I have a Magento 2.0.7 installation, and I updated it by bumping the version number in my composer.json file from 2.0.7 to 2.1.0. Composer update went fine, no errors there, and all my vendor modules get updated.

However, when I try to execute bin/magento setup:upgrade, I get the error:

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

setup:upgrade [--keep-generated] [--magento-init-params="..."]

Anyone know what might be causing this issue?

I already deleted var/generated, var/di, var/cache and var/page_cache. I also applied the MDVA-532 patch. None of it worked.

Most helpful comment

I found it. The problem was my own custom module. Shame on me...

I accidentally set the state in the __construct()-method of a CLI command:

public function __construct(
    State $state,
    $name = 'something'
)
{
    $state->setAreaCode('adminhtml');
    parent::__construct($name);
}

This causes setup:upgrade to throw the exception. I solved it by setting the state in the execute()-method instead:

protected $state;

public function __construct(
    State $state,
    $name = 'something'
)
{
    $this->state = $state;
    parent::__construct($name);
}

// ... some code ...

protected function execute(InputInterface $input, OutputInterface $output)
{
    if (!$this->state->getAreaCode()) {
        $this->state->setAreaCode('adminhtml');
    }

    // do something ...
}

I'll just leave this here if anyone else encounters the same problem.

All 5 comments

@kanduvisla
It may be caused by the cache. In this case try to clean "./var" folder and browser cache and re-install Magento.

If the problem still exists could you please provide description according to the template?

I found it. The problem was my own custom module. Shame on me...

I accidentally set the state in the __construct()-method of a CLI command:

public function __construct(
    State $state,
    $name = 'something'
)
{
    $state->setAreaCode('adminhtml');
    parent::__construct($name);
}

This causes setup:upgrade to throw the exception. I solved it by setting the state in the execute()-method instead:

protected $state;

public function __construct(
    State $state,
    $name = 'something'
)
{
    $this->state = $state;
    parent::__construct($name);
}

// ... some code ...

protected function execute(InputInterface $input, OutputInterface $output)
{
    if (!$this->state->getAreaCode()) {
        $this->state->setAreaCode('adminhtml');
    }

    // do something ...
}

I'll just leave this here if anyone else encounters the same problem.

@kanduvisla
Thank you for solution!

@kanduvisla Thanks for sharing your solution. I just upgraded a client's site to 2.1 and ran into the same issue. Interestingly enough, it was also caused by the area code being set in the __construct method of a custom CLI command.

@erikhansen in magento 2.1, the function getAreaCode is like that :

/**
 * Get area code
 *
 * @return string
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function getAreaCode()
{
    if (!isset($this->_areaCode)) {
        throw new \Magento\Framework\Exception\LocalizedException(
            new \Magento\Framework\Phrase('Area code is not set')
        );
    }
    return $this->_areaCode;
}`

To get around that, I did :

protected function execute(InputInterface $input, OutputInterface $output) {
    try {
        $this->_state->setAreaCode('adminhtml');
    } catch (Exception $ex) {
        $output->writeln("Areacode was already set");
    }
    ....
}
Was this page helpful?
0 / 5 - 0 ratings