Skip to content

fix: skip unreadable .env in Boot::loadDotEnv for separate-process tests#10417

Closed
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/load-dotenv-unreadable-silent
Closed

fix: skip unreadable .env in Boot::loadDotEnv for separate-process tests#10417
gr8man wants to merge 1 commit into
codeigniter4:developfrom
gr8man:fix/load-dotenv-unreadable-silent

Conversation

@gr8man

@gr8man gr8man commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Description

Test ResponseSendTest::testHeaderOverride fails when tests run in random order because the spawned process (#[RunInSeparateProcess]) calls Boot::loadDotEnv()DotEnv::parse(), which throws InvalidArgumentException when .env exists but is unreadable. The fix adds an early is_file() + is_readable() guard so loadDotEnv() returns silently — .env is optional.

Tests

No new tests — DotEnvTest::testLoadsUnreadableFile already covers the exception path. Verified with:

vendor/bin/phpunit tests/system/HTTP --order-by=random --random-order-seed=1784491692

@michalsn michalsn 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.

Now we would silently ignore a present but unreadable .env file. Not acceptable.

If you identified the test that causes this, you may configure the test bootstrap to use a controlled environment directory.

@gr8man
gr8man force-pushed the fix/load-dotenv-unreadable-silent branch from 352e956 to 92eac67 Compare July 20, 2026 17:56

@michalsn michalsn 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.

Please identify the test or operation that changes the file's permissions and causes the error.

I cannot recreate the issue by running the test command you provided.

Comment thread system/Test/bootstrap.php Outdated
Comment thread system/Boot.php Outdated
@gr8man

gr8man commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

vendor/bin/phpunit tests/system/HTTP --colors=never --no-coverage --do-not-cache-result --order-by=random --random-order-seed=1784491692

PHPUnit 11.5.56 by Sebastian Bergmann and contributors.

Runtime: PHP 8.5.8
Configuration: /home/runner/work/CodeIgniter4/CodeIgniter4/phpunit.dist.xml
Random Seed: 1784491692

Time: 00:09.249, Memory: 26.00 MB

There was 1 error:

  1. CodeIgniter\HTTP\ResponseSendTest::testHeaderOverride
    PHPUnit\Framework\Exception: PHP Fatal error: Uncaught CodeIgniter\Exceptions\InvalidArgumentException: The .env file is not readable: /home/runner/work/CodeIgniter4/CodeIgniter4/app/Config/../../.env in /home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/DotEnv.php:64
    Stack trace:
    #0 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/DotEnv.php(47): CodeIgniter\Config\DotEnv->parse()
    Initial license #1 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Boot.php(209): CodeIgniter\Config\DotEnv->load()
    APPPATH, SYSPATH and similar constants. Rename? #2 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Boot.php(177): CodeIgniter\Boot::loadDotEnv()
    Reverse Routing #3 /home/runner/work/CodeIgniter4/CodeIgniter4/system/Test/bootstrap.php(82): CodeIgniter\Boot::bootTest()
    Redirect Security #4 Standard input code(140): require_once('...')
    Replace our escaper with Zend Escaper #5 {main}
    thrown in /home/runner/work/CodeIgniter4/CodeIgniter4/system/Config/DotEnv.php on line 64

https://github.com/codeigniter4/CodeIgniter4/actions/runs/29701403791/job/88230952540#step:10:249

@gr8man

gr8man commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

I found the likely root cause of the test failure — it was in EncryptionTest itself, not in the framework code.

EncryptionTest::setUpBeforeClass() was renaming the entire .env.env.bak at the class level, and tearDownAfterClass() restored it after all test methods in the class finished. When tests run in random order (--order-by=random), other test classes — like ResponseSendTest with #[RunInSeparateProcess] — could execute between these hooks, finding .env in an unexpected state (renamed to .bak, or just restored mid-execution).

Fixed in commit 4875736: instead of moving the whole file, the test now only clears the encryption.key environment variable — that's all the rename was trying to achieve (preventing the encryption key from being loaded from .env).

With this fix, no framework changes are needed (DotEnv.php, Boot.php, bootstrap.php can stay untouched) — the problem was entirely in the test itself.

@gr8man
gr8man force-pushed the fix/load-dotenv-unreadable-silent branch from 92890fc to 9fdc43e Compare July 21, 2026 21:15
@github-actions github-actions Bot added the testing Pull requests that changes tests only label Jul 21, 2026
@gr8man
gr8man requested a review from michalsn July 22, 2026 10:48

@michalsn michalsn 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.

This cannot be the root cause of the reported random-test failure. The Encryption component was not enabled in the random-test configuration used by that run, so EncryptionTest was never executed.

The change may be a reasonable test cleanup, but it does not explain or resolve the cited failure.

@gr8man gr8man closed this Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Pull requests that changes tests only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants