Skip to content

Commit a23c99e

Browse files
committed
Adding support for passing ${file} as cwd.
If cwd is a file path we check for that and if so, we grab its folder. If any error occurs during this test for dir/grab parent dir operation we log it and continue. I don't think we should crash the debugger. Unfortunately the call to set-location with a bad path doens't get echoed to the debug console (just the debugAdapter.log) so the user won't know they typo'd a path - if they change the default. :-(
1 parent 83aed94 commit a23c99e

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,22 @@ 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);
77+
// Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
78+
// In case that is null, use the the folder of the script to be executed. If the resulting
79+
// working dir path is a file path then extract the directory and use that.
80+
string workingDir = launchParams.Cwd ?? launchParams.Program;
81+
try
82+
{
83+
if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
84+
{
85+
workingDir = Path.GetDirectoryName(workingDir);
86+
}
87+
}
88+
catch (Exception ex)
89+
{
90+
Logger.Write(LogLevel.Error, "cwd path is bad: " + ex.Message);
91+
}
92+
8193
var setWorkingDirCommand = new PSCommand();
8294
setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location")
8395
.AddParameter("LiteralPath", workingDir);

0 commit comments

Comments
 (0)