fix: normalise CRLF line endings and skip comment lines in parseEnvFile#2635
Open
saiashok0981 wants to merge 1 commit into
Open
fix: normalise CRLF line endings and skip comment lines in parseEnvFile#2635saiashok0981 wants to merge 1 commit into
saiashok0981 wants to merge 1 commit into
Conversation
Problem
-------
`pack build --env-file` reads env files by splitting on LF (\n) only.
When a developer creates an env file on Windows (CRLF line endings) and
the same file is consumed inside a Linux build container -- or when a
CI system checks out files with CRLF -- the trailing \r becomes part of
the variable key or value.
For value-less variables the key itself ends up with a \r suffix,
causing os.Getenv to silently return an empty string even when the
variable is correctly set in the host environment:
# env.file created on Windows (CRLF)
MY_SECRET\r <- resolved via os.Getenv("MY_SECRET\r") -> ""
Additionally, the parser did not skip comment lines (lines starting with
'#'), which is the widely accepted convention in .env files used by
Docker Compose, direnv, 12-factor tooling and other standard tooling.
Passing an env file with comments currently injects the comment text
as an environment variable name, silently corrupting the build
environment.
Fix
---
- Normalise \r\n -> \n before splitting, so that CRLF-encoded env files
are handled correctly on all platforms.
- Skip lines whose first non-whitespace character is '#'.
Both changes are additive: existing well-formed env files continue to
work unchanged.
Signed-off-by: [email protected]
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
pack build --env-filereads env files by splitting on LF (\n) only. When a developer creates an env file on Windows (CRLF line endings) and the same file is consumed inside a Linux build container -- or when a CI system checks out files with CRLF -- the trailing \r becomes part of the variable key or value.For value-less variables the key itself ends up with a \r suffix, causing os.Getenv to silently return an empty string even when the variable is correctly set in the host environment:
env.file created on Windows (CRLF)
MY_SECRET\r <- resolved via os.Getenv("MY_SECRET\r") -> ""
Additionally, the parser did not skip comment lines (lines starting with '#'), which is the widely accepted convention in .env files used by Docker Compose, direnv, 12-factor tooling and other standard tooling. Passing an env file with comments currently injects the comment text as an environment variable name, silently corrupting the build environment.
Fix
Both changes are additive: existing well-formed env files continue to work unchanged.
Signed-off-by: [email protected]
Summary
Output
Before
After
Documentation
Related
Resolves #___