Skip to content

🩹 Skip env file loading when no .env file exists#500

Merged
retlehs merged 2 commits into
roots:mainfrom
apermo:fix/skip-env-loading-when-file-missing
Mar 9, 2026
Merged

🩹 Skip env file loading when no .env file exists#500
retlehs merged 2 commits into
roots:mainfrom
apermo:fix/skip-env-loading-when-file-missing

Conversation

@apermo

@apermo apermo commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #496

When no .env file exists anywhere in the directory tree, Application::environmentPath() falls back to basePath(). phpdotenv's Reader::readFromFile() then calls @file_get_contents() on the non-existent <basePath>/.env, emitting a suppressed PHP warning. Error tracking tools like Sentry still capture these suppressed warnings, generating noise on every request.

Approach

This adds a Roots\Acorn\Bootstrap\LoadEnvironmentVariables bootstrapper — following the existing pattern used by LoadConfiguration, HandleExceptions, and RegisterFacades — that checks if the environment file exists before attempting to load it. If the file doesn't exist, loading is skipped entirely since environment variables are provided by other means (server config, Docker, GKE, etc.).

Both Http\Kernel and Console\Kernel are updated to use the new Acorn bootstrapper.

Changes

  • New: src/Roots/Acorn/Bootstrap/LoadEnvironmentVariables.php — extends Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables with an is_file() guard
  • Updated: src/Roots/Acorn/Http/Kernel.php — use Roots\Acorn\Bootstrap\LoadEnvironmentVariables
  • Updated: src/Roots/Acorn/Console/Kernel.php — use Roots\Acorn\Bootstrap\LoadEnvironmentVariables

No vendored src/Illuminate/ files are modified.

@QWp6t QWp6t left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, but unfortunately this won't be merged in its current state because it updates a vendored Illuminate file. Nothing within src/Illuminate/ should be modified.

@apermo

apermo commented Feb 25, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the quick reply, I'll look for another option. And come back with an updated version.

@apermo apermo force-pushed the fix/skip-env-loading-when-file-missing branch from ace303d to 22c2b23 Compare February 25, 2026 13:19
@apermo apermo requested a review from QWp6t February 25, 2026 13:41
When no .env file exists anywhere in the directory tree,
Application::environmentPath() falls back to basePath().
phpdotenv then calls @file_get_contents() on the non-existent
file, which emits a suppressed PHP warning. Error tracking
tools like Sentry still capture these suppressed warnings,
generating noise.

This adds a Roots\Acorn\Bootstrap\LoadEnvironmentVariables
bootstrapper that checks if the environment file exists before
attempting to load it. If the file doesn't exist, loading is
skipped entirely since environment variables are provided by
other means (server config, Docker, etc.).

Fixes #496
@apermo apermo force-pushed the fix/skip-env-loading-when-file-missing branch from 22c2b23 to 2f675e9 Compare February 25, 2026 16:27
Tests cover:
- missing .env file skips dotenv load (no warning)
- existing .env file loads normally
- cached configuration skips loading entirely

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@retlehs retlehs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@retlehs retlehs merged commit 5710c68 into roots:main Mar 9, 2026
2 checks passed
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.

error reading env file, if no env file present

3 participants