Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2eb6273

Browse filesBrowse files
Maël Nisonrichardlau
authored andcommitted
src: allows escaping NODE_OPTIONS with backslashes
The characters specified within NODE_OPTIONS can now be escaped, which is handy especially in conjunction with `--require` (where the file path might happen to contain spaces that shouldn't cause the option to be split into two). Fixes: #12971 PR-URL: #24065 Backport-PR-URL: #35342 Reviewed-By: Anna Henningsen <anna@addaleax.net> Refs: microsoft/vscode-js-debug#769
1 parent 3564424 commit 2eb6273
Copy full SHA for 2eb6273

File tree

Expand file treeCollapse file tree

4 files changed

+51
-12
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+51
-12
lines changed
Open diff view settings
Collapse file

‎doc/api/cli.md‎

Copy file name to clipboardExpand all lines: doc/api/cli.md
+7Lines changed: 7 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,13 @@ if they had been specified on the command line before the actual command line
642642
(so they can be overridden). Node.js will exit with an error if an option
643643
that is not allowed in the environment is used, such as `-p` or a script file.
644644

645+
In case an option value happens to contain a space (for example a path listed in
646+
`--require`), it must be escaped using double quotes. For example:
647+
648+
```bash
649+
--require "./my path/file.js"
650+
```
651+
645652
Node.js options that are allowed are:
646653
- `--enable-fips`
647654
- `--experimental-modules`
Collapse file

‎src/node.cc‎

Copy file name to clipboardExpand all lines: src/node.cc
+38-12Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,23 +2610,49 @@ void Init(std::vector<std::string>* argv,
26102610

26112611
#if !defined(NODE_WITHOUT_NODE_OPTIONS)
26122612
std::string node_options;
2613+
26132614
if (SafeGetenv("NODE_OPTIONS", &node_options)) {
26142615
std::vector<std::string> env_argv;
26152616
// [0] is expected to be the program name, fill it in from the real argv.
26162617
env_argv.push_back(argv->at(0));
26172618

2618-
// Split NODE_OPTIONS at each ' ' character.
2619-
std::string::size_type index = std::string::npos;
2620-
do {
2621-
std::string::size_type prev_index = index;
2622-
index = node_options.find(' ', index + 1);
2623-
if (index - prev_index == 1) continue;
2624-
2625-
const std::string option = node_options.substr(
2626-
prev_index + 1, index - prev_index - 1);
2627-
if (!option.empty())
2628-
env_argv.emplace_back(std::move(option));
2629-
} while (index != std::string::npos);
2619+
bool is_in_string = false;
2620+
bool will_start_new_arg = true;
2621+
for (std::string::size_type index = 0;
2622+
index < node_options.size();
2623+
++index) {
2624+
char c = node_options.at(index);
2625+
2626+
// Backslashes escape the following character
2627+
if (c == '\\' && is_in_string) {
2628+
if (index + 1 == node_options.size()) {
2629+
fprintf(stderr, "invalid value for NODE_OPTIONS "
2630+
"(invalid escape)\n");
2631+
exit(9);
2632+
} else {
2633+
c = node_options.at(++index);
2634+
}
2635+
} else if (c == ' ' && !is_in_string) {
2636+
will_start_new_arg = true;
2637+
continue;
2638+
} else if (c == '"') {
2639+
is_in_string = !is_in_string;
2640+
continue;
2641+
}
2642+
2643+
if (will_start_new_arg) {
2644+
env_argv.push_back(std::string(1, c));
2645+
will_start_new_arg = false;
2646+
} else {
2647+
env_argv.back() += c;
2648+
}
2649+
}
2650+
2651+
if (is_in_string) {
2652+
fprintf(stderr, "invalid value for NODE_OPTIONS "
2653+
"(unterminated string)\n");
2654+
exit(9);
2655+
}
26302656

26312657

26322658
ProcessArgv(&env_argv, nullptr, true);
Collapse file

‎test/fixtures/print A.js‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('A')
Collapse file

‎test/parallel/test-cli-node-options.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-cli-node-options.js
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ tmpdir.refresh();
1515
process.chdir(tmpdir.path);
1616

1717
const printA = require.resolve('../fixtures/printA.js');
18+
const printSpaceA = require.resolve('../fixtures/print A.js');
19+
20+
expect(` -r ${printA} `, 'A\nB\n');
1821
expect(`-r ${printA}`, 'A\nB\n');
22+
expect(`-r ${JSON.stringify(printA)}`, 'A\nB\n');
23+
expect(`-r ${JSON.stringify(printSpaceA)}`, 'A\nB\n');
1924
expect(`-r ${printA} -r ${printA}`, 'A\nB\n');
2025
expect('--no-deprecation', 'B\n');
2126
expect('--no-warnings', 'B\n');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.