-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
Preconditions
- Magento 2.2.7;
composer require kassner/module-test-admin-observer:dev-master
(source code)php bin/magento module:enable Kassner_TestAdminObserver
Steps to reproduce
- Manually edit the
app/etc/config.php
file adding a configuration (i.e.: https://gist.github.com/kassner/d1368133ec09541a7a0927cd3964c3e0) - Run
php bin/magento app:config:import
Expected result
The observer declared in the module is not executed:
www-data@ec46ba01ae51:~/magento$ php bin/magento app:config:import
Processing configurations data from configuration file...
System config was processed
www-data@ec46ba01ae51:~/magento$
Actual result
The observer declared in the module is executed:
www-data@ec46ba01ae51:~/magento$ php bin/magento app:config:import
Processing configurations data from configuration file...
string(64) "Kassner\TestAdminObserver\Observer\ModelSaveBeforeAdmin::execute"
System config was processed
string(64) "Kassner\TestAdminObserver\Observer\ModelSaveBeforeAdmin::execute"
www-data@ec46ba01ae51:~/magento$
Description
I did some extensive debugging and found out that during the config import, some methods are invoked using the emulateAreaCode for the adminhtml
area. This is fine, but some classes, like Magento\Framework\Event\Config
are populated with information related to the adminhtml area, but are never cleaned up or reinstantiated (given the way Magento dependency resolution works), making the next calls outside the emulation to use a dirty configuration, thus calling observers that were just declared for the adminhtml area.
I don't know a good solution for it right now, hence I come here to discuss this. Should we not have this emulation step at all during the app:config:import, or should we make sure that everything the emulateAreaCode
steps touch are properly cleaned after?
I have a very cheap patch that I created to test this problem in other environments, and I would love some feedback from anyone that is currently running into those kind of issue.
Extra
This is also related to random failures during setup:upgrade
or app:config:import
with the message Import failed: Area code not set: Area code must be set before starting a session.
for Magento Commerce installations. This is related to the model_save_before
observer from Magento_AdminGws kicking in and a dependency of this observer uses the backend session.
Thank you!