Skip to content

Commit bb3993b

Browse files
authored
Remove redundant asynchronicity in NuGetProjectFactory and its providers (#5755)
1 parent 7e08bbe commit bb3993b

19 files changed

Lines changed: 56 additions & 69 deletions

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,6 @@ dotnet_naming_rule.private_field_rule.style = private_member_style
146146
; Diagnostics rule: Don't leave unnecessary suppressions
147147
dotnet_diagnostic.IDE0076.severity = warning
148148
dotnet_diagnostic.IDE0005.severity = warning
149+
150+
; Code Quality rules: Assumes methods do null checks.
151+
dotnet_code_quality.CA1062.null_check_validation_methods = Assumes

src/NuGet.Clients/NuGet.PackageManagement.PowerShellCmdlets/PowerShellHost.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ private async Task<bool> IsWebSiteAsync(IVsProjectAdapter project)
838838
{
839839
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
840840

841-
return (await project.GetProjectTypeGuidsAsync()).Contains(VsProjectTypes.WebSiteProjectTypeGuid);
841+
return project.GetProjectTypeGuids().Contains(VsProjectTypes.WebSiteProjectTypeGuid);
842842
}
843843

844844
private async Task<Tuple<string, string>> CompleteTaskAsync(List<Task<Tuple<string, string>>> nameTasks)

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/GlobalSuppressions.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
[assembly: SuppressMessage("Build", "CA1822:Member GetConfigFilePaths does not access instance data and can be marked as static (Shared in VisualBasic)", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.LegacyPackageReferenceProject.GetConfigFilePaths(NuGet.Configuration.ISettings)~System.Collections.Generic.IList{System.String}")]
3535
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<bool> LegacyPackageReferenceProject.InstallPackageAsync(string packageId, VersionRange range, INuGetProjectContext _, BuildIntegratedInstallationContext __, CancellationToken token)', validate parameter '__' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.LegacyPackageReferenceProject.InstallPackageAsync(System.String,NuGet.Versioning.VersionRange,NuGet.ProjectManagement.INuGetProjectContext,NuGet.ProjectManagement.BuildIntegratedInstallationContext,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Boolean}")]
3636
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<bool> LegacyPackageReferenceProject.UninstallPackageAsync(PackageIdentity packageIdentity, INuGetProjectContext _, CancellationToken token)', validate parameter 'packageIdentity' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.LegacyPackageReferenceProject.UninstallPackageAsync(NuGet.Packaging.Core.PackageIdentity,NuGet.ProjectManagement.INuGetProjectContext,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Boolean}")]
37-
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<NuGetProject> LegacyPackageReferenceProjectProvider.TryCreateNuGetProjectAsync(IVsProjectAdapter vsProjectAdapter, ProjectProviderContext context, bool forceProjectType)', validate parameter 'vsProjectAdapter' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.LegacyPackageReferenceProjectProvider.TryCreateNuGetProjectAsync(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext,System.Boolean)~System.Threading.Tasks.Task{NuGet.ProjectManagement.NuGetProject}")]
37+
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'NuGetProject LegacyPackageReferenceProjectProvider.TryCreateNuGetProject(IVsProjectAdapter vsProjectAdapter, ProjectProviderContext context, bool forceProjectType)', validate parameter 'vsProjectAdapter' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.LegacyPackageReferenceProjectProvider.TryCreateNuGetProject(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext,System.Boolean)~NuGet.ProjectManagement.NuGetProject")]
3838
[assembly: SuppressMessage("Build", "CA1031:Modify 'GetAssemblyReferences' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.MicrosoftBuildEvaluationProjectUtility.GetAssemblyReferences(Microsoft.Build.Evaluation.Project)~System.Collections.Generic.IEnumerable{System.Tuple{Microsoft.Build.Evaluation.ProjectItem,System.Reflection.AssemblyName}}")]
3939
[assembly: SuppressMessage("Build", "CA1303:Method 'MultiSourcePackageFeed.MultiSourcePackageFeed(IEnumerable<SourceRepository> sourceRepositories, INuGetUILogger logger, INuGetTelemetryService telemetryService)' passes a literal string as parameter 'message' of a call to 'ArgumentException.ArgumentException(string message, string paramName)'. Retrieve the following string(s) from a resource table instead: \"Collection of source repositories cannot be empty\".", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.MultiSourcePackageFeed.#ctor(System.Collections.Generic.IEnumerable{NuGet.Protocol.Core.Types.SourceRepository},NuGet.PackageManagement.VisualStudio.INuGetUILogger,NuGet.Common.INuGetTelemetryService)")]
4040
[assembly: SuppressMessage("Build", "CA1822:Member AggregateSearchResultsAsync does not access instance data and can be marked as static (Shared in VisualBasic)", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.MultiSourcePackageFeed.AggregateSearchResultsAsync(System.String,System.Collections.Generic.IEnumerable{NuGet.PackageManagement.VisualStudio.SearchResult{NuGet.Protocol.Core.Types.IPackageSearchMetadata}},NuGet.PackageManagement.VisualStudio.MultiSourcePackageFeed.TelemetryState)~System.Threading.Tasks.Task{NuGet.PackageManagement.VisualStudio.SearchResult{NuGet.Protocol.Core.Types.IPackageSearchMetadata}}")]
@@ -45,12 +45,9 @@
4545
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<IPackageSearchMetadata> MultiSourcePackageMetadataProvider.GetLocalPackageMetadataAsync(PackageIdentity identity, bool includePrerelease, CancellationToken cancellationToken)', validate parameter 'identity' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.MultiSourcePackageMetadataProvider.GetLocalPackageMetadataAsync(NuGet.Packaging.Core.PackageIdentity,System.Boolean,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Protocol.Core.Types.IPackageSearchMetadata}")]
4646
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<IPackageSearchMetadata> MultiSourcePackageMetadataProvider.GetPackageMetadataAsync(PackageIdentity identity, bool includePrerelease, CancellationToken cancellationToken)', validate parameter 'identity' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.MultiSourcePackageMetadataProvider.GetPackageMetadataAsync(NuGet.Packaging.Core.PackageIdentity,System.Boolean,System.Threading.CancellationToken)~System.Threading.Tasks.Task{NuGet.Protocol.Core.Types.IPackageSearchMetadata}")]
4747
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<bool> CpsPackageReferenceProject.InstallPackageAsync(string packageId, VersionRange range, INuGetProjectContext nuGetProjectContext, BuildIntegratedInstallationContext installationContext, CancellationToken token)', validate parameter 'range' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.CpsPackageReferenceProject.InstallPackageAsync(System.String,NuGet.Versioning.VersionRange,NuGet.ProjectManagement.INuGetProjectContext,NuGet.ProjectManagement.BuildIntegratedInstallationContext,System.Threading.CancellationToken)~System.Threading.Tasks.Task{System.Boolean}")]
48-
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<NuGetProject> CpsPackageReferenceProjectProvider.TryCreateNuGetProjectAsync(IVsProjectAdapter vsProject, ProjectProviderContext context, bool forceProjectType)', validate parameter 'vsProject' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.CpsPackageReferenceProjectProvider.TryCreateNuGetProjectAsync(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext,System.Boolean)~System.Threading.Tasks.Task{NuGet.ProjectManagement.NuGetProject}")]
4948
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'NuGetLockService.NuGetLockService(JoinableTaskContext joinableTaskContext)', validate parameter 'joinableTaskContext' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.NuGetLockService.#ctor(Microsoft.VisualStudio.Threading.JoinableTaskContext)")]
5049
[assembly: SuppressMessage("Build", "CA1031:Modify 'CreateNuGetProjectAsync' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.NuGetProjectFactory.CreateNuGetProjectAsync``1(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext)~System.Threading.Tasks.Task{``0}")]
51-
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<TProject> NuGetProjectFactory.CreateNuGetProjectAsync<TProject>(IVsProjectAdapter vsProjectAdapter, ProjectProviderContext optionalContext = null)', validate parameter 'vsProjectAdapter' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.NuGetProjectFactory.CreateNuGetProjectAsync``1(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext)~System.Threading.Tasks.Task{``0}")]
52-
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task<NuGetProject> NuGetProjectFactory.TryCreateNuGetProjectAsync(IVsProjectAdapter vsProjectAdapter, ProjectProviderContext context)', validate parameter 'vsProjectAdapter' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.NuGetProjectFactory.TryCreateNuGetProjectAsync(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext)~System.Threading.Tasks.Task{NuGet.ProjectManagement.NuGetProject}")]
53-
[assembly: SuppressMessage("Build", "CA1031:Modify 'TryCreateNuGetProjectAsync' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.NuGetProjectFactory.TryCreateNuGetProjectAsync(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext)~System.Threading.Tasks.Task{NuGet.ProjectManagement.NuGetProject}")]
50+
[assembly: SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Not all failures are within our control, and failures to initialize one project system type should not crash the initialization.", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.NuGetProjectFactory.TryCreateNuGetProjectAsync(NuGet.VisualStudio.IVsProjectAdapter,NuGet.PackageManagement.VisualStudio.ProjectProviderContext)~System.Threading.Tasks.Task{NuGet.ProjectManagement.NuGetProject}")]
5451
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task ProjectJsonToPackageRefMigrator.MigrateAsync(BuildIntegratedNuGetProject project)', validate parameter 'project' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.ProjectJsonToPackageRefMigrator.MigrateAsync(NuGet.ProjectManagement.Projects.BuildIntegratedNuGetProject)~System.Threading.Tasks.Task")]
5552
[assembly: SuppressMessage("Build", "CA1062:In externally visible method 'Task ProjectRetargetingUtility.MarkPackagesForReinstallation(NuGetProject project, IList<PackageIdentity> packagesToBeReinstalled)', validate parameter 'project' is non-null before using it. If appropriate, throw an ArgumentNullException when the argument is null or add a Code Contract precondition asserting non-null argument.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.ProjectRetargetingUtility.MarkPackagesForReinstallation(NuGet.ProjectManagement.NuGetProject,System.Collections.Generic.IList{NuGet.Packaging.Core.PackageIdentity})~System.Threading.Tasks.Task")]
5653
[assembly: SuppressMessage("Build", "CA1031:Modify 'MarkPackagesForReinstallation' to catch a more specific allowed exception type, or rethrow the exception.", Justification = "<Pending>", Scope = "member", Target = "~M:NuGet.PackageManagement.VisualStudio.ProjectRetargetingUtility.MarkPackagesForReinstallation(NuGet.ProjectManagement.NuGetProject,System.Collections.Generic.IList{NuGet.Packaging.Core.PackageIdentity})~System.Threading.Tasks.Task")]

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/IDE/VSSolutionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,13 +912,13 @@ await _semaphoreLock.ExecuteAsync(async () =>
912912
}
913913
}
914914

915-
private async Task<NuGetProject> CreateNuGetProjectAsync(IVsProjectAdapter project, INuGetProjectContext projectContext = null)
915+
private Task<NuGetProject> CreateNuGetProjectAsync(IVsProjectAdapter project, INuGetProjectContext projectContext = null)
916916
{
917917
var context = new ProjectProviderContext(
918918
projectContext ?? EmptyNuGetProjectContext,
919919
() => PackagesFolderPathUtility.GetPackagesFolderPath(this, _settings.Value));
920920

921-
return await _projectSystemFactory.TryCreateNuGetProjectAsync(project, context);
921+
return _projectSystemFactory.TryCreateNuGetProjectAsync(project, context);
922922
}
923923

924924
internal async Task<IDictionary<string, List<IVsProjectAdapter>>> GetDependentProjectsDictionaryAsync()

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/ProjectSystems/MSBuildNuGetProjectSystemFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Globalization;
77
using System.Linq;
88
using System.Threading.Tasks;
9-
using Microsoft.VisualStudio.Shell;
109
using NuGet.ProjectManagement;
1110
using NuGet.VisualStudio;
1211
using VsMSBuildNuGetProjectSystemThunk = System.Func<
@@ -29,7 +28,7 @@ internal static class MSBuildNuGetProjectSystemFactory
2928
{ VsProjectTypes.DeploymentProjectTypeGuid, (project, nuGetProjectContext) => new VsMSBuildProjectSystem(project, nuGetProjectContext) }
3029
};
3130

32-
public async static Task<VsMSBuildProjectSystem> CreateMSBuildNuGetProjectSystemAsync(IVsProjectAdapter vsProjectAdapter, INuGetProjectContext nuGetProjectContext)
31+
public static VsMSBuildProjectSystem CreateMSBuildNuGetProjectSystem(IVsProjectAdapter vsProjectAdapter, INuGetProjectContext nuGetProjectContext)
3332
{
3433
if (vsProjectAdapter == null)
3534
{
@@ -43,7 +42,7 @@ public async static Task<VsMSBuildProjectSystem> CreateMSBuildNuGetProjectSystem
4342
Strings.DTE_ProjectUnsupported, vsProjectAdapter.ProjectName));
4443
}
4544

46-
var guids = await vsProjectAdapter.GetProjectTypeGuidsAsync();
45+
var guids = vsProjectAdapter.GetProjectTypeGuids();
4746
if (guids.Contains(VsProjectTypes.CppProjectTypeGuid)) // Got a cpp project
4847
{
4948
return new NativeProjectSystem(vsProjectAdapter, nuGetProjectContext);

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/ProjectSystems/VsMSBuildProjectSystem.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ public NuGetFramework TargetFramework
7878
{
7979
if (_targetFramework == null)
8080
{
81-
_targetFramework = NuGetUIThreadHelper.JoinableTaskFactory.Run(VsProjectAdapter.GetTargetFrameworkAsync);
81+
_targetFramework = NuGetUIThreadHelper.JoinableTaskFactory.Run(async () =>
82+
{
83+
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
84+
return VsProjectAdapter.GetTargetFramework();
85+
});
8286
}
8387

8488
return _targetFramework;
@@ -96,9 +100,9 @@ public VsMSBuildProjectSystem(
96100
NuGetProjectContext = nuGetProjectContext;
97101
}
98102

99-
public async Task InitializeProperties()
103+
public void InitializeProperties()
100104
{
101-
_targetFramework = await VsProjectAdapter.GetTargetFrameworkAsync();
105+
_targetFramework = VsProjectAdapter.GetTargetFramework();
102106
}
103107

104108
public virtual void AddFile(string path, Stream stream)

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/CpsPackageReferenceProjectProvider.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.ComponentModel.Composition;
6-
using System.Threading.Tasks;
76
using Microsoft;
87
using Microsoft.VisualStudio.ProjectSystem;
98
using Microsoft.VisualStudio.ProjectSystem.Properties;
@@ -32,31 +31,22 @@ public class CpsPackageReferenceProjectProvider : INuGetProjectProvider
3231

3332
[ImportingConstructor]
3433
public CpsPackageReferenceProjectProvider(IProjectSystemCache projectSystemCache, Lazy<IScriptExecutor> scriptExecutor)
35-
: this(AsyncServiceProvider.GlobalProvider, projectSystemCache, scriptExecutor)
36-
{ }
37-
38-
public CpsPackageReferenceProjectProvider(
39-
IAsyncServiceProvider vsServiceProvider,
40-
IProjectSystemCache projectSystemCache,
41-
Lazy<IScriptExecutor> scriptExecutor)
4234
{
43-
Assumes.Present(vsServiceProvider);
4435
Assumes.Present(projectSystemCache);
4536
Assumes.Present(scriptExecutor);
4637

4738
_projectSystemCache = projectSystemCache;
4839
_scriptExecutor = scriptExecutor;
4940
}
5041

51-
public async Task<NuGetProject> TryCreateNuGetProjectAsync(
42+
public NuGetProject TryCreateNuGetProject(
5243
IVsProjectAdapter vsProject,
5344
ProjectProviderContext context,
5445
bool forceProjectType)
5546
{
5647
Assumes.Present(vsProject);
5748
Assumes.Present(context);
58-
59-
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
49+
ThreadHelper.ThrowIfNotOnUIThread();
6050

6151
// The project must be an IVsHierarchy.
6252
var hierarchy = vsProject.VsHierarchy;

0 commit comments

Comments
 (0)