diff --git a/src/LanguageServer/Impl/Sources/DefinitionSource.cs b/src/LanguageServer/Impl/Sources/DefinitionSource.cs index 5bbebe062..a91cad1fe 100644 --- a/src/LanguageServer/Impl/Sources/DefinitionSource.cs +++ b/src/LanguageServer/Impl/Sources/DefinitionSource.cs @@ -123,6 +123,8 @@ private Reference HandleFromImport(IDocumentAnalysis analysis, SourceLocation lo case ImplicitPackageImport packageImport: module = mres.GetImportedModule(packageImport.FullName); break; + case ImportNotFound _: + return null; } // Are we in the module name (i.e. A in 'from A import B')? @@ -262,7 +264,8 @@ private Reference TryFromVariable(string name, IDocumentAnalysis analysis, Sourc definingMember = null; var m = analysis.ExpressionEvaluator.LookupNameInScopes(name, out var scope, LookupOptions.All); - if (m == null || scope.Module.ModuleType == ModuleType.Builtins || !(scope.Variables[name] is IVariable v)) { + var v = scope?.Variables[name]; + if (m == null || scope == null || scope.Module.ModuleType == ModuleType.Builtins || v.IsUnknown()) { return null; } diff --git a/src/LanguageServer/Test/GoToDefinitionTests.cs b/src/LanguageServer/Test/GoToDefinitionTests.cs index 6f98ec046..37bcb933b 100644 --- a/src/LanguageServer/Test/GoToDefinitionTests.cs +++ b/src/LanguageServer/Test/GoToDefinitionTests.cs @@ -688,5 +688,27 @@ def c(): ... reference.uri.AbsolutePath.Should().Contain("other.py"); reference.range.Should().Be(3, 4, 3, 5); } + + [TestMethod, Priority(0)] + public async Task UnknownType() { + const string code = @" +A +"; + var analysis = await GetAnalysisAsync(code); + var ds = new DefinitionSource(Services); + var reference = ds.FindDefinition(analysis, new SourceLocation(2, 1), out _); + reference.Should().BeNull(); + } + + [TestMethod, Priority(0)] + public async Task UnknownImportedType() { + const string code = @" +from nonexistent import some +"; + var analysis = await GetAnalysisAsync(code); + var ds = new DefinitionSource(Services); + var reference = ds.FindDefinition(analysis, new SourceLocation(2, 26), out _); + reference.Should().BeNull(); + } } }