Skip to content

Handle \n in quotes strings - #363

Closed
danmichaelo wants to merge 2 commits into
phpdotenv:masterfrom
danmichaelo:fix-newline
Closed

danmichaelo wants to merge 2 commits into
phpdotenv:masterfrom
danmichaelo:fix-newline

Conversation

@danmichaelo

@danmichaelo danmichaelo commented Jul 31, 2019 •

Copy link
Copy Markdown
Contributor

Background: I have a .env file that I also share with Docker. Docker doesn't handle newlines directly, so I escape them like so:

VARIABLE="Line1\nLine2"

I expected this to also work with phpdotenv, so I was caught by surprise when it didn't. Is there a compelling reason not to support it, or can support be added?

I'm attaching a failing test and also a patch that makes the test go green. In my implementation, I created an array of allowed characters (['n', 'r']), so that it can be easily expanded if more characters should also be allowed (like \t). If you don't like the implementation, please let me know how it could be improved, or just change it.

Note: I guess it could be argued that \n should not be expanded when using single quotes (the TEST_NS value in the multiline.env test). But as long as the parser currently uses the same state (QUOTED_STATE) for both single and double quotes, it seems hard to distinguish without a larger rewrite)

@GrahamCampbell

GrahamCampbell commented Aug 11, 2019 •

Copy link
Copy Markdown
Collaborator

Thanks for the PR. Had this been in 2.x, it would have been a breaking change, but we are now strict about slashes in 3.x, so we can introduce new escape sequences without being breaking. 👍

I think we'd need to support either none, or all of the common sequences. We'll definely need to add \t. I'm also wonder if we should add \v, \e or \f. I'm not sure what docker supports. Is there a list somewhere? There's a list for what PHP supports here: https://www.php.net/manual/en/language.types.string.php.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

It looks like \e is probably a bad idea actually. Here's the ones C supports: https://en.cppreference.com/w/c/language/escape.

I think we should add \f, \t, and \v to go along side \n and \r.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Note: I guess it could be argued that \n should not be expanded when using single quotes (the TEST_NS value in the multiline.env test). But as long as the parser currently uses the same state (QUOTED_STATE) for both single and double quotes, it seems hard to distinguish without a larger rewrite)

@danmichaelo Does docker only expand on double quoted strings?

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

I guess it doesn't hurt to expand for both cases, since our parser will just fail otherwise.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Thanks, @danmichaelo. I've merged this, and it'll be included in the v3.5.0 release. 🚀

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