Skip to content

Conversation

torreytsui
Copy link
Contributor

@torreytsui torreytsui commented Nov 5, 2016

Related issues:
#4568
magento/magento2-sample-data#19

Bug reproduction

Prerequisite: a newly installed Magento 2.1 instance

Steps:

  1. bin/magento sampledata:deploy
  2. bin/magento module:enable --all
  3. php bin/magento setup:upgrade

Expected result: sample data installed successfully

Actual result:

...

Module 'Magento_Authorizenet':
Module 'Magento_OfflineShippingSampleData':
Module 'Magento_BundleSampleData':
Module 'Magento_ConfigurableSampleData':
                                                     
  [Magento\Framework\Exception\SessionException]                       
  Area code not set: Area code must be set before starting a session.  

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

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

Suggested fix

Break the dependency chain by proxy so that the session requirement will only be required right before it is necessary.

Cause of issue

\Magento\ImportExport\Model\History depends on \Magento\Backend\Model\Auth\Session and it requires session to be ready before construction.

While this requirement is likely to be satisfied in most cases on frontend, it is not the case when we run bin/magento setup:upgrade

Dependency Hierarchy

\Magento\ConfigurableSampleData\Setup\Installer
   |
  \Magento\ConfigurableSampleData\Model\Product
     |
    \Magento\ImportExport\Model\Import
       |
      \Magento\ImportExport\Model\History
          |
         \Magento\Backend\Model\Auth\Session
#0  unknown() called at [/src/m2/vendor/magento/framework/Session/SessionManager.php:169]
#1  Magento\Framework\Session\SessionManager->start() called at [/src/m2/var/generation/Magento/Backend/Model/Auth/Session/Interceptor.php:167]
#2  Magento\Backend\Model\Auth\Session\Interceptor->start() called at [/src/m2/vendor/magento/framework/Session/SessionManager.php:130]
#3  Magento\Framework\Session\SessionManager->__construct() called at [/src/m2/vendor/magento/module-backend/Model/Auth/Session.php:88]
#4  Magento\Backend\Model\Auth\Session->__construct() called at [/src/m2/var/generation/Magento/Backend/Model/Auth/Session/Interceptor.php:14]
#5  Magento\Backend\Model\Auth\Session\Interceptor->__construct() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:93]
#6  Magento\Framework\ObjectManager\Factory\AbstractFactory->createObject() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:89]
#7  Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [/src/m2/vendor/magento/framework/ObjectManager/ObjectManager.php:71]
#8  Magento\Framework\ObjectManager\ObjectManager->get() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:126]
#9  Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:53]
#10 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:82]
#11 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [/src/m2/vendor/magento/framework/ObjectManager/ObjectManager.php:71]
#12 Magento\Framework\ObjectManager\ObjectManager->get() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:126]
#13 Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:53]
#14 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:82]
#15 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [/src/m2/vendor/magento/framework/ObjectManager/ObjectManager.php:71]
#16 Magento\Framework\ObjectManager\ObjectManager->get() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:126]
#17 Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:53]
#18 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:82]
#19 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [/src/m2/vendor/magento/framework/ObjectManager/ObjectManager.php:71]
#20 Magento\Framework\ObjectManager\ObjectManager->get() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:126]
#21 Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:53]
#22 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:82]
#23 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [/src/m2/vendor/magento/framework/ObjectManager/ObjectManager.php:71]
#24 Magento\Framework\ObjectManager\ObjectManager->get() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php:126]
#25 Magento\Framework\ObjectManager\Factory\AbstractFactory->resolveArgument() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:53]
#26 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->_resolveArguments() called at [/src/m2/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php:82]
#27 Magento\Framework\ObjectManager\Factory\Dynamic\Developer->create() called at [/src/m2/vendor/magento/framework/ObjectManager/ObjectManager.php:57]
#28 Magento\Framework\ObjectManager\ObjectManager->create() called at [/src/m2/setup/src/Magento/Setup/Model/Installer.php:931]
#29 Magento\Setup\Model\Installer->createSchemaDataHandler() called at [/src/m2/setup/src/Magento/Setup/Model/Installer.php:1245]
#30 Magento\Setup\Model\Installer->getSchemaDataHandler() called at [/src/m2/setup/src/Magento/Setup/Model/Installer.php:841]
#31 Magento\Setup\Model\Installer->handleDBSchemaData() called at [/src/m2/setup/src/Magento/Setup/Model/Installer.php:796]
#32 Magento\Setup\Model\Installer->installDataFixtures() called at [/src/m2/setup/src/Magento/Setup/Console/Command/UpgradeCommand.php:74]
#33 Magento\Setup\Console\Command\UpgradeCommand->execute() called at [/src/m2/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:257]
#34 Symfony\Component\Console\Command\Command->run() called at [/src/m2/vendor/symfony/console/Symfony/Component/Console/Application.php:874]
#35 Symfony\Component\Console\Application->doRunCommand() called at [/src/m2/vendor/symfony/console/Symfony/Component/Console/Application.php:195]
#36 Symfony\Component\Console\Application->doRun() called at [/src/m2/vendor/magento/framework/Console/Cli.php:96]
#37 Magento\Framework\Console\Cli->doRun() called at [/src/m2/vendor/symfony/console/Symfony/Component/Console/Application.php:126]
#38 Symfony\Component\Console\Application->run() called at [/src/m2/bin/magento:23]

This will allow import history dependants to be created before session / store simulation start
@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Nov 5, 2016

CLA assistant check
All committers have signed the CLA.

@torreytsui torreytsui changed the title Fix import history dependant needs session before injected Fix import history dependant needs session before injected (#4568) Nov 5, 2016
@vrann
Copy link
Contributor

vrann commented Mar 24, 2017

@torreytsui Thank you for the contribution. Please check comments in the review

@@ -59,7 +59,7 @@ public function __construct(
\Magento\ImportExport\Model\ResourceModel\History $resource,
\Magento\ImportExport\Model\ResourceModel\History\Collection $resourceCollection,
\Magento\ImportExport\Helper\Report $reportHelper,
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Backend\Model\Auth\Session\Proxy $authSession,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proxy should never be requested directly from the constructor. Instead, you should configure DI via di.xml to substitute Proxy for the authSession parameter.

@vrann vrann self-assigned this Mar 24, 2017
@vrann vrann added this to the March 2017 milestone Mar 24, 2017
@okorshenko okorshenko modified the milestones: March 2017, April 2017 Apr 2, 2017
@okorshenko okorshenko modified the milestones: April 2017, May 2017 May 9, 2017
@vrann
Copy link
Contributor

vrann commented May 19, 2017

@torreytsui please see the comment to the code. It is based on http://devdocs.magento.com/guides/v2.1/coding-standards/technical-guidelines/technical-guidelines.html

Please reopen PR once fixed.

@ahmed-oz
Copy link

ahmed-oz commented Apr 7, 2018

run this command
php bin/magento sampledata:reset
then run the upgrade

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants