Service is being instantiated during installation with unknown entity type call I have a module that defines two services. Service A has service B as dependency.

Service B has some dependencies, one of which is entity type manager.

In the constructor of the service B an entity storage is being called for entity type that is defined by this module as well

public function __construct(AccountProxy $user, EntityTypeManager $entity_manager, QueryFactory $entity_query) {
    $this-&gt;user = $user;
    $this-&gt;storage = $entity_manager-&gt;getStorage('entity_type_name');
    $this-&gt;query = $entity_query-&gt;get('entity_type_name');
  }


During installation I get error about unknown entity and the installation breaks. I have tried to backtrace the cause of this but so far it looks like everything is in order. The container is only parsing the service definitions and instantiates them when needed. What I think might be the issue is the order in which the entity definitions are processed and service definitions parsed.

I do not want to add any condition into service B constructor to check if installation is in progress. I think this should not be happening.

What would be a proper workaround?
While it will require an additional call when you require it, you could just store a reference to the entity type manager only.

Especially for entity query, you must not do it like this anyway. A query object is not re-usable. You can't execute multiple queries from a single instance.

Additionally, you can simplify your dependencies if you call $storage->getQuery() instead of using the entity query for that (which does the same internally).