From 1d27dd7a4c20aaea97ead93d0c0306574bb03599 Mon Sep 17 00:00:00 2001 From: Abdelrahman Shawki Hassan Date: Tue, 17 Dec 2024 14:04:21 +0100 Subject: [PATCH] fix: after vs load init (#338) * fix: delay force init until project exists * fix: execute LanguageClientManagerOnLanguageClientNotInitializedAsync once at a time --- .../SnykVSPackage.cs | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/Snyk.VisualStudio.Extension.2022/SnykVSPackage.cs b/Snyk.VisualStudio.Extension.2022/SnykVSPackage.cs index 0f0d93b5..eab48c8a 100644 --- a/Snyk.VisualStudio.Extension.2022/SnykVSPackage.cs +++ b/Snyk.VisualStudio.Extension.2022/SnykVSPackage.cs @@ -9,6 +9,7 @@ using EnvDTE; using Microsoft; using Microsoft.VisualStudio.ComponentModelHost; +using Microsoft.VisualStudio.PlatformUI; using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; using Serilog; @@ -64,6 +65,7 @@ public sealed class SnykVSPackage : AsyncPackage, ISnykOptionsProvider new TaskCompletionSource(); public static SnykVSPackage Instance; + private readonly SemaphoreSlim languageClientInitSemaphore = new SemaphoreSlim(1, 1); private ISnykServiceProvider serviceProvider; @@ -229,7 +231,7 @@ private async Task InitializeLanguageClientAsync() { try { - this.serviceProvider.LanguageClientManager.OnLanguageClientNotInitializedAsync += LanguageClientManagerOnOnLanguageClientNotInitializedAsync; + this.serviceProvider.LanguageClientManager.OnLanguageClientNotInitializedAsync += LanguageClientManagerOnLanguageClientNotInitializedAsync; this.serviceProvider.LanguageClientManager.OnLanguageServerReadyAsync += LanguageClientManagerOnOnLanguageServerReadyAsync; if (!LanguageClientHelper.IsLanguageServerReady()) { @@ -257,21 +259,47 @@ private async Task LanguageClientManagerOnOnLanguageServerReadyAsync(object send } private Window tempOpenedFileWindow; - private async Task LanguageClientManagerOnOnLanguageClientNotInitializedAsync(object sender, SnykLanguageServerEventArgs args) + private async Task LanguageClientManagerOnLanguageClientNotInitializedAsync(object sender, SnykLanguageServerEventArgs args) { - await JoinableTaskFactory.SwitchToMainThreadAsync(); + await languageClientInitSemaphore.WaitAsync(DisposalToken); + + ThreadHelper.JoinableTaskFactory.RunAsync(async () => + { + try + { + await JoinableTaskFactory.SwitchToMainThreadAsync(); + while (!LanguageClientHelper.IsLanguageServerReady()) + { + var isSolutionOrFolderOpen = SnykSolutionService.Instance.IsSolutionOpen(); + if (isSolutionOrFolderOpen) + { + var dte = (DTE)await GetServiceAsync(typeof(DTE)); + if (dte == null) return; - var dte = (DTE)await GetServiceAsync(typeof(DTE)); - if (dte == null) return; + // Get the path to the file within the installed extension directory + var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + if (assemblyLocation == null) return; - // Get the path to the file within the installed extension directory - var assemblyLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); - if (assemblyLocation == null) return; + var filePath = Path.Combine(assemblyLocation, "Resources", "SnykLsInit.cs"); - var filePath = Path.Combine(assemblyLocation, "Resources", "SnykLsInit.cs"); + // Open the file + tempOpenedFileWindow = + dte.ItemOperations.OpenFile(filePath, EnvDTE.Constants.vsViewKindTextView); + return; + } - // Open the file - tempOpenedFileWindow = dte.ItemOperations.OpenFile(filePath, EnvDTE.Constants.vsViewKindTextView); + await Task.Delay(3000, DisposalToken); + } + } + catch (Exception ex) + { + Logger.Error("LanguageClientManagerOnLanguageClientNotInitializedAsync Failed with {Ex}", ex); + } + finally + { + languageClientInitSemaphore.Release(); + } + }).FireAndForget(); } private async Task InitializeGeneralOptionsAsync()