node icon indicating copy to clipboard operation
node copied to clipboard

Expose `process.env.NODE_OPTIONS` parser to user land

Open Ethan-Arrowood opened this issue 10 months ago • 2 comments

What is the problem this feature will solve?

Node.js has an internal parser for correctly parsing and validating the process.env.NODE_OPTIONS variable. I'd like to expose this parser to user land as something like util.parseNodeOptions.

What is the feature you are proposing to solve the problem?

The exact parser code is here: https://github.com/nodejs/node/blob/c29d53c5cfc63c5a876084e788d70c9e87bed880/src/node_options.cc#L1401

The solution should create a new C++ function that accesses the NODE_OPTIONS environment variable from the Environment so that we do not incur an unnecessary serialization cost passing the NODE_OPTIONS from JS.

Calling the function should return the list of strings.

The function should throw any error it encounters during the parsing process.

I will contribute this feature myself soon 👍

What alternatives have you considered?

We required this functionality for a recent bug in Next.js. I was able to solve that issue by manually converting the C++ parser to JS and using that.

The PR introducing the fix: https://github.com/vercel/next.js/pull/65046

The JS parser implementation itself: https://github.com/vercel/next.js/blob/270a9db0567808db5d53e7a18c7be0a4710796e8/packages/next/src/server/lib/utils.ts#L59-L113

Ethan-Arrowood avatar Apr 26 '24 16:04 Ethan-Arrowood

Note that ParseNodeOptionsEnvVar only splits the argument string into a vector of strings by whitespaces. It's not a full parser (i.e. doesn't validate and assign values).

joyeecheung avatar Apr 29 '24 14:04 joyeecheung

That is okay. I think providing even the consistent whitespace/quote parsing is important. The core of the issue we ran into in Next.js was because we didn't understand the parsing rules for process.env.NODE_OPTIONS.

I'd have to go and look at the code again, but there was something else that was validating the parsed options. That could be valuable to export as well.

Ethan-Arrowood avatar Apr 29 '24 15:04 Ethan-Arrowood