Skip to content

Commit 5bab47a

Browse files
committed
Change debug launch handler to treat null/empty cwd to not change dir
Fix for PowerShell/vscode-powershell#1330 Removes deprecated "Program" field from LaunchRequest class. Simplifies working dir logic in HandleLaunchRequest. Basically if the launch request happens in the regular integrated console, a null/empty cwd means "do not change the working dir". If the request is using the temp integrated console, there is no "existing" workng dir, so we use the original logic to set the working dir.
1 parent 045d65b commit 5bab47a

File tree

2 files changed

+20
-29
lines changed

2 files changed

+20
-29
lines changed

src/PowerShellEditorServices.Protocol/DebugAdapter/LaunchRequest.cs

-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@ public static readonly
1818

1919
public class LaunchRequestArguments
2020
{
21-
/// <summary>
22-
/// Gets or sets the absolute path to the program to debug.
23-
/// </summary>
24-
[Obsolete("This property has been deprecated in favor of the Script property.")]
25-
public string Program { get; set; }
26-
2721
/// <summary>
2822
/// Gets or sets the absolute path to the script to debug.
2923
/// </summary>

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

+20-23
Original file line numberDiff line numberDiff line change
@@ -255,22 +255,9 @@ protected async Task HandleLaunchRequest(
255255
this.RegisterEventHandlers();
256256

257257
// Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
258-
// In case that is null, use the the folder of the script to be executed. If the resulting
259-
// working dir path is a file path then extract the directory and use that.
260-
string workingDir =
261-
launchParams.Cwd ??
262-
launchParams.Script ??
263-
#pragma warning disable 618
264-
launchParams.Program;
265-
#pragma warning restore 618
266-
267-
// When debugging an "untitled" (unsaved) file - the working dir can't be derived
268-
// from the Script path. OTOH, if the launch params specifies a Cwd, use it.
269-
if (ScriptFile.IsUntitledPath(workingDir) && string.IsNullOrEmpty(launchParams.Cwd))
270-
{
271-
workingDir = null;
272-
}
258+
string workingDir = launchParams.Cwd;
273259

260+
// Assuming we have a specified working dir, unescape the path and verify it.
274261
if (!string.IsNullOrEmpty(workingDir))
275262
{
276263
workingDir = PowerShellContext.UnescapePath(workingDir);
@@ -289,18 +276,28 @@ protected async Task HandleLaunchRequest(
289276
}
290277
}
291278

292-
if (string.IsNullOrEmpty(workingDir))
279+
// A null or empty string value for CWD is an indicator to NOT change the working directory
280+
// but only if we are debugging in the integrated console where there is an existing cwd.
281+
// However, if we are running in a temporary integrated console, there is no pre-existing
282+
// cwd, so fall through to the "else if: where we set a working directory.
283+
if (string.IsNullOrEmpty(workingDir) && !launchParams.CreateTemporaryIntegratedConsole)
293284
{
285+
Logger.Write(LogLevel.Verbose, "Launch config requested working dir not be changed/set");
286+
}
287+
else if (this.editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local &&
288+
!this.editorSession.DebugService.IsDebuggerStopped)
289+
{
290+
// If we have no working dir by this point, pick some reasonable default.
291+
if (string.IsNullOrEmpty(workingDir))
292+
{
294293
#if CoreCLR
295-
workingDir = AppContext.BaseDirectory;
294+
//TODO: RKH 2018-06-26 .NET standard 2.0 has added Environment.CurrentDirectory - let's use it.
295+
workingDir = AppContext.BaseDirectory;
296296
#else
297-
workingDir = Environment.CurrentDirectory;
297+
workingDir = Environment.CurrentDirectory;
298298
#endif
299-
}
299+
}
300300

301-
if (this.editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local &&
302-
!this.editorSession.DebugService.IsDebuggerStopped)
303-
{
304301
await editorSession.PowerShellContext.SetWorkingDirectory(workingDir, isPathAlreadyEscaped: false);
305302
Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);
306303
}
@@ -316,7 +313,7 @@ protected async Task HandleLaunchRequest(
316313
// Store the launch parameters so that they can be used later
317314
this.noDebug = launchParams.NoDebug;
318315
#pragma warning disable 618
319-
this.scriptToLaunch = launchParams.Script ?? launchParams.Program;
316+
this.scriptToLaunch = launchParams.Script;
320317
#pragma warning restore 618
321318
this.arguments = arguments;
322319
this.IsUsingTempIntegratedConsole = launchParams.CreateTemporaryIntegratedConsole;

0 commit comments

Comments
 (0)