TypeScript: "Cannot read property 'lastIndexOf' of undefined" error in tsserver.js

Bug Report

πŸ”Ž Search Terms

  • lastIndexOf

πŸ•— Version & Regression Information

  • This is a crash in tsserver.js
  • This happens in 4.2.2, 4.2.3 and next (4.3.0-dev.20210305), but not version 4.1.5
  • node version v14.15.4
  • windows version 20H2

πŸ’» Steps to reproduce

This happens consistently for me in the OmniSharp/omnisharp-vscode repository, when opening a typescript file with 4.2 installed.

  1. git clone https://github.com/OmniSharp/omnisharp-vscode.git
  2. cd omnisharp-vscode
  3. npm install
  4. npm install -g typescript@4.2.2
  5. code .
  6. Configure vscode to use the global ts server (for me this is "typescript.tsdk": "C:\\Users\\phil\\AppData\\Roaming\\npm\\node_modules\\typescript\\lib",)
  7. Restart code
  8. Open the TypeScript output to view errors

πŸ™ Actual behavior

The TypeScript language server crashes. Attached is a sample log: tsserver.zip

πŸ™‚ Expected behavior

It to not crash.

Related Issues

There’s a couple of related issues in vscode repo such as microsoft/vscode#115905

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 2
  • Comments: 28 (22 by maintainers)

Most upvoted comments

@amcasey I am experiencing this issue on 4.4.3, MacOS 11.5. From logging, seems to be caused by this bit of our tsconfig:

"typeRoots": ["./typings", "./node_modules/@types"]

which I understand should be legit?

This causes guessDirectorySymlink to be called with /[CWD]/typings/index.d.ts, /[CWD]/node_modules/@types/../../typings/index.d.ts leading to the same paths to be compared (/[CWD]/typings/index.d.ts) in the while loop until Cannot read property 'lastIndexOf' of undefined

Update: git bisect suggests the issue was introduced in 4.4.0-dev.20210717

Just a quick update on scheduling: last week we agreed to push this to 4.3 since telemetry shows that a limited number of machines are hitting this.

If you are impacted by this bug, once this issue is closed you should be able to install JavaScript and TypeScript nightly extension to pick up a fix instead of waiting on VS Code to pick up TS 4.3

Looks like 175K telemetry hits in the last day. (!!)

Edit: but they’re from only 246 distinct machines.

Confirmed that converting:

-            const realpathSync = _fs.realpathSync.native ?? _fs.realpathSync;
+           const realpathSync = _fs.realpathSync;

Which effectively undoes #41292 but does fix my repro, now to try figure out what triggers the difference in behavior

@orta @RyanCavanaugh We’ve seen a few reports of they on the VS Code side. Is this something that we could look into for a TS 4.2 recovery build if the fix isn’t too risky?