It's fairly common to split up settings and configuration between multiple files, and to include these files in settings.php.
However, it appears that settings.php is read multiple times during site installs, leading to unexpected behavior if you use include_once or require_once (instead of include or require) in settings.php. For instance, if you set $config_directories in a separate file and call it with require_once, the site install task will not be able to read it, and a random config_directory value will be written to settings.php.
I believe the root cause is that settings.php is being included by the core install / bootstrap tasks multiple times. The first time, $config_directories is not defined as a global prior to the file being included, so this variable is simply lost. Only on a later include (such as in Drupal\Core\Site\Settings::Initialize()) is $config_directories defined as a global, but by this point it's too late as the variable won't be read again.
I can see two solutions: either document in settings.php that you shouldn't use include_once / require_once, or find all of the functions where settings.php is included by core and ensure that in each case all of the possible variables are defined as globals.
Issue fork drupal-2793259
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
Comment #2
cilefen commentedSo, predefining $config_directories works, but moving it to a require_once at the same place, it is ignored. Can you reproduce this on 8.2.x? I ask because there was an issue I can't find at the moment that was similar.
Comment #3
cashwilliams commentedCan't reproduce on 8.2.x
Comment #4
danepowell commentedI can confirm Cash's finding, this seems to no longer be an issue in 8.2.0-beta3.
Closing as fixed, but if you can find the other issue you can mark it as a dupe.
Comment #5
cashwilliams commentedShould the fix be back ported to 8.1?
Comment #6
danepowell commentedIf it were up to me, yes :) Maybe at least leave this open for future travelers who run across the same issue.
Comment #9
quietone commentedIn #4, This was reported fixed in 8.2.0-beta3. It was reopened in #6 in the hope it was backported and to help others. That was over four years and and 8.2.x is no longer supported. It seems sensible to close this as outdated.
Comment #12
fengtanI seem to be able to reproduce this on Drupal 9.1.
Here is what I did:
settings.phpinclude another file namedsettings.included.php(note the use ofrequire_onceinstead ofrequire)settings.phpis left untouchedsettings.phprequireinstead ofrequire_oncesettings.phpwill be left untouched, as expectedMaybe we could document this in
sites/default/default.settings.php? I have opened project/drupal!488.Comment #13
fengtanComment #18
smustgrave commentedCan the MR be updated for 10.1 please.
Comment #19
cafuego commentedThis issue is now making my drush calls fail on Drupal 10.1 because my skeleton settings.php used require_once to grab pretty much *all* actual config from a place I manage with config-as-code tools. When I run drush I get:
Of course, when Drupal is accessed through the web server, it bootstraps fine and everything works as expected.
Comment #20
szeidler commentedWe're running into the same issue as #19. The error is triggered when using `include_once`. With `include` it works just fine.
For us the error message only happens when using Drush 12. With Drush 11 it works fine.
Comment #22
brad.bulger commentedA colleague reports discovering a problem with require/require_once in 10.3. Our settings.php file requires default.settings.php and then adds some customization. What we found: