Skip to content

Crash for copilot code block #60118

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

Closed
mjbvz opened this issue Oct 2, 2024 · 3 comments Β· Fixed by #60377
Closed

Crash for copilot code block #60118

mjbvz opened this issue Oct 2, 2024 · 3 comments Β· Fixed by #60377
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@mjbvz
Copy link
Contributor

mjbvz commented Oct 2, 2024

πŸ”Ž Search Terms

  • Copilot

πŸ•— Version & Regression Information

5.7.0-dev.20240927, not a regression

⏯ Playground Link

No response

πŸ’» Code

Using copilot in VS Code:

  1. Add this selection to panel chat https://github.com/microsoft/vscode/blob/d085816005ae61fc8f39b3720b3ec4594b35ecd0/src/vs/base/common/async.ts#L1145-L1278.
  2. Have this be your current selection https://github.com/microsoft/vscode/blob/d085816005ae61fc8f39b3720b3ec4594b35ecd0/src/vs/base/test/common/async.test.ts#L1523-L1554
  3. As @workspace /tests The last work call has a delay of 51 ms, but I already waited out the throttle delay why is this and how do I fix it.
  4. πŸ› The TS Server crashes

πŸ™ Actual behavior

As the result is streaming back, we get a crash:

Err 55338 [09:24:42.710] Exception on executing command {
  "seq": 18117,
  "type": "request",
  "command": "updateOpen",
  "arguments": {
    "changedFiles": [
      {
        "fileName": "^/vscode-chat-code-block/ts-nul-authority/Users/matb/projects/vscode/src/vs/base/test/common/async.test.ts",
        "textChanges": [
          {
            "newText": "",
            "start": {
              "line": 1503,
              "offset": 1
            },
            "end": {
              "line": 1503,
              "offset": 42
            }
          }
        ]
      }
    ],
    "closedFiles": [],
    "openFiles": []
  }
}:

    Cannot read properties of undefined (reading 'charCount')

    TypeError: Cannot read properties of undefined (reading 'charCount')
        at _LineNode.walk (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:194903:30)
        at _LineIndex.edit (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:194803:17)
        at _ScriptVersionCache._getSnapshot (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:194609:31)
        at _ScriptVersionCache.lineOffsetToPosition (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:194628:17)
        at TextStorage.lineOffsetToPosition (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:184011:22)
        at ScriptInfo.lineOffsetToPosition (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:184319:29)
        at /Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:191473:40
        at mapDefinedIterator (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:2602:19)
        at mapDefinedIterator.next (<anonymous>)
        at _ProjectService.applyChangesToFile (/Users/matb/projects/vscode/node_modules/typescript/lib/typescript.js:190256:16)

The crash seems to happen during response streaming. During this, code blocks may be updated multiple times and the code in them may not be complete

πŸ™‚ Expected behavior

No crash

Additional information about the issue

No response

@sandersn
Copy link
Member

This doesn't repro for me on VS Code 1.94, with Typescript built from main and Copilot Chat 0.21.2 with GPT 4o as the model. However, the first response "matched public code so it was blocked". The second response succeeded and didn't crash. o1-preview blocked both a first and second response, but not before streaming considerable amounts of code, also without crashing.

  1. What versions of VS Code and Copilot Chat are you using?
  2. What model did you use?
  3. Do you have public code blocking disabled?

@sandersn
Copy link
Member

Looking at the code, it can definitely crash if the children array of the LineNode is empty, which it is upon construction. The crashing line is
let childCharCount = this.children[childIndex].charCount() and childIndex starts at 0.

@mjbvz
Copy link
Contributor Author

mjbvz commented Oct 29, 2024

@sandersn I can't reproduce this consistently. I suspect that during streaming of responses, the document ends up in some weird incomplete state that causes this. However even asking the same question again may not trigger this depending on how the code block is streamed to TS

sandersn added a commit that referenced this issue Oct 30, 2024
Fix a crash in an unbacked buffer that copilot is writing to.

LineNode.walk reads the first element out of LineNode.children without
checking that there are any elements to read. Of course, it's supposed
to be called only on nodes that have children.

Why no tests?
-------------

- This failure is flaky -- I never managed to reproduce it.
- scriptVersionCache is barely maintained. The last two fixes in this
  particular code are 3.5 and 6.5 years ago.
- #21924 has no tests to fix an assert
- af47ddbc3b0c6421c38b18e56f8da88434a473c8d has an exasperated comment
  about the difficulty of learning invariants and an apology for an
  only-possibly-relevant test.
- So, I didn't think the effort of a complete fix was worthwhile in a case where a
  small, local fix will stop the crashes.
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants