Typescript: tsserver: TypeError: Cannot read property 'length' of undefined

Created on 11 Sep 2017  路  20Comments  路  Source: microsoft/TypeScript

TypeScript Version: 2.6.0-dev.20170910

Code

          <Stager
            ref={(_stager)=> this._stager=_stager } // << here
            onChange={this.setTitle}
            >

Expected behavior:

No error

Actual behavior:

Erroring with:

TypeError: Cannot read property 'length' of undefined
    at Object.unescapeLeadingUnderscores (C:\nodejs\node_modules\typescript\lib\tsserver.js:8627:18)
    at Object.getSuggestionForNonexistentSymbol (C:\nodejs\node_modules\typescript\lib\tsserver.js:19848:95)
    at Object.getActionsForCorrectSpelling [as getCodeActions] (C:\nodejs\node_modules\typescript\lib\tsserver.js:73600:38)
    at C:\nodejs\node_modules\typescript\lib\tsserver.js:73316:33
    at Object.forEach (C:\nodejs\node_modules\typescript\lib\tsserver.js:1299:30)
    at Object.getFixes (C:\nodejs\node_modules\typescript\lib\tsserver.js:73315:16)
    at C:\nodejs\node_modules\typescript\lib\tsserver.js:77197:35
    at Object.flatMap (C:\nodejs\node_modules\typescript\lib\tsserver.js:1531:25)
    at Object.getCodeFixesAtPosition (C:\nodejs\node_modules\typescript\lib\tsserver.js:77195:23)
    at IOSession.Session.getCodeFixes (C:\nodejs\node_modules\typescript\lib\tsserver.js:83234:64)
    at Session.handlers.ts.createMapFromTemplate._a.(anonymous function) (C:\nodejs\node_modules\typescript\lib\tsserver.js:82152:61)
    at C:\nodejs\node_modules\typescript\lib\tsserver.js:83378:88
    at IOSession.Session.executeWithRequestId (C:\nodejs\node_modules\typescript\lib\tsserver.js:83369:28)
    at IOSession.Session.executeCommand (C:\nodejs\node_modules\typescript\lib\tsserver.js:83378:33)
    at IOSession.Session.onMessage (C:\nodejs\node_modules\typescript\lib\tsserver.js:83398:35)
    at Interface.<anonymous> (C:\nodejs\node_modules\typescript\lib\tsserver.js:84529:27)
    at emitOne (events.js:96:13)
    at Interface.emit (events.js:191:7)
    at Interface._onLine (readline.js:241:10)
    at Interface._normalWrite (readline.js:384:12)
    at Socket.ondata (readline.js:101:10)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at Socket.Readable.push (_stream_readable.js:136:10)
    at Pipe.onread (net.js:560:20)

cross posting with vscode Microsoft/vscode#34123

Bug Fixed

Most helpful comment

I'm not getting these anymore @andy-ms

All 20 comments

Same stack trace as #17460.

I think is better only report an issue in one project. If needed, they will move the issue to the correct project.

I also came across the issue outside of vscode, on webpack with awesome-typescript-loader.
Please note that I used TS 2.5.2.

My call-stack actually differs:

TypeError: Cannot read property 'length' of undefined
    at Object.unescapeLeadingUnderscores (.\node_modules\typescript\lib\typescript.js:10513:18)
    at getTextOfNode (.\node_modules\typescript\lib\typescript.js:68969:27)
    at emitIdentifier (.\node_modules\typescript\lib\typescript.js:67308:19)
    at pipelineEmitUnspecified (.\node_modules\typescript\lib\typescript.js:66971:28)
    at pipelineEmitWithHint (.\node_modules\typescript\lib\typescript.js:66943:50)
    at emitNodeWithSourceMap (.\node_modules\typescript\lib\typescript.js:64360:21)
    at pipelineEmitWithSourceMap (.\node_modules\typescript\lib\typescript.js:66932:17)
    at emitNodeWithNestedComments (.\node_modules\typescript\lib\typescript.js:64649:17)
    at emitNodeWithSynthesizedComments (.\node_modules\typescript\lib\typescript.js:64599:13)
    at emitNodeWithComments (.\node_modules\typescript\lib\typescript.js:64535:21)
    at pipelineEmitWithComments (.\node_modules\typescript\lib\typescript.js:66924:17)
    at emitNodeWithNotification (.\node_modules\typescript\lib\typescript.js:63983:21)
    at pipelineEmitWithNotification (.\node_modules\typescript\lib\typescript.js:66915:17)
    at emit (.\node_modules\typescript\lib\typescript.js:66905:13)
    at emitPropertyAccessExpression (.\node_modules\typescript\lib\typescript.js:67612:13)
    at pipelineEmitExpression (.\node_modules\typescript\lib\typescript.js:67212:28)
    at pipelineEmitWithHint (.\node_modules\typescript\lib\typescript.js:66942:49)
    at emitNodeWithSourceMap (.\node_modules\typescript\lib\typescript.js:64360:21)
    at pipelineEmitWithSourceMap (.\node_modules\typescript\lib\typescript.js:66932:17)
    at emitNodeWithNestedComments (.\node_modules\typescript\lib\typescript.js:64649:17)
    at emitNodeWithSynthesizedComments (.\node_modules\typescript\lib\typescript.js:64599:13)
    at emitNodeWithComments (.\node_modules\typescript\lib\typescript.js:64535:21)
    at pipelineEmitWithComments (.\node_modules\typescript\lib\typescript.js:66924:17)
    at emitNodeWithNotification (.\node_modules\typescript\lib\typescript.js:63983:21)
    at pipelineEmitWithNotification (.\node_modules\typescript\lib\typescript.js:66915:17)
    at emitExpression (.\node_modules\typescript\lib\typescript.js:66911:13)
    at emitCallExpression (.\node_modules\typescript\lib\typescript.js:67641:13)
    at pipelineEmitExpression (.\node_modules\typescript\lib\typescript.js:67216:28)
    at pipelineEmitWithHint (.\node_modules\typescript\lib\typescript.js:66942:49)
    at emitNodeWithSourceMap (.\node_modules\typescript\lib\typescript.js:64360:21)

@pocesar @j-oliveras @Igorbek Does anyone have a complete example that reproduces the issue? Codefix relies on type information so we need to know the context the expression is in to find out what went wrong.

It started to happen in many places on a big project when I tried to update it to TS 2.5. I'll try to narrow down it to a single repro.

the issue arose from writing in vscode @andy-ms, when I was mid way typing this._something inside a React Native project class, having tsc -w on, and before setting the property on the class itself (I also have noImplicityAny and strict set to true)

In my case it is reproducible in presence of a custom transformer. I use my own typescript-plugin-styled-components which has very simple implementation.
I tried to compile and test it with TS2.5 and all tests did pass successfully. I keep digging to find a smaller repro.

@andy-ms I'm having the same issue in my TS transformer (was working on 2.4): https://github.com/longlho/ts-transform-css-modules/tree/dev (very small repo, you can just npm i & npm test to see the issue)

I think the issue happens when I'm trying to transform an import node to another node. The node supposedly got transformed, but for some reason is still being traversed

@longlho Enable "noImplicitAny": true, in your tsconfig.json and it will point you straight to the error -- you assigned to escaptedText instead of escapedText, so when we try to access the variable's name via escapedText, it's undefined. You also might want to use ts.createVariableDeclaration instead of ts.createNode for extra safety to ensure you assign all properties.

Thanks I'll give that a try

I my case was not able to reproduce that exact error but found a case when TS ends up with fatal error - out of memory.

Reproduction:

  • clone https://github.com/Igorbek/typescript-plugin-styled-components
  • yarn install
  • yarn runner ./src/__tests__/baselines/sample3.tsx (is reproduced only with that case)
typescript-plugin-styled-components> yarn runner ./src/__tests__/baselines/sample3.tsx
yarn runner v0.27.5
$ ts-node ./src/runner.ts "./src/__tests__/baselines/sample3.tsx"

<--- Last few GCs --->

[10640:000001882AE8A3F0]    19478 ms: Mark-sweep 1409.8 (1462.2) -> 1409.8 (1446.2) MB, 826.5 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since
 start of marking 827 ms) last resort
[10640:000001882AE8A3F0]    20324 ms: Mark-sweep 1409.8 (1446.2) -> 1409.8 (1446.2) MB, 846.0 / 0.0 ms  last resort


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 000002DBC20A8799 <JSObject>
    1: set [native collection.js:~247] [pc=000003DDAD51E7D7](this=0000025D1B1CFF81 <Map map = 000001EE28584E01>,p=000003A2CBC1C5C9 <String[10]: onMouseOut>,x=000003D9699E2C19 <SymbolOb
ject map = 000001B592B9AD51>)
    2: resolveMappedTypeMembers(aka resolveMappedTypeMembers) [.\typescript-plugin-styled-components\node_modules\typescript\lib\typescript.js:~27791] [pc=000003DD..
.

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
error Command failed with exit code 3.

@sandersn might be able to help with that.

@Igorbek I tried your out-of-memory case with 2.4.2 and typescript@next. It runs out of memory with 2.4.2 but not with typescript@next. Can you try that instead?

@sandersn thank you. Yes, it does fail on both 2.4.2 and 2.5.2. And works correctly on @next.
I'll keep trying to reproduce the original issue on real project where I originally found it, and reduce to something that can be shared.

ah, after hours of trying to catch it, I realized I had inconsistency in TS versions in the project. Somehow I had 2 versions resolved at the same time (yarn, why??) - obviously my transformer used older version and a-t-loader used newer. Now it works for me. @pocesar check, maybe the same case for you

isn't my case @igorbek, i'm using gulp to spawn globally installed tsc command line script

still happening on nightly 20170929

[Error  - 01:42:26] 'getCodeFixes' request failed with error.
Error processing request. Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at Object.unescapeLeadingUnderscores (C:\nodejs\node_modules\typescript\lib\tsserver.js:8643:18)
    at Object.getSuggestionForNonexistentSymbol (C:\nodejs\node_modules\typescript\lib\tsserver.js:19952:95)
    at Object.getActionsForCorrectSpelling [as getCodeActions] (C:\nodejs\node_modules\typescript\lib\tsserver.js:73870:38)
    at C:\nodejs\node_modules\typescript\lib\tsserver.js:73582:33
    at Object.forEach (C:\nodejs\node_modules\typescript\lib\tsserver.js:1300:30)
    at Object.getFixes (C:\nodejs\node_modules\typescript\lib\tsserver.js:73581:16)
    at C:\nodejs\node_modules\typescript\lib\tsserver.js:77694:35
    at Object.flatMap (C:\nodejs\node_modules\typescript\lib\tsserver.js:1532:25)
    at Object.getCodeFixesAtPosition (C:\nodejs\node_modules\typescript\lib\tsserver.js:77692:23)
    at IOSession.Session.getCodeFixes (C:\nodejs\node_modules\typescript\lib\tsserver.js:83750:64)
    at Session.handlers.ts.createMapFromTemplate._a.(anonymous function) (C:\nodejs\node_modules\typescript\lib\tsserver.js:82668:61)
    at C:\nodejs\node_modules\typescript\lib\tsserver.js:83895:88
    at IOSession.Session.executeWithRequestId (C:\nodejs\node_modules\typescript\lib\tsserver.js:83886:28)
    at IOSession.Session.executeCommand (C:\nodejs\node_modules\typescript\lib\tsserver.js:83895:33)
    at IOSession.Session.onMessage (C:\nodejs\node_modules\typescript\lib\tsserver.js:83915:35)
    at Interface.<anonymous> (C:\nodejs\node_modules\typescript\lib\tsserver.js:85062:27)
    at emitOne (events.js:96:13)
    at Interface.emit (events.js:191:7)
    at Interface._onLine (readline.js:241:10)
    at Interface._normalWrite (readline.js:384:12)
    at Socket.ondata (readline.js:101:10)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:191:7)
    at readableAddChunk (_stream_readable.js:178:18)
    at Socket.Readable.push (_stream_readable.js:136:10)
    at Pipe.onread (net.js:560:20)

@pocesar Can you provide some example code that reproduces the error?
Also, may have been accidentally fixed by #18793 since the line that stacktrace is failing on no longer calls unescapeLeadingUnderscores.

I'm not getting these anymore @andy-ms

Confirming, updating to TypeScript version 2.6.1 fixed. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbondc picture jbondc  路  3Comments

dlaberge picture dlaberge  路  3Comments

uber5001 picture uber5001  路  3Comments

seanzer picture seanzer  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments