Repository navigation
Preserve carriage returns in values when reading and rewriting .env files - #724
Open
Sreekant13 wants to merge 2 commits into
Open
Sreekant13 wants to merge 2 commits into
Sreekant13 wants to merge 2 commits into
Conversation
Reads opened files with universal-newline translation, so a carriage return inside a value was rewritten to "\n" before the parser saw it. set_key quotes a value "for the value to survive a write/read round-trip" (its own words), yet a value such as "a\rb" was read back as "a\nb". The parser already recognises "\r\n", "\r" and "\n" itself, so the translation was both redundant and lossy, and reading a path disagreed with reading the same bytes from a stream. Open the read streams and the rewrite temp file with newline="" so no newline translation happens at the file layer. Values now round-trip byte-for-byte for "\r", "\n" and "\r\n".
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
set_key()quotes a value, in its own words, "for the value to survive a write/read round-trip", yet a value containing a carriage return does not survive it:The parser itself is not at fault. Feeding the exact same bytes through a stream returns the value intact:
The difference is that file reads (
DotEnv._get_streamandrewrite) open with universal-newline translation (open(..., newline=None)), which rewrites\rand\r\nto\nbefore the parser runs. Sodotenv_values(path)anddotenv_values(stream=...)disagree for identical bytes, and a carriage return in a value is silently lost. This has stayed hidden because the parser tests all feedStringIO, which does no translation.Fix
Open the read streams and the
rewritetemp file withnewline="". The parser already recognises\r\n,\rand\non its own (see_newline,_end_of_line,_unquoted_value), so disabling the file-layer translation makes path reads agree with stream reads and keeps values byte-exact. Round-trips now hold for\r,\n,\r\nand mixed line endings.A regression case is added to the existing
test_set_key_round_trips(it fails before this change for the\rvalues and passes after).Note on line endings
Because the temp file is now written with
newline="", lines thatset_key/unset_keywrite go out with\n. Untouched lines are echoed back with their original bytes. On a file that currently uses CRLF, an edited line is therefore written with LF while untouched lines keep CRLF. Fidelity of the stored value is the gain; I am happy to instead match the surrounding file's newline style if you prefer that behaviour.