Skip to content

Commit 40ed35d

Browse files
committed
Fixes #137 - working dir should be set to something reasonable.
Turns out vscode passes us the cwd. Just needed to rename the property in the LaunchRequest class to match. Same for args (renamed Arguments). I have not hooked up script args yet though.
1 parent 5c69c7d commit 40ed35d

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/PowerShellEditorServices.Protocol/DebugAdapter/LaunchRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public class LaunchRequestArguments
2424
public bool StopOnEntry { get; set; }
2525

2626
// /** Optional arguments passed to the debuggee. */
27-
public string[] Arguments { get; set; }
27+
public string[] Args { get; set; }
2828

2929
// /** Launch the debuggee in this working directory (specified as an absolute path). If omitted the debuggee is lauched in its own directory. */
30-
public string WorkingDirectory { get; set; }
30+
public string Cwd { get; set; }
3131

3232
// /** Absolute path to the runtime executable to be used. Default is the runtime executable on the PATH. */
3333
public string RuntimeExecutable { get; set; }

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
using Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter;
77
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
88
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.Channel;
9-
using Microsoft.PowerShell.EditorServices.Protocol.Server;
109
using Microsoft.PowerShell.EditorServices.Utility;
1110
using System;
1211
using System.Collections.Generic;
12+
using System.IO;
1313
using System.Linq;
1414
using System.Management.Automation;
1515
using System.Threading.Tasks;
@@ -74,23 +74,37 @@ protected async Task HandleLaunchRequest(
7474
LaunchRequestArguments launchParams,
7575
RequestContext<object> requestContext)
7676
{
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+
7785
// Execute the given PowerShell script and send the response.
7886
// Note that we aren't waiting for execution to complete here
7987
// because the debugger could stop while the script executes.
8088
Task executeTask =
8189
editorSession.PowerShellContext
82-
.ExecuteScriptAtPath(launchParams.Program)
90+
.ExecuteCommand(setWorkingDirCommand)
8391
.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+
});
94108
});
95109

96110
await requestContext.SendResult(null);

0 commit comments

Comments
 (0)