Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xduugu opened this issue Aug 6, 2024 · 1 comment · Fixed by #54237
Closed

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

xduugu opened this issue Aug 6, 2024 · 1 comment · Fixed by #54237
Labels
cli Issues and PRs related to the Node.js command line interface. dotenv Issues and PRs related to .env file parsing

Comments

@xduugu
Copy link
Contributor

xduugu commented Aug 6, 2024

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 --.

@RedYetiDev RedYetiDev added repro-exists cli Issues and PRs related to the Node.js command line interface. dotenv Issues and PRs related to .env file parsing labels Aug 6, 2024
@RedYetiDev
Copy link
Member

Able to reproduce on (Kali) linux:

$ node -- --env-file nonexistent-file
node: nonexistent-file: not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Issues and PRs related to the Node.js command line interface. dotenv Issues and PRs related to .env file parsing
Projects
None yet
2 participants