magento2: Import failed: Area code not set: Area code must be set before starting a session.
Preconditions
- Magento 2.2.3 or 2.2.4
- PHP 7.1.18
- Ubuntu 16.04.4 LTS
- mysql Ver 14.14 Distrib 5.7.22-22, for debian-linux-gnu (x86_64) using 6.3
Steps to reproduce
- clean install of Magento 2.2.3 or 2.2.4
- bin/magento app:config:dump
- bin/magento app:config:import
- change only the base_url in the env.php files
'web' => array ( 'unsecure' => array ( 'base_url' => 'http://m223ee.ffm/', 'base_link_url' => '{{unsecure_base_url}}', - bin/magento app:config:import
Expected result
- Expected output : Processing configurations data from configuration file… System config was processed
Actual result
- Output: Processing configurations data from configuration file… Import failed: Area code not set: Area code must be set before starting a session.
- var/log/exception.log:
report.ERROR: Area code not set: Area code must be set before starting a session. {“exception”:“[object] (Magento\Framework\Exception\State\InvalidTransitionException(code: 0): Area code not set: Area code must be set before starting a session. at /home/springerin/Workspace/Progetti/m224ee/vendor/magento/module-config/Model/Config/Importer.php:137, Magento\Framework\Exception\SessionException(code: 0): Area code not set: Area code must be set before starting a session. at /home/springerin/Workspace/Progetti/m224ee/vendor/magento/framework/Session/SessionManager.php:175, Magento\Framework\Exception\LocalizedException(code: 0): Area code is not set at /home/springerin/Workspace/Progetti/m224ee/vendor/magento/framework/App/State.php:152)”} []
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 22
- Comments: 34 (10 by maintainers)
We had to run an extra query (but maybe that is Magento Cloud specific, in our case when moving a database from one environment to the other). And we are just clearing the flag data instead of deleting them entirely, this probably doesn’t make a difference, but I’m not entirely sure. Anyway, maybe it helps:
Credits to @joeshelton-wagento who pointed me in this direction on Slack a couple of months ago 😃
I was able to work around the issue by deleting the system_config_snapshot flag.
DELETE FROM flag WHERE flag_code = 'system_config_snapshot';In my case, I received the same error using either
setup:upgradeorapp:config:import.I’ve triggered this error when building custom console commands. To get past it, I had to try to set the area code explicitly and catch a LocalizedException:
I’m not getting this error with vanilla Magento, so I can’t test or guarantee a solution. That said, the
setup:upgradeclass instantiates and runs anapp:config:importobject like so:(From / vendor / magento / magento2-base / setup / src / Magento / Setup / Console / Command / UpgradeCommand.php)
So, someone having an area code problem with either
app:config:importorsetup:upgradecould attempt to set the area code in/vendor/magento/module-deploy/Console/Command/App/ConfigImportCommand.phpto see if that gets them past the error, even if duct-taping the core is the furthest thing from a long-term solution.@tuyennn Magento 2 is pretty hard to develop now days. There are just too many little triggers that might affect the stability of the platform.
I hope Magento team can make this better in the near future.
Hello @springerin, thank you for your report. We’ve acknowledged the issue and added to our backlog.
Also happens in Magento 2.3.0 with multiple sites/webstore/view when trying to install Mageplaza_StoreLocator.
As a temporary work around I modified vendor/magento/framework/Session/SessionManager.php
But agree that emulating the area code in the Setup/Upgrade php classes appears to be the better solution.
Needed to delete the module row from the setup_module table as it had schema-version set but data_version was null.
I recently encountered this issue as well. This time working on a Magento 2.1.X store.
We recently removed all command output in favour of logging. This caused the aforementioned “Area code not set: Area code must be set before starting a session.” exception to be thrown.
We fixed this by ensuring the command has output by writing a line to the console. ($output->writeln()).
The issue is the following: when your command has a dependency which somehow injects the SessionManager class, the \Magento\Framework\Session\SessionManager::start() function which is called in the constructor. Causes the class to attempt to initialize a session.
The reason why adding output to your console commands solves the problem is that headers are send and the session is never initialized. (\Magento\Framework\Session\SessionManager::isSessionExists)
I realize that this does not seem to be a proper future-proof solution, but at least it works!
In my case, I followed @hostep 's advice here: and had to make sure that there were no values set in my
env.phpwhich conflicted with what was saved in thesystem_config_snapshotvalue. Again for me, it was havingsystem/default/catalog/search/engine/mysqlin myenv.phpwhereas a db dump I was importing had a value in theflagtable saved in thesystem_config_snapshotjson. So the gist of my situation was to make sure there’s no conflicting values between the two. Hope that helps someone.Thanks for the tips.
have been nginx fan at all time 😃
Don’t remove the
generateddirectory directly, as that will wipe out its.htaccessfile. That would be a security hazard in Apache-hosted websites.To remove generated files, use the approach recommended by the dev docs:
Note that it removes the contents of the
metadataandcodedirectories, not the directories themselves.Just wanted to leave this here in case it helps someone. I had this same error and it was actually due to some formatting issues (line breaks) created by the
config:dumpcommand. This caused the import parsing to fail and the exception thrown was misleading. Hope this helps someone else!