From 5ce700e74eb18fa1e04ae91c31ecadb47795f15f Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Mon, 5 Aug 2019 13:27:16 -0700 Subject: [PATCH] Prevent crashes when the AST happens to be null --- .../Impl/Indexing/MostRecentDocumentSymbols.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs index 2096e26fc..fe8b6969b 100644 --- a/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs +++ b/src/LanguageServer/Impl/Indexing/MostRecentDocumentSymbols.cs @@ -7,6 +7,7 @@ using Microsoft.Python.Analysis.Documents; using Microsoft.Python.Core; using Microsoft.Python.Core.Diagnostics; +using Microsoft.Python.Parsing.Ast; namespace Microsoft.Python.LanguageServer.Indexing { class MostRecentDocumentSymbols : IMostRecentDocumentSymbols { @@ -120,7 +121,20 @@ public void Dispose() { } private async Task> IndexAsync(IDocument doc, CancellationToken indexCt) { - var ast = await doc.GetAstAsync(indexCt); + PythonAst ast = null; + + for (var i = 0; i < 5; i++) { + ast = await doc.GetAstAsync(indexCt); + if (ast != null) { + break; + } + await Task.Delay(100); + } + + if (ast == null) { + return Array.Empty(); + } + indexCt.ThrowIfCancellationRequested(); var walker = new SymbolIndexWalker(ast); ast.Walk(walker);