diff --git a/src/PowerShellEditorServices/Server/PsesLanguageServer.cs b/src/PowerShellEditorServices/Server/PsesLanguageServer.cs index e6480adac..82abc2b72 100644 --- a/src/PowerShellEditorServices/Server/PsesLanguageServer.cs +++ b/src/PowerShellEditorServices/Server/PsesLanguageServer.cs @@ -113,15 +113,6 @@ public async Task StartAsync() break; } } - - // Set the working directory of the PowerShell session to the workspace path - if (workspaceService.WorkspacePath != null - && Directory.Exists(workspaceService.WorkspacePath)) - { - await serviceProvider.GetService().SetWorkingDirectoryAsync( - workspaceService.WorkspacePath, - isPathAlreadyEscaped: false).ConfigureAwait(false); - } }); }).ConfigureAwait(false); diff --git a/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs b/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs index bf39a1f3f..8d94f1a7b 100644 --- a/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs +++ b/src/PowerShellEditorServices/Services/Workspace/Handlers/ConfigurationHandler.cs @@ -14,6 +14,7 @@ using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; using OmniSharp.Extensions.LanguageServer.Protocol.Models; using OmniSharp.Extensions.LanguageServer.Protocol.Workspace; +using System.IO; namespace Microsoft.PowerShell.EditorServices.Handlers { @@ -26,6 +27,7 @@ internal class PsesConfigurationHandler : IDidChangeConfigurationHandler private DidChangeConfigurationCapability _capability; private bool _profilesLoaded; private bool _consoleReplStarted; + private bool _cwdSet; public PsesConfigurationHandler( ILoggerFactory factory, @@ -66,6 +68,26 @@ public async Task Handle(DidChangeConfigurationParams request, Cancellatio _workspaceService.WorkspacePath, _logger); + if (!this._cwdSet) + { + if (!string.IsNullOrEmpty(_configurationService.CurrentSettings.Cwd) + && Directory.Exists(_configurationService.CurrentSettings.Cwd)) + { + await _powerShellContextService.SetWorkingDirectoryAsync( + _configurationService.CurrentSettings.Cwd, + isPathAlreadyEscaped: false).ConfigureAwait(false); + + } else if (_workspaceService.WorkspacePath != null + && Directory.Exists(_workspaceService.WorkspacePath)) + { + await _powerShellContextService.SetWorkingDirectoryAsync( + _workspaceService.WorkspacePath, + isPathAlreadyEscaped: false).ConfigureAwait(false); + } + + this._cwdSet = true; + } + if (!this._profilesLoaded && _configurationService.CurrentSettings.EnableProfileLoading && oldLoadProfiles != _configurationService.CurrentSettings.EnableProfileLoading) diff --git a/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs b/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs index 705c79866..2e2441619 100644 --- a/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs +++ b/src/PowerShellEditorServices/Services/Workspace/LanguageServerSettings.cs @@ -30,6 +30,8 @@ internal class LanguageServerSettings public PesterSettings Pester { get; set; } + public string Cwd { get; set; } + public LanguageServerSettings() { this.ScriptAnalysis = new ScriptAnalysisSettings(); @@ -56,6 +58,7 @@ public void Update( this.CodeFormatting = new CodeFormattingSettings(settings.CodeFormatting); this.CodeFolding.Update(settings.CodeFolding, logger); this.Pester = new PesterSettings(settings.Pester); + this.Cwd = settings.Cwd; } } }