Skip to content

Commit 9e31e9d

Browse files
committed
fix handling of bareword args matching node flags; closes #3761
1 parent 6535965 commit 9e31e9d

4 files changed

Lines changed: 58 additions & 10 deletions

File tree

‎lib/cli/node-flags.js‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,32 @@ const debugFlags = new Set(['debug', 'debug-brk', 'inspect', 'inspect-brk']);
2929
* - `--v8-*` (but *not* `--v8-options`)
3030
* @summary Whether or not to pass a flag along to the `node` executable.
3131
* @param {string} flag - Flag to test
32-
* @returns {boolean}
32+
* @param {boolean} [bareword=true] - If `false`, we expect `flag` to have one or two leading dashes.
33+
* @returns {boolean} If the flag is considered a "Node" flag.
3334
* @private
3435
*/
35-
exports.isNodeFlag = flag =>
36-
!/^(?:require|r)$/.test(flag) &&
37-
(nodeFlags.has(flag) ||
38-
debugFlags.has(flag) ||
39-
/(?:preserve-symlinks(?:-main)?|harmony(?:[_-]|$)|(?:trace[_-].+$)|gc(?:[_-]global)?$|es[_-]staging$|use[_-]strict$|v8[_-](?!options).+?$)/.test(
40-
flag
41-
));
36+
exports.isNodeFlag = (flag, bareword = true) => {
37+
if (!bareword) {
38+
// check if the flag begins with dashes; if not, not a node flag.
39+
if (!/^--?/.test(flag)) {
40+
return false;
41+
}
42+
// strip the leading dashes to match against subsequent checks
43+
flag = flag.replace(/^--?/, '');
44+
}
45+
return (
46+
// treat --require/-r as Mocha flag even though it's also a node flag
47+
!(flag === 'require' || flag === 'r') &&
48+
// check actual node flags from `process.allowedNodeEnvironmentFlags`,
49+
// then historical support for various V8 and non-`NODE_OPTIONS` flags
50+
// and also any V8 flags with `--v8-` prefix
51+
(nodeFlags.has(flag) ||
52+
debugFlags.has(flag) ||
53+
/(?:preserve-symlinks(?:-main)?|harmony(?:[_-]|$)|(?:trace[_-].+$)|gc(?:[_-]global)?$|es[_-]staging$|use[_-]strict$|v8[_-](?!options).+?$)/.test(
54+
flag
55+
))
56+
);
57+
};
4258

4359
/**
4460
* Returns `true` if the flag is a "debug-like" flag. These require timeouts

‎lib/cli/options.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ const parse = (args = [], ...configObjects) => {
8585
const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce(
8686
(acc, arg) => {
8787
const pair = arg.split('=');
88-
const flag = pair[0].replace(/^--?/, '');
89-
if (isNodeFlag(flag)) {
88+
let flag = pair[0];
89+
if (isNodeFlag(flag, false)) {
90+
flag = flag.replace(/^--?/, '');
9091
return arg.includes('=')
9192
? acc.concat([[flag, pair[1]]])
9293
: acc.concat([[flag, true]]);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var invokeMocha = require('../helpers').invokeMocha;
4+
5+
describe('node flags', function() {
6+
it('should not consider argument values to be node flags', function(done) {
7+
invokeMocha(
8+
['--require', 'trace-dependency'],
9+
function(err, res) {
10+
if (err) {
11+
return done(err);
12+
}
13+
expect(res, 'not to have failed with output', /bad option/i);
14+
done();
15+
},
16+
'pipe'
17+
);
18+
});
19+
});

‎test/node-unit/cli/node-flags.spec.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ describe('node-flags', function() {
1515
});
1616
});
1717

18+
describe('when expecting leading dashes', function() {
19+
it('should require leading dashes', function() {
20+
expect(isNodeFlag('throw-deprecation', false), 'to be false');
21+
expect(isNodeFlag('--throw-deprecation', false), 'to be true');
22+
});
23+
24+
it('should return false for --require/-r', function() {
25+
expect(isNodeFlag('--require', false), 'to be false');
26+
expect(isNodeFlag('-r', false), 'to be false');
27+
});
28+
});
29+
1830
describe('special cases', function() {
1931
it('should return true for flags starting with "preserve-symlinks"', function() {
2032
expect(isNodeFlag('preserve-symlinks'), 'to be true');

0 commit comments

Comments
 (0)