Skip to content

Commit 5dbff19

Browse files
authored
Handle undefined result from executeDocumentSymbolProvider (#39)
When resolving test symbols in a document, it's possible that there are situations where the call to get document symbols will return undefined (mainly if language server is still activating). This was resulting in an uncaught exception that causes the loading to appear "stuck" on the UI. This adds correct handling if this result is undefined.
1 parent 46f98db commit 5dbff19

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/language-tools/python.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class PythonLanguageTools
9797
}
9898

9999
// Document symbols provided by Pylance.
100-
const symbols: vscode.DocumentSymbol[] =
100+
const symbols: vscode.DocumentSymbol[] | undefined =
101101
await vscode.commands.executeCommand(
102102
'vscode.executeDocumentSymbolProvider',
103103
document
@@ -161,8 +161,10 @@ export class PythonLanguageTools
161161
}
162162

163163
// Start at top level and evaluate each symbol in the document.
164-
for (const symbol of symbols) {
165-
evaluateCurrentSymbol(symbol)
164+
if (symbols) {
165+
for (const symbol of symbols) {
166+
evaluateCurrentSymbol(symbol)
167+
}
166168
}
167169

168170
const finalTestCases = result.filter(item => {

src/test/suite/language-tools/python.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ suite('Python Language Tools', () => {
7878
assert.equal(result.documentTest?.name, 'my_test.py')
7979
})
8080

81+
test('undefined symbols', async () => {
82+
executeCommandStub.resolves(undefined)
83+
const result = await languageTools.getDocumentTestCases(
84+
vscode.Uri.parse('file:///repo/root/sample/my_test.py'),
85+
'/repo/root/'
86+
)
87+
assert.strictEqual(result.isTestFile, true)
88+
assert.strictEqual(result.testCases.length, 0)
89+
})
90+
8191
test('non test file', async () => {
8292
const result = await languageTools.getDocumentTestCases(
8393
vscode.Uri.parse('file:///repo/root/sample/my_file.py'),

0 commit comments

Comments
 (0)