Skip to content

Comment lines which are environment variable assignments in .env file are not ignored when using --env-file #52466

@Simon-Prieul

Description

@Simon-Prieul

Version

v20.12.2

Platform

Linux arthur 6.8.0-76060800daily20240311-generic #202403110203171139393022.04~331756a SMP PREEMPT_DYNAMIC Mon M x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

I found a regression starting in Node v20.12.0 for the parsing of .env files with the CLI option --env-file
The comment lines (starting with #) should be ignored, which seem to no longer be the case

With the following index.js :

console.log(`Starting with node version ${process.version}`)
const vars = Object.entries(process.env).filter(([evName]) => {
  return evName.startsWith("FOO_")
})
console.log(vars)
console.log("count: " + vars.length)

and the following .env file :

# FOO_ONE=
FOO_TWO=fooTwoVal
FOO_AAA=

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior? Why is that the expected behavior?

When running the command node --env-file .env index.js
The expected behavior is the output should be the same that when running with Node v20.11.1 :

[ [ 'FOO_AAA', '' ], [ 'FOO_TWO', 'fooTwoVal' ] ]
count: 2

Indeed, in the .env file, the FOO_ONE variable was commented, and should not be set

What do you see instead?

When running the command node --env-file .env index.js
The ouptut is :

[ [ 'FOO_AAA', '' ], [ 'FOO_ONE', '' ], [ 'FOO_TWO', 'fooTwoVal' ] ]
count: 3

You can see that FOO_ONE was set to an empty string whereas it was not set in the .env file (the line # FOO_ONE= is present but it should be ignored as it is a comment line)

Additional information

Note that the bug seems to be present in Node v21.7.3 too

Metadata

Metadata

Assignees

No one assigned

    Labels

    dotenvIssues and PRs related to .env file parsing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions