Skip to content

Commit d4aec5f

Browse files
committed
Merge pull request #140 from rkeithhill/rkeithhill/accept-dollarfile-as-cwd
Adding support for passing ${file} as cwd.
2 parents 83aed94 + 7ea1598 commit d4aec5f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,23 @@ 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+
workingDir = Environment.CurrentDirectory;
92+
}
93+
8194
var setWorkingDirCommand = new PSCommand();
8295
setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location")
8396
.AddParameter("LiteralPath", workingDir);

0 commit comments

Comments
 (0)