From 15525f2934df29263015f2546eb19aadbb6518b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Sch=C3=BCtz?= Date: Sat, 25 Jan 2020 22:40:48 +0100 Subject: [PATCH] Fix issue with "Add Import" code fix --- .../AddUsing/AddUsingTests_NuGet.cs | 33 +++++++++++++++++++ .../AbstractAddImportCodeFixProvider.cs | 16 +++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests_NuGet.cs b/src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests_NuGet.cs index 2e45f70efea..4943e87dc18 100644 --- a/src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests_NuGet.cs +++ b/src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests_NuGet.cs @@ -277,6 +277,39 @@ public async Task TestFailedInstallRollsBackFile() installerServiceMock.Verify(); } + [WorkItem(40857, "https://github.com/dotnet/roslyn/issues/40857")] + [Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddImport)] + public async Task TestNuGetFailureDoesNotImpactCodeFix() + { + var installerServiceMock = new Mock(MockBehavior.Strict); + installerServiceMock.Setup(i => i.IsEnabled(It.IsAny())).Returns(true); + installerServiceMock.Setup(i => i.GetPackageSources()).Throws(new Exception("Test Exception")); + + var packageServiceMock = new Mock(MockBehavior.Strict); + packageServiceMock.Setup(s => s.FindReferenceAssembliesWithTypeAsync("NuGetType", 0, It.IsAny())) + .Returns(Task.FromResult>(new List())); + packageServiceMock.Setup(s => s.FindPackagesWithTypeAsync(NugetOrgSource, "NuGetType", 0, It.IsAny())) + .Returns(CreateSearchResult(null)); + + await TestInRegularAndScriptAsync( +@"class Class +{ + [|IDictionary|] Method() + { + Goo(); + } +}", +@"using System.Collections; + +class Class +{ + IDictionary Method() + { + Goo(); + } +}", fixProviderData: new FixProviderData(installerServiceMock.Object, packageServiceMock.Object)); + } + private Task> CreateSearchResult( string packageName, string typeName, ImmutableArray containingNamespaceNames) { diff --git a/src/Features/Core/Portable/AddImport/AbstractAddImportCodeFixProvider.cs b/src/Features/Core/Portable/AddImport/AbstractAddImportCodeFixProvider.cs index d581823a15a..f1bb147db3e 100644 --- a/src/Features/Core/Portable/AddImport/AbstractAddImportCodeFixProvider.cs +++ b/src/Features/Core/Portable/AddImport/AbstractAddImportCodeFixProvider.cs @@ -56,9 +56,19 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context) : null; var installerService = GetPackageInstallerService(document); - var packageSources = searchNuGetPackages && symbolSearchService != null && installerService?.IsEnabled(document.Project.Id) == true - ? installerService.GetPackageSources() - : ImmutableArray.Empty; + ImmutableArray packageSources; + try + { + // Ensure any issues with getting access to package sources will not break the Code Fix for local imports + // https://github.com/dotnet/roslyn/issues/40857 + packageSources = searchNuGetPackages && symbolSearchService != null && installerService?.IsEnabled(document.Project.Id) == true + ? installerService.GetPackageSources() + : ImmutableArray.Empty; + } + catch + { + packageSources = ImmutableArray.Empty; + } var fixesForDiagnostic = await addImportService.GetFixesForDiagnosticsAsync( document, span, diagnostics, MaxResults, symbolSearchService, searchReferenceAssemblies, packageSources, cancellationToken).ConfigureAwait(false); -- GitLab