Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit cb1c3df

Browse files
authored
Fix various common exceptions, log on a failed directory load (#498)
* rethrow exception after telemetry instead of disposing * log an error instead of crashing when hitting some exceptions loading files, fix nullref when shutting down without an idle tracker * fix short circuiting of question mark checks to prevent null from being returned * print name of exception instead of relying on ToString * don't use MaybeEnumerate * upgrade message to Show
1 parent 2a91f85 commit cb1c3df

File tree

5 files changed

+19
-8
lines changed

5 files changed

+19
-8
lines changed

src/Analysis/Engine/Impl/Intellisense/AnalysisQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private async Task ConsumerLoop() {
6767
return;
6868
} catch (Exception ex) when (!ex.IsCriticalException()) {
6969
UnhandledException?.Invoke(this, new UnhandledExceptionEventArgs(ex, false));
70-
Dispose();
70+
throw;
7171
}
7272
}
7373
RaiseEventOnThreadPool(AnalysisComplete);

src/Analysis/Engine/Impl/Values/BuiltinNamespace.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
using System;
1818
using System.Collections.Generic;
19+
using System.Linq;
1920
using Microsoft.PythonTools.Analysis.Infrastructure;
2021
using Microsoft.PythonTools.Interpreter;
2122
using Microsoft.PythonTools.Parsing.Ast;
@@ -101,7 +102,7 @@ public bool TryGetMember(string name, out IAnalysisSet value) {
101102

102103
public virtual ILocatedMember GetLocatedMember() => null;
103104

104-
public override IEnumerable<ILocationInfo> Locations => GetLocatedMember()?.Locations.MaybeEnumerate();
105+
public override IEnumerable<ILocationInfo> Locations => GetLocatedMember()?.Locations ?? Enumerable.Empty<ILocationInfo>();
105106

106107
public override bool Equals(object obj) {
107108
if (obj is BuiltinNamespace<TMemberContainer> bn && GetType() == bn.GetType()) {

src/Analysis/Engine/Impl/Values/SpecializedNamespace.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,15 @@ public override bool IsOfType(IAnalysisSet klass) {
209209
return _original.IsOfType(klass);
210210
}
211211

212-
public override IEnumerable<ILocationInfo> Locations => _original?.Locations.MaybeEnumerate();
212+
public override IEnumerable<ILocationInfo> Locations => _original?.Locations ?? Enumerable.Empty<ILocationInfo>();
213213

214214
public override string Name => _original == null ? base.Name : _original.Name;
215215

216-
public override IEnumerable<OverloadResult> Overloads => _original?.Overloads.MaybeEnumerate();
216+
public override IEnumerable<OverloadResult> Overloads => _original?.Overloads ?? Enumerable.Empty<OverloadResult>();
217217

218218
public override IPythonType PythonType => _original?.PythonType;
219219

220-
internal override IEnumerable<ILocationInfo> References => _original?.References.MaybeEnumerate();
220+
internal override IEnumerable<ILocationInfo> References => _original?.References ?? Enumerable.Empty<ILocationInfo>();
221221

222222
public override PythonMemberType MemberType => _original == null ? PythonMemberType.Unknown : _original.MemberType;
223223

src/LanguageServer/Impl/LanguageServer.Lifetime.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ public async Task Shutdown() {
6464
// https://microsoft.github.io/language-server-protocol/specification#shutdown
6565
await _server.Shutdown();
6666
_shutdown = true;
67-
_idleTimeTracker.Dispose();
67+
_idleTimeTracker?.Dispose();
6868
}
6969

7070
[JsonRpcMethod("exit")]
7171
public async Task Exit() {
7272
await _server.Exit();
7373
_sessionTokenSource.Cancel();
74-
_idleTimeTracker.Dispose();
74+
_idleTimeTracker?.Dispose();
7575
// Per https://microsoft.github.io/language-server-protocol/specification#exit
7676
Environment.Exit(_shutdown ? 0 : 1);
7777
}

src/LanguageServer/Impl/LanguageServer.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Diagnostics;
20+
using System.IO;
2021
using System.Linq;
2122
using System.Threading;
2223
using System.Threading.Tasks;
@@ -168,7 +169,16 @@ public async Task DidChangeConfiguration(JToken token, CancellationToken cancell
168169
await _server.DidChangeConfiguration(new DidChangeConfigurationParams { settings = settings }, cancellationToken);
169170

170171
if (!_filesLoaded) {
171-
await LoadDirectoryFiles();
172+
try {
173+
await LoadDirectoryFiles();
174+
} catch (Exception ex) when (
175+
ex is IOException // FileNotFoundException, DirectoryNotFoundException, PathTooLongException, etc
176+
|| ex is UnauthorizedAccessException
177+
) {
178+
// These exceptions are not caused by the LS, but by invalid/inaccessible user-specified paths.
179+
_server.ShowMessage(MessageType.Error, $"Failed to load files in {_initParams.rootPath} - {ex.GetType().Name}: {ex.Message}");
180+
return;
181+
}
172182
}
173183
_filesLoaded = true;
174184
}

0 commit comments

Comments
 (0)