Skip to content

.env file is not loaded sometimes #756

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

Open
tysian opened this issue Jan 16, 2025 · 7 comments
Open

.env file is not loaded sometimes #756

tysian opened this issue Jan 16, 2025 · 7 comments
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.

@takotakot
Copy link

We used this software, adding testing.php, to perform local tests. Since .env.local is not being loaded, PHPUnit is not working correctly.

The addition of testing.php is based on the following PR and comments:

#366
#366 (comment)

@retlehs
Copy link
Member

retlehs commented Feb 17, 2025

@takotakot I don't think your comment is relevant to this issue?

@takotakot
Copy link

@retlehs
Yes. .env file is not loaded because of #598. We are impacted.

@ethanclevenger91
Copy link
Contributor

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.

Yeah this is specifically turning up during custom WP-CLI commands for us. Maybe more specifically, calling WP_CLI commands within other WP_CLI commands. Switching $options solved the problem. I'm not sure what the deal is, but 31 doesn't seem to even be a valid option for the env library?

@hainm0912
Copy link

hainm0912 commented Mar 10, 2025

I have the same problem
I use base wordpress does not happen same bug, wait bedrock fix

@pfcgoncalves
Copy link

The same thing happens to me, the problem is with the USE_ENV_ARRAY option.

I managed to solve it by setting php.ini to

variables_order = "EGPCS" default is GPCS

or changing the options to 15.

Env\Env::$options = 15;

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

7 participants