Skip to content

Fix parsing of export with a single-character variable name - #598

Closed
semx wants to merge 1 commit into
phpdotenv:masterfrom
semx:fix-export-single-char-name
Closed

semx wants to merge 1 commit into
phpdotenv:masterfrom
semx:fix-export-single-char-name

Conversation

@semx

@semx semx commented Aug 21, 2026

Copy link
Copy Markdown

A dotenv file containing an export line with a single-character variable name fails to parse:

export A=1
Dotenv\Exception\InvalidFileException: Failed to parse dotenv file.
Encountered an invalid name at [export A].

Since the exception is thrown at file level, one such line rejects the entire file. export AB=1 and longer names work fine.

The cause is the guard in EntryParser::parseName():

if (Str::len($name) > 8 && Str::substr($name, 0, 6) === 'export' && \ctype_space(Str::substr($name, 6, 1))) {

export A is exactly 8 characters, so the strict > 8 comparison skips the prefix stripping and the raw string export A is then rejected as an invalid name.

This PR relaxes the guard to >= 8 and adds regression tests for both space and tab separators. The other conditions are unchanged, so inputs like exportFOO=... (no whitespace after the prefix) or a variable actually named export are unaffected.

Full test suite passes: 252 tests, 518 assertions.

The guard in parseName() required the raw name to be strictly longer
than 8 characters before stripping the leading "export " prefix. A
single-character variable name ("export A") is exactly 8 characters,
so the prefix was never stripped and the entry failed with
InvalidFileException, rejecting the whole file. Two-character names
and longer were unaffected.

Relax the guard to >= 8 and cover the boundary with tests for both
space and tab separators.
@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Thanks for the report and the fix! I went with a slightly different approach in #599, dropping the length check altogether rather than adjusting it, since the prefix and whitespace checks already cover it. That's merged now and will go out in 5.6.5. Closing this in favour of that, but much appreciated for tracking it down.

@semx

semx commented Aug 24, 2026

Copy link
Copy Markdown
Author

Dropping the guard entirely is cleaner indeed, the prefix and whitespace checks already imply a valid length. Thanks for the quick turnaround!

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.

2 participants