Skip to content

Script argument --env-file is wrongly used as node option #54232

@xduugu

Description

@xduugu

Version

v22.5.1 / v20.16.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

A script argument named the same as the node option --env-file is detected as option by node.

According to node's --help output, node options should be specified before the script path (or the argument separator --):

Usage: node [options] [ script.js ] [arguments]

Executing a given test script works.js with content:

console.log("works");

works fine when called with a script argument which uses the same name as a node option, for example --help . The commands

node -e 'console.log("works");' -- --help
node works.js --help

correctly print

works

instead of node's --help output.

However, if the script argument is named --env-file, the commands

node -e 'console.log("works");' -- --env-file nonexistent-file
node works.js --env-file nonexistent-file

print this instead:

node: nonexistent-file: not found

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

Happens always.

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

Node should not try to read the env file and the test script should print

works

when passing a script argument --env-file.

What do you see instead?

Node tries to read the env file and, for example, returns the following for nonexistent files:

node: nonexistent-file: not found

Additional information

To me, it looks like that the function Dotenv::GetPathFromArgs is missing a break from the while loop when it encounters a non-option commandline argument (the script path) or the argument separator --.

Metadata

Metadata

Assignees

No one assigned

    Labels

    cliIssues and PRs related to the Node.js command line interface.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