I seem to be running into a circular dependency issue in the scenario where my new build has a new module (B), new code in an existing module (A) that depends on the new module (B) and new config being imported for the new module (B).

In the past I have handled this with creating and deploying two independent builds, one where I deploy the new modules and enable them, and in the second build, deploy code that requires the new modules and the config for the new modules.

Is there a better way to handle this?

The change in Module A is that one of the services in Module A requires a service from Module B.

I have already added dependency to the new module B in module_A.info.yml file but that does not help as the module is already installed.

drush en module_b does not work and it gives the error.

The service "module_A_service" has a dependency on a non-existent service "module_B_service".

The config import that is in the build has core.extensions updated to enable module_B when it is imported but config import with drush cim is failing with the same error as above.

  • I have done what you have done. You can also think about detecting the other module or service and falling back onto some basic behavior.
    – mona lisa
    Commented May 9, 2022 at 19:41
  • It sounds like you want to add a hook_update_N in module_A to install module_B
    – Clive
    Commented May 9, 2022 at 20:05
  • Thanks for the responses @cilefen Aren't service dependencies kind of hard in the way the dependencies are added in the yml files?
    – anoopjohn
    Commented May 9, 2022 at 20:15
  • Thanks for the response @Clive - I didn't explicitly try that, but I tried a drush php eval of \Drupal::service('module_installer')->install(['module_b']);. That gave the same error. Would it behave differently in hook_update. I could try that as well.
    – anoopjohn
    Commented May 9, 2022 at 20:15
  • The error indicates that your A service has a dependency that does not exist, which is not a circular dependency. What is the error if you deploy everything in one commit, rather than splitting it into two?
    – Jaypan
    Commented May 9, 2022 at 20:19

1 Answer 1


This is the solution that worked

a) Implement a hook_update_N in module A that is already installed. In the hook_update_N add code to install module_b. Include this in the same build.

function module_a_update_NNNN(&$sandbox) {
  $module_installer = \Drupal::service('module_installer');
  $module_handler = \Drupal::service('module_handler');
  if (!$module_handler->moduleExists('module_b')) {

b) Deploy the build

c) Do not clear cache. If you run cache clear before updb it will throw the same error as mentioned in the question.

d) Run drush updb

e) Run drush cim

f) Run drush cr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.