Skip to content

🐛 Fix intermittent env loading failures#800

Merged
retlehs merged 2 commits into
masterfrom
fix/env-options-local-first
Mar 8, 2026
Merged

🐛 Fix intermittent env loading failures#800
retlehs merged 2 commits into
masterfrom
fix/env-options-local-first

Conversation

@retlehs

@retlehs retlehs commented Mar 8, 2026

Copy link
Copy Markdown
Member

Summary

  • Replace USE_ENV_ARRAY with LOCAL_FIRST in Env\Env::$options
  • Replace magic-number config (31) with explicit bit flags for readability and intent

Fixes #756

Problem

USE_ENV_ARRAY forces oscarotero/env to read from $_ENV only. In #756, multiple users reported intermittent misses in some contexts (notably WP-CLI and sequential command flows), where env() returns null for expected values (e.g. DB_HOST), leading to fallback behavior like localhost and database connection failures.

Fix

Set Env\Env::$options explicitly to CONVERT_BOOL | CONVERT_NULL | CONVERT_INT | STRIP_QUOTES | LOCAL_FIRST.

This changes lookup to getenv() (local first), which aligns with Bedrock's dotenv repository setup that includes PutenvAdapter, while still avoiding $_SERVER (the security goal of #598).

Test results

Tested on a Trellis VM running Bedrock with this change applied:

Basic env loading:

$ trellis vm shell -- wp eval 'echo env("DB_HOST") . " " . env("DB_NAME") . " " . env("WP_HOME");'
localhost example_com_development http://example.test

Nested WP-CLI commands (the reported #756 failure case):

$ trellis vm shell -- wp eval 'echo "outer: " . env("DB_HOST") . PHP_EOL; WP_CLI::runcommand("eval \"echo \\\"inner: \\\" . env(\\\"DB_HOST\\\") . PHP_EOL;\"");'
outer: localhost
inner: localhost

wp rewrite structure (triggers internal rewrite flush, another reported failure):

$ trellis vm shell -- wp rewrite structure '/%postname%/'
Success: Rewrite structure set.
Success: Rewrite rules flushed.

Repeated read consistency:

$ trellis vm shell -- wp eval 'for ($i = 0; $i < 5; $i++) { echo env("DB_HOST") . " "; }'
localhost localhost localhost localhost localhost
Consistent: yes

$_SERVER not leaking .env secrets (#598 security intent preserved):

$ trellis vm shell -- wp eval 'echo "SERVER has DB_HOST: " . (isset($_SERVER["DB_HOST"]) ? "YES (leak!)" : "no");'
SERVER has DB_HOST: no

🤖 Generated with Claude Code

Replace `USE_ENV_ARRAY` with `LOCAL_FIRST` in `Env\Env::$options` so
env lookups use `getenv()` instead of the unreliable `$_ENV` superglobal.
Also replace magic number `31` with explicit bit flags.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@retlehs retlehs merged commit e59b8d2 into master Mar 8, 2026
8 checks passed
@retlehs retlehs deleted the fix/env-options-local-first branch March 8, 2026 20:46
retlehs added a commit that referenced this pull request Mar 8, 2026
Stale formatting from #800.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
retlehs added a commit that referenced this pull request Mar 8, 2026
* ✅ Add Pest testing scaffold

* 🎨 Format application.php

Stale formatting from #800.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
xelmido pushed a commit to xelmedia/bedrock-headless-zilch that referenced this pull request Apr 1, 2026
Replace `USE_ENV_ARRAY` with `LOCAL_FIRST` in `Env\Env::$options` so
env lookups use `getenv()` instead of the unreliable `$_ENV` superglobal.
Also replace magic number `31` with explicit bit flags.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
xelmido pushed a commit to xelmedia/bedrock-headless-zilch that referenced this pull request Apr 1, 2026
* ✅ Add Pest testing scaffold

* 🎨 Format application.php

Stale formatting from roots#800.

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.env file is not loaded sometimes

1 participant