diff --git a/src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests_NuGet.cs b/src/EditorFeatures/CSharpTest/AddUsing/AddUsingTests_NuGet.cs index 2e45f70efea3ec2ef4349c7733e3cba1082c5670..4943e87dc18345bc5cda360cad6b29fe4a5d2b08 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 d581823a15a0bfa42ad27f7198ccb86c36ceeaa6..f1bb147db3e9522892e4e1f6c4902d8d24b6761f 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);