|
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,23 +74,37 @@ 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 | + |
77 | 85 | // Execute the given PowerShell script and send the response.
|
78 | 86 | // Note that we aren't waiting for execution to complete here
|
79 | 87 | // because the debugger could stop while the script executes.
|
80 | 88 | Task executeTask =
|
81 | 89 | editorSession.PowerShellContext
|
82 |
| - .ExecuteScriptAtPath(launchParams.Program) |
| 90 | + .ExecuteCommand(setWorkingDirCommand) |
83 | 91 | .ContinueWith(
|
84 |
| - async (t) => |
85 |
| - { |
86 |
| - Logger.Write(LogLevel.Verbose, "Execution completed, terminating..."); |
87 |
| - |
88 |
| - await requestContext.SendEvent( |
89 |
| - TerminatedEvent.Type, |
90 |
| - null); |
91 |
| - |
92 |
| - // Stop the server |
93 |
| - this.Stop(); |
| 92 | + (t1) => { |
| 93 | + Logger.Write(LogLevel.Verbose, "Working dir set to '" + workingDir + "'"); |
| 94 | + |
| 95 | + editorSession.PowerShellContext |
| 96 | + .ExecuteScriptAtPath(launchParams.Program) |
| 97 | + .ContinueWith( |
| 98 | + async (t2) => { |
| 99 | + Logger.Write(LogLevel.Verbose, "Execution completed, terminating..."); |
| 100 | + |
| 101 | + await requestContext.SendEvent( |
| 102 | + TerminatedEvent.Type, |
| 103 | + null); |
| 104 | + |
| 105 | + // Stop the server |
| 106 | + this.Stop(); |
| 107 | + }); |
94 | 108 | });
|
95 | 109 |
|
96 | 110 | await requestContext.SendResult(null);
|
|
0 commit comments