GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — watch: escape quotes and backslashes in NODE_OPTIONS · nodejs/node@41e9ab9

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/main
    • watch_mode.js
  • test/sequential
    • test-watch-mode.mjs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/main/watch_mode.js‎

Copy file name to clipboardExpand all lines: lib/internal/main/watch_mode.js
+14-4Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
ArrayPrototypePushApply,
88
ArrayPrototypeSlice,
99
StringPrototypeIncludes,
10+
StringPrototypeReplaceAll,
1011
StringPrototypeStartsWith,
1112
} = primordials;
1213

@@ -101,11 +102,20 @@ if (kNodeOptions != null) {
101102
i++;
102103
continue;
103104
}
104-
// The C++ tokenizer strips quotes during parsing, so values that
105-
// originally contained spaces (e.g. --require "./path with spaces/f.js")
105+
// The C++ tokenizer strips quotes and escapes during parsing, so values
106+
// that contain spaces or double quotes (e.g. --require "./a b/f.js")
106107
// need to be re-quoted before rejoining into a single string, otherwise
107-
// the child's C++ parser would split them into separate tokens.
108-
ArrayPrototypePush(keep, StringPrototypeIncludes(part, ' ') ? `"${part}"` : part);
108+
// the child's C++ parser would split or misparse them. Inside quotes,
109+
// backslashes are escape characters, so `"` and `\` must be escaped.
110+
// Backslashes are escaped first so the ones added for `"` aren't doubled.
111+
// Outside quotes, backslashes are literal, so other values are kept as-is.
112+
if (StringPrototypeIncludes(part, ' ') || StringPrototypeIncludes(part, '"')) {
113+
const escaped = StringPrototypeReplaceAll(
114+
StringPrototypeReplaceAll(part, '\\', '\\\\'), '"', '\\"');
115+
ArrayPrototypePush(keep, `"${escaped}"`);
116+
} else {
117+
ArrayPrototypePush(keep, part);
118+
}
109119
}
110120
cleanNodeOptions = ArrayPrototypeJoin(keep, ' ');
111121
}
Collapse file

‎test/sequential/test-watch-mode.mjs‎

Copy file name to clipboardExpand all lines: test/sequential/test-watch-mode.mjs
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,35 @@ process.on('message', (message) => {
10641064
}
10651065
});
10661066

1067+
for (const { name, nodeOptions, expected } of [
1068+
{ name: 'a double quote', nodeOptions: '--title="a\\"b"', expected: 'a"b' },
1069+
{ name: 'a backslash', nodeOptions: '--title="a \\\\b"', expected: 'a \\b' },
1070+
]) {
1071+
it(`should preserve NODE_OPTIONS values containing ${name} in child process`, {
1072+
// Honoring --title from NODE_OPTIONS is required for this test.
1073+
// process.title is always an empty string on SunOS, so --title
1074+
// cannot be observed there.
1075+
skip: !!process.config.variables.node_without_node_options || common.isSunOS,
1076+
}, async () => {
1077+
const file = createTmpFile('console.log(JSON.stringify(process.title));');
1078+
const { done, restart } = runInBackground({
1079+
args: ['--watch', file],
1080+
options: {
1081+
env: { ...process.env, NODE_OPTIONS: `--watch ${nodeOptions}` },
1082+
},
1083+
});
1084+
1085+
try {
1086+
const { stdout, stderr } = await restart();
1087+
1088+
assert.strictEqual(stderr, '');
1089+
assert.ok(stdout.includes(JSON.stringify(expected)), stdout.join('\n'));
1090+
} finally {
1091+
await done();
1092+
}
1093+
});
1094+
}
1095+
10671096
it('should handle NODE_OPTIONS containing only watch flags', async () => {
10681097
const file = createTmpFile('console.log(JSON.stringify(process.env.NODE_OPTIONS));');
10691098
const { done, restart } = runInBackground({

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.