Sometimes console commands use DI in a way that instantiates objects which have side-effects or do a lot of work to initialize themselves in the __construct
method. This generally will surface in one of two ways:
a) Errors when running bin/magento due to application scope not being set
b) Inability to run setup:install from-scratch due to a module instantiating an object which relies on tables which do not exist yet creating errors such as the following:
Table 'magento_integration_tests.flag' doesn't exist
Appling this patch will cause Magento to output a list of all shared instances instantiated by the ObjectManager during the DI process. This can be useful in debugging the above issues. Inspect the output for Command
classes, and then trace down what is instantiated following the last Command
class, and you may find the class that is at fault. Once you have this information, a bit of trial and error may be used such as disabling modules or appending \Proxy
to injected classes (remember to flush cache and clean the generated
directory) to defer their loading.
Usually adding \Proxy
permanently to the classes that are injected is sufficient to solve these errors. This is a wrapper class which will be injected by the Object Manager, only instantiating the class that's needed when it is first used. More info on those here: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/proxies.html