Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env file is not loaded sometimes #756

Open
tysian opened this issue Jan 16, 2025 · 1 comment
Open

.env file is not loaded sometimes #756

tysian opened this issue Jan 16, 2025 · 1 comment
Labels

Comments

@tysian
Copy link

tysian commented Jan 16, 2025

Version

1.26.0

What did you expect to happen?

Read all env values from .env file.

What actually happens?

Environmental variables are not loaded from .env file and DB_HOST value fallbacks to localhost. "Error establishing a database connection" is thrown. Need to refresh a couple of times to open desired page. It happens like 33% times when navigating through the wp admin.

I've found a workaround by reverting this #598 PR.

Steps to reproduce

  1. Install Bedrock
  2. Add external db credentials in .env file
  3. Go to "create new post" admin page
  4. Refresh it a couple of times

System info

Windows 10 Professional
Laragon 5.0.0
Apache httpd 2.4.35 VC15
PHP 8.1.3
MySQL 8.0.33-25 (external database)

Log output

Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or that contact with the database server at localhost could not be established. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure you have typed the correct hostname?
Are you sure the database server is running?
If you are unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress support forums.

Please confirm this isn't a support request.

Yes

@tysian tysian added the bug label Jan 16, 2025
@tysian tysian changed the title phpdotenv library is not reading .env file sometimes .env file is not loaded sometimes Jan 16, 2025
@xelmido
Copy link

xelmido commented Jan 22, 2025

I’m encountering the same issue with WP-CLI (not sure if this applies to your case). Reverting the changes didn’t resolve it for me, although I agree with the importance of the security fix. As a workaround, I found that setting Env\Env::$options to 15 resolved the issue. I don’t believe always reading the .env file would be harmful, though it might have a slight performance impact.

The problem seems to arise when WP-CLI performs sequential calls. For example, running wp rewrite structure '/%postname%/' also triggers wp rewrite flush. During the second call, my environment variables ($_ENV) appear to be missing. Despite debugging, I haven’t been able to figure out why $_ENV is lost during the second execution.

I also tried setting variables_order=EGPC in my PHP configuration, but unfortunately, it didn’t help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants