react-doctor icon indicating copy to clipboard operation
react-doctor copied to clipboard

Cannot read properties of undefined (reading 'length')

Open PunGrumpy opened this issue 6 months ago • 7 comments

Bug Description

$ npx react-doctor@latest --verbose --diff

Something went wrong. Please check the error below for more details.
If the problem persists, please open an issue at https://github.com/millionco/react-doctor/issues.

Cannot read properties of undefined (reading 'length')

Environment

  • react-doctor version: latest (0.2.4)
  • Node.js version: v26.0.0

PunGrumpy avatar May 24 '26 10:05 PunGrumpy

Got this too

hernancasillas avatar May 24 '26 16:05 hernancasillas

fixing - thank you

aidenybai avatar May 24 '26 23:05 aidenybai

Reproducing this as well on 0.2.4. Adding root cause analysis below in case it's helpful.

Workaround

Use --diff=<base> (inline value) instead of --diff with no argument — this avoids the buggy code path.


[!NOTE] The analysis below is AI generated.

Root Cause Analysis

The crash is in stripUnknownCliFlags → stripUnknownFlags, specifically in shouldConsumeNextArgument (cli.js:14780).

The bug: When --diff is the last CLI argument (no value after it), shouldConsumeNextArgument incorrectly returns true:

// Line 14779 — required values: correctly guards against undefined
if (flagSpec.longOptionsWithRequiredValues.has(optionName)) return nextArgument !== void 0;

// Line 14780 — optional values: missing the same guard!
return flagSpec.longOptionsWithOptionalValues.has(optionName) && !isFlagLike(nextArgument ?? "");

When nextArgument is undefined (nothing after --diff), the ?? coalesces it to "", isFlagLike("") returns false, so the function returns true — "consume the next argument." stripUnknownFlags then pushes userArguments[outOfBounds] (undefined) into the sanitized args array. Commander's parseOptions subsequently calls maybeOption(undefined), which crashes on undefined.length.

Suggested fix (line 14780):

return flagSpec.longOptionsWithOptionalValues.has(optionName)
    && nextArgument !== undefined
    && !isFlagLike(nextArgument);

schanchani-sumer avatar May 25 '26 05:05 schanchani-sumer

pushed on latest - please let me know if you are still experiencing this issue!

aidenybai avatar May 25 '26 07:05 aidenybai

It now works normally for npx react-doctor@latest --verbose --diff.

PunGrumpy avatar May 25 '26 17:05 PunGrumpy

@rayhanadev

NisargIO avatar May 27 '26 02:05 NisargIO

going to close this issue since it seems it was fixed in the latest release 🫡

rayhanadev avatar May 27 '26 08:05 rayhanadev