Using include_once / require_once in settings.php leads to unexpected behavior

Created on 31 August 2016, almost 8 years ago
Updated 17 August 2023, 10 months ago

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.

πŸ› Bug report
Status

Needs work

Version

9.5

Component
BootstrapΒ  β†’

Last updated 9 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States Dane Powell

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Can the MR be updated for 10.1 please.

  • πŸ‡¦πŸ‡ΊAustralia cafuego

    This 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:

    In Settings.php line 171:                                                    
        Missing $settings['hash_salt'] in settings.php.

    Of course, when Drupal is accessed through the web server, it bootstraps fine and everything works as expected.

  • πŸ‡©πŸ‡ͺGermany szeidler Berlin

    We'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.

    In Settings.php line 171:                                                    
        Missing $settings['hash_salt'] in settings.php.
    
Production build 0.69.0 2024