From 83eb079e6378fe02e7d10390e55a21c23ea215f1 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 10 Aug 2020 17:02:27 -0700 Subject: [PATCH] Fix startup assembly version loading issue in PS6 --- .../Internal/EditorServicesRunner.cs | 20 +++++++++---------- .../Internal/PsesLoadContext.cs | 20 +++++++++++++++---- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs b/src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs index 730964127..8619c75e3 100644 --- a/src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs +++ b/src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs @@ -118,19 +118,20 @@ private async Task CreateEditorServicesAndRunUntilShutdown() debugServerCreation = CreateDebugServerWithLanguageServerAsync(languageServer, usePSReadLine: _config.ConsoleRepl == ConsoleReplKind.PSReadLine); } -#pragma warning disable CS4014 - // We don't need to wait for this to start, since we instead wait for it to complete later - languageServer.StartAsync(); -#pragma warning restore CS4014 + Task languageServerStart = languageServer.StartAsync(); + Task debugServerStart = null; if (creatingDebugServer) { -#pragma warning disable CS4014 // We don't need to wait for this to start, since we instead wait for it to complete later - StartDebugServer(debugServerCreation); -#pragma warning restore CS4014 + debugServerStart = StartDebugServer(debugServerCreation); } + await languageServerStart.ConfigureAwait(false); + if (debugServerStart != null) + { + await debugServerStart.ConfigureAwait(false); + } await languageServer.WaitForShutdown().ConfigureAwait(false); } finally @@ -165,10 +166,7 @@ private async Task StartDebugServer(Task debugServerCreation) _logger.Log(PsesLogLevel.Diagnostic, "Starting debug server"); -#pragma warning disable CS4014 - // No need to await, since we just want to kick it off - debugServer.StartAsync(); -#pragma warning restore CS4014 + await debugServer.StartAsync().ConfigureAwait(false); } private Task RestartDebugServerAsync(PsesDebugServer debugServer, bool usePSReadLine) diff --git a/src/PowerShellEditorServices.Hosting/Internal/PsesLoadContext.cs b/src/PowerShellEditorServices.Hosting/Internal/PsesLoadContext.cs index ed2583df7..ee1e145a2 100644 --- a/src/PowerShellEditorServices.Hosting/Internal/PsesLoadContext.cs +++ b/src/PowerShellEditorServices.Hosting/Internal/PsesLoadContext.cs @@ -37,16 +37,15 @@ protected override Assembly Load(AssemblyName assemblyName) // we must restrict the code in here to only use core types, // otherwise we may depend on assembly that we are trying to load and cause a StackOverflowException + // If we find the required assembly in $PSHOME, let another mechanism load the assembly string psHomeAsmPath = Path.Join(s_psHome, $"{assemblyName.Name}.dll"); - - if (File.Exists(psHomeAsmPath)) + if (IsSatisfyingAssembly(assemblyName, psHomeAsmPath)) { return null; } string asmPath = Path.Join(_dependencyDirPath, $"{assemblyName.Name}.dll"); - - if (File.Exists(asmPath)) + if (IsSatisfyingAssembly(assemblyName, asmPath)) { return LoadFromAssemblyPath(asmPath); } @@ -74,5 +73,18 @@ private void TrySetName(string name) // Do nothing -- we did our best } } + + private static bool IsSatisfyingAssembly(AssemblyName requiredAssemblyName, string assemblyPath) + { + if (!File.Exists(assemblyPath)) + { + return false; + } + + AssemblyName asmToLoadName = AssemblyName.GetAssemblyName(assemblyPath); + + return string.Equals(asmToLoadName.Name, requiredAssemblyName.Name, StringComparison.OrdinalIgnoreCase) + && asmToLoadName.Version >= requiredAssemblyName.Version; + } } }