Skip to content

fix: normalise CRLF line endings and skip comment lines in parseEnvFile#2635

Open
saiashok0981 wants to merge 1 commit into
buildpacks:mainfrom
saiashok0981:fix/env-file-crlf-line-endings
Open

fix: normalise CRLF line endings and skip comment lines in parseEnvFile#2635
saiashok0981 wants to merge 1 commit into
buildpacks:mainfrom
saiashok0981:fix/env-file-crlf-line-endings

Conversation

@saiashok0981

Copy link
Copy Markdown

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]

Summary

Output

Before

After

Documentation

  • Should this change be documented?
    • Yes, see #___
    • No

Related

Resolves #___

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]
@saiashok0981 saiashok0981 requested review from a team as code owners June 17, 2026 18:01
@github-actions github-actions Bot added this to the 0.41.0 milestone Jun 17, 2026
@github-actions github-actions Bot added the type/enhancement Issue that requests a new feature or improvement. label Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/enhancement Issue that requests a new feature or improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant