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

Unexpected error for argument value "----" #1869

Closed
ericcornelissen opened this issue Feb 18, 2021 · 1 comment · Fixed by yargs/yargs-parser#354
Closed

Unexpected error for argument value "----" #1869

ericcornelissen opened this issue Feb 18, 2021 · 1 comment · Fixed by yargs/yargs-parser#354
Labels

Comments

@ericcornelissen
Copy link

ericcornelissen commented Feb 18, 2021

On yargs@16.2.0 I discovered a bug causing the application to crash unexpectedly if one of the values in the arguments array provided to the yargs function is exactly ---- (i.e., 4 hyphens). As far as I can tell any other number of hyphens works fine, I did not test any other version of yargs. The error produced by this input can be found below and it is thrown (if I'm not mistaken) by this snippet of code:

yargs/lib/yargs-factory.ts

Lines 1757 to 1760 in 31765cb

self._copyDoubleDash = function (argv: Arguments): any {
if (!argv._ || !argv['--']) return argv;
// eslint-disable-next-line prefer-spread
argv._.push.apply(argv._, argv['--']);

I'm not exactly sure what goes wrong but it seems to me that argv['--'] is internally used for something, and by using the argument value ---- this value is changed unexpectedly resulting in the error.

.../node_modules/yargs/build/index.cjs:2794
        argv._.push.apply(argv._, argv['--']);
                    ^
TypeError: CreateListFromArrayLike called on non-object
    at Object.Yargs.self._copyDoubleDash (.../node_modules/yargs/build/index.cjs:2794:21)
    at Object.Yargs.self._postProcess (.../node_modules/yargs/build/index.cjs:2782:25)
    at Object.parseArgs [as _parseArgs] (.../node_modules/yargs/build/index.cjs:2774:21)
    at Object.get [as argv] (.../node_modules/yargs/build/index.cjs:2651:25)
    at Object.<anonymous> (.../src/index.ts:41:5)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Module.m._compile (/home/eric/.nvm/versions/node/v12.16.0/lib/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Object.require.extensions.<computed> [as .ts] (/home/eric/.nvm/versions/node/v12.16.0/lib/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:1001:32)

EDIT: it seems that appending whitespace characters (e.g. \r or \n) or = and than other non-space character to ---- will still result in the same error.

@ericcornelissen
Copy link
Author

Thanks for the timely fix @bcoe 😄

One minor thing: it seems the issue persists if the ---- is followed by a \n or \r, likely due to the details the regular expression you used to fix the initial issue. I'm not sure if you consider this a problem. But if you want to fix it, assuming args in that snippet is a trimmed string, the regular expression /---+(\s+|=|$)/ should prevent the issue from showing up due to \n or \r (or other whitespace characters) as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants