Skip to content

Change debug launch handler to treat null/empty cwd to not change dir #694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -18,12 +18,6 @@ public static readonly

public class LaunchRequestArguments
{
/// <summary>
/// Gets or sets the absolute path to the program to debug.
/// </summary>
[Obsolete("This property has been deprecated in favor of the Script property.")]
public string Program { get; set; }

/// <summary>
/// Gets or sets the absolute path to the script to debug.
/// </summary>
81 changes: 40 additions & 41 deletions src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs
Original file line number Diff line number Diff line change
@@ -254,55 +254,56 @@ protected async Task HandleLaunchRequest(
{
this.RegisterEventHandlers();

// Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
// In case that is null, use the the folder of the script to be executed. If the resulting
// working dir path is a file path then extract the directory and use that.
string workingDir =
launchParams.Cwd ??
launchParams.Script ??
#pragma warning disable 618
launchParams.Program;
#pragma warning restore 618

// When debugging an "untitled" (unsaved) file - the working dir can't be derived
// from the Script path. OTOH, if the launch params specifies a Cwd, use it.
if (ScriptFile.IsUntitledPath(workingDir) && string.IsNullOrEmpty(launchParams.Cwd))
// Determine whether or not the working directory should be set in the PowerShellContext.
if ((this.editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local) &&
!this.editorSession.DebugService.IsDebuggerStopped)
{
workingDir = null;
}
// Get the working directory that was passed via the debug config
// (either via launch.json or generated via no-config debug).
string workingDir = launchParams.Cwd;

if (!string.IsNullOrEmpty(workingDir))
{
workingDir = PowerShellContext.UnescapePath(workingDir);
try
// Assuming we have a non-empty/null working dir, unescape the path and verify
// the path exists and is a directory.
if (!string.IsNullOrEmpty(workingDir))
{
if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
workingDir = PowerShellContext.UnescapePath(workingDir);
try
{
workingDir = Path.GetDirectoryName(workingDir);
if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this magical syntax?
File.GetAttributes(workingDir) & FileAttributes.Directory

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enums as flags. Attributes are usually modelled as flag enums by C# to make them more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See here)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually see the check as != 0, but another recommendation I've seen is to use HasFlag():

if (File.GetAttributes(workingDir).HasFlag(FileAttributes.Directory))
{
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently HasFlag() was added in .NET 4.

{
workingDir = Path.GetDirectoryName(workingDir);
}
}
catch (Exception ex)
{
workingDir = null;
Logger.Write(
LogLevel.Error,
$"The specified 'cwd' path is invalid: '{launchParams.Cwd}'. Error: {ex.Message}");
}
}
catch (Exception ex)
{
Logger.Write(LogLevel.Error, "cwd path is invalid: " + ex.Message);

workingDir = null;
}
}

if (string.IsNullOrEmpty(workingDir))
{
// If we have no working dir by this point and we are running in a temp console,
// pick some reasonable default.
if (string.IsNullOrEmpty(workingDir) && launchParams.CreateTemporaryIntegratedConsole)
{
#if CoreCLR
workingDir = AppContext.BaseDirectory;
//TODO: RKH 2018-06-26 .NET standard 2.0 has added Environment.CurrentDirectory - let's use it.
workingDir = AppContext.BaseDirectory;
#else
workingDir = Environment.CurrentDirectory;
workingDir = Environment.CurrentDirectory;
#endif
}
}

if (this.editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local &&
!this.editorSession.DebugService.IsDebuggerStopped)
{
await editorSession.PowerShellContext.SetWorkingDirectory(workingDir, isPathAlreadyEscaped: false);
Logger.Write(LogLevel.Verbose, "Working dir set to: " + workingDir);
// At this point, we will either have a working dir that should be set to cwd in
// the PowerShellContext or the user has requested (via an empty/null cwd) that
// the working dir should not be changed.
if (!string.IsNullOrEmpty(workingDir))
{
await editorSession.PowerShellContext.SetWorkingDirectory(workingDir, isPathAlreadyEscaped: false);
}

Logger.Write(LogLevel.Verbose, $"Working dir " + (string.IsNullOrEmpty(workingDir) ? "not set." : $"set to '{workingDir}'"));
}

// Prepare arguments to the script - if specified
@@ -315,9 +316,7 @@ protected async Task HandleLaunchRequest(

// Store the launch parameters so that they can be used later
this.noDebug = launchParams.NoDebug;
#pragma warning disable 618
this.scriptToLaunch = launchParams.Script ?? launchParams.Program;
#pragma warning restore 618
this.scriptToLaunch = launchParams.Script;
this.arguments = arguments;
this.IsUsingTempIntegratedConsole = launchParams.CreateTemporaryIntegratedConsole;