|
6 | 6 | using Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter; |
7 | 7 | using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol; |
8 | 8 | using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel; |
9 | | -using Microsoft.PowerShell.EditorServices.Protocol.Server; |
10 | 9 | using Microsoft.PowerShell.EditorServices.Utility; |
11 | 10 | using System; |
12 | 11 | using System.Collections.Generic; |
| 12 | +using System.IO; |
13 | 13 | using System.Linq; |
14 | 14 | using System.Management.Automation; |
15 | 15 | using System.Threading.Tasks; |
@@ -74,15 +74,26 @@ protected async Task HandleLaunchRequest( |
74 | 74 | LaunchRequestArguments launchParams, |
75 | 75 | RequestContext<object> requestContext) |
76 | 76 | { |
| 77 | + // Set the working directory for the PowerShell runspace to something reasonable |
| 78 | + // such as the cwd passed in via launch.json. And in case that is null, use the |
| 79 | + // folder or the script to be executed. |
| 80 | + string workingDir = launchParams.Cwd ?? Path.GetDirectoryName(launchParams.Program); |
| 81 | + var setWorkingDirCommand = new PSCommand(); |
| 82 | + setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location") |
| 83 | + .AddParameter("LiteralPath", workingDir); |
| 84 | + |
| 85 | + await editorSession.PowerShellContext.ExecuteCommand(setWorkingDirCommand); |
| 86 | + |
| 87 | + Logger.Write(LogLevel.Verbose, "Working dir set to '" + workingDir + "'"); |
| 88 | + |
77 | 89 | // Execute the given PowerShell script and send the response. |
78 | 90 | // Note that we aren't waiting for execution to complete here |
79 | 91 | // because the debugger could stop while the script executes. |
80 | 92 | Task executeTask = |
81 | 93 | editorSession.PowerShellContext |
82 | 94 | .ExecuteScriptAtPath(launchParams.Program) |
83 | 95 | .ContinueWith( |
84 | | - async (t) => |
85 | | - { |
| 96 | + async (t) => { |
86 | 97 | Logger.Write(LogLevel.Verbose, "Execution completed, terminating..."); |
87 | 98 |
|
88 | 99 | await requestContext.SendEvent( |
|
0 commit comments