Symfony 4 - Service removed or inlined though it is public

Ben picture Ben · Jan 24, 2018 · Viewed 13.8k times · Source

I'm trying to migrate a SF 3.3 app to SF 4 with its new directory structure and everything.

I'm struggling on this exception:

The "simplethings_entityaudit.reader" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

(This service comes from an external bundle located in /vendor).

Nevertheless, when I bin/console debug:container simplethings_entityaudit.reader you'll see the service exists and is public:

Information for Service "simplethings_entityaudit.reader"
=========================================================

 ----------------- -------------------------------------- 
  Option            Value                                 
 ----------------- -------------------------------------- 
  Service ID        simplethings_entityaudit.reader       
  Class             SimpleThings\EntityAudit\AuditReader  
  Tags              -                                     
  Public            yes                                   
  Synthetic         no                                    
  Lazy              no                                    
  Shared            yes                                   
  Abstract          no                                    
  Autowired         no                                    
  Autoconfigured    no                                    
  Factory Service   simplethings_entityaudit.manager      
  Factory Method    createAuditReader                     
 ----------------- -------------------------------------- 

This service is currently called in one of my own with $this->container->get('simplethings_entityaudit.reader').

I also tried to inject SimpleThings\EntityAudit\AuditReader into my service constructor, but here's what I get:

Argument "$auditReader" of method "__construct()" references class "SimpleThings\EntityAudit\AuditReader" but no such service exists. It cannot be auto-registered because it is from a different root namespace.

When I add this into my services.yaml it works, but I shouldn't need to do this:

SimpleThings\EntityAudit\AuditReader:
    alias: simplethings_entityaudit.reader

Any ideas?

Answer

Hendrik Pilz picture Hendrik Pilz · Mar 25, 2019

In my case, the error appears in a unit test.

I had a single service, which could not be loaded in tests (Symfony 4.2) while all other services in my project worked well.

I've cleared the cache, but it didn't help. Then I created a simple controller with a route and injected the service as method parameter. Afterwards the service worked in my test as well.

Conclusion: If you have a unit test and want to test your service, you must also provide a controller where the service is injected, otherwise it is not available in the test service container. An explicit service configuration might also help.