Problem/Motivation

When modules are being installed during configuration sync, \Drupal::service('config.installer')->isSyncing() returns FALSE in hook_module_preinstall() implementations. This is because the kernel is updated and the container is rebuilt just prior to the preinstall hooks being invoked. The config installer sync status is not set back to TRUE until later.

Proposed resolution

Restore state of ConfigInstaller, including whether it is syncing and its source storage in \Drupal\Core\Extension\ModuleInstaller::updateKernel().

Pass ConfigInstaller::isSyncing value to hook_module_preinstall() and hook_module_preuninstall implementations.

Remaining tasks

(reviews needed, tests to be written or run, documentation to be written, etc.)

API changes

See change record: hook_module_preinstall() and hook_module_preuninstall() now have a second argument $is_syncing

Data model changes

Release notes snippet

Issue fork drupal-3103962

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

godotislate created an issue. See original summary.

godotislate’s picture

godotislate’s picture

StatusFileSize
new1.82 KB

Messed up diff with original patch. Trying again.

godotislate’s picture

Here's a failing test.

godotislate’s picture

Patch with tests.

godotislate’s picture

Status: Active » Needs review
xjm’s picture

Version: 9.0.x-dev » 9.1.x-dev

Since this fix would involve a behavior change, it's best done in a minor release. Moving to 9.1.x. Thanks!

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ranjith_kumar_k_u’s picture

StatusFileSize
new4.31 KB

Rerolled #5 for 9.5

Status: Needs review » Needs work

The last submitted patch, 12: 3103962-12.patch, failed testing. View results

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

godotislate’s picture

Status: Needs work » Needs review

Created MR against 11.x

smustgrave made their first commit to this issue’s fork.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative, +Bug Smash Initiative

Rebased to run test-only feature

1) Drupal\KernelTests\Core\Config\ConfigImporterTest::testIsSyncingInHooks
\Drupal::isConfigSyncing() returns TRUE
Failed asserting that null is true.
/builds/issue/drupal-3103962/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:121
/builds/issue/drupal-3103962/vendor/phpunit/phpunit/src/Framework/Constraint/Constraint.php:55
/builds/issue/drupal-3103962/core/tests/Drupal/KernelTests/Core/Config/ConfigImporterTest.php:848
/builds/issue/drupal-3103962/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
FAILURES!
Tests: 23, Assertions: 136, Failures: 1.

Hiding files for clarity.

Issue summary seems clear about what is being fixed and matches the solution in the MR.

LGTM!

xjm’s picture

Title: ConfigInstaller::isSyncing is not reset before module preinstall » ConfigInstaller::isSyncing() is not reset before module preinstall
larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Issue credits

Left some questions on the MR, tl;dr I think hook_module_preuninstall has the same issue and wonder if perhaps we should be doing the fix inside ::updateKernel so it resolves both issues at once.

alexpott’s picture

@larowlan excellent point about uninstall. Install and uninstall should be mirrors.

I'm slightly dubious about the use-case here because what are you doing in pre-install - really shouldn't be doing anything permanent - so whatever is happening probably should happen regardless of whether we are syncing - but hey workspaces does interesting stuff here but it is inconsistent so +1 to fixing.

I think we should also consider adding is_syncing to the hook signature to anyone who implements hook_module_preinstall() has to consider the config syncing case.

godotislate’s picture

I agree use cases are pretty obscure, and I'm having trouble recalling exactly how it was I ran into it four years ago. I believe that I was working on a project that had an install profile that used Config Rewrite (https://www.drupal.org/project/config_rewrite). That module has an hook_module_preinstall() implementation that I wanted to prevent from happening when installing from existing configuration. Though it looks like between then and now they found a different way to do that detection.

godotislate’s picture

Issue summary: View changes
Status: Needs work » Needs review

Addressed review comment to restore state of config installer in ::updateKernel.
Added $is_syncing value as second parameter to hook_module_preinstall() and hook_module_preuninstall implementations.
Updated tests and rebased.
Added CR and updated IS.

alexpott’s picture

Status: Needs review » Needs work

@godotislate - nice work - one tiny nit in a kinda out-of-scope change and this will be rtbc.

godotislate’s picture

Status: Needs work » Reviewed & tested by the community

Applied suggested change and moved to RTBC per last comment.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed ba827d4 and pushed to 11.x. Thanks!

  • alexpott committed ba827d46 on 11.x
    Issue #3103962 by godotislate, smustgrave, alexpott, larowlan:...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.