Magento 2 Setup – How to Run setup:di:compile

bin-magentomagento2setup-di-compilesetup-upgrade

I have been working in a project with some custom code… this is our first "medium" Magento 2 project, so (as all people here I think) every day we learn new things, and we have to change the way to deal with this new Magento version

The reason for this question is asking about the command setup:di:compile

I've been using it since the first day with Magento 2, as bin/magento asks for it after every setup:upgrade, with message "Please re-run Magento compile command"

Well… I have found executing setup:di:compile breaks product view page in this project, with a totally ambiguous Fatal Error. I've spent whole working days trying to debug it and testing with code changes with zero result

Today, I have discovered that if I omit that command then all works like a charm, even in production mode

So, the question is… what exactly does that setup:di:compile command? Is it required? Just recommended? Or it is some deprecated command, which is not needed to be executed?

UPDATE

As some users have required, this is the Fatal Error I was referring

PHP Fatal error: Cannot instantiate abstract class
Magento\Catalog\Block\Product\View\AbstractView in
***/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php
on line 93

I've searched for any custom block using Magento\Catalog\Block\Product\View\AbstractView but I've found it only in layout files, it is not present in any block class constructor

What I can't understand is: why Magento is throwing this Fatal Error with compiled code, but it works like a charm without compiled code

Best Answer

The command setup:di:compile command generates the contents of the var/di folder in Magento <2.2 and generated for Magento >= 2.2

According to Magento, this serves the following purpose:

  • Application code generation (factories, proxies, and so on)
  • Area configuration aggregation (that is, optimized dependency injection configurations per area)
  • Interceptor generation (that is, optimized code generation of interceptors)
  • Interception cache generation
  • Repositories code generation (that is, generated code for APIs)
  • Service data attributes generation (that is, generated extension classes for data objects)

Source (http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-compiler.html)

However, when you place Magento in production mode, without compilation it indeed still works. So according to the Magento docs this is more an optimization step (that is, optimized code generation of interceptors)

When we have errors in the setup:di:compile command, this is mostly because of errors in one of the constructors of custom php classes.

Related Topic