Skip to content

Allow user to customize the working dir that the debugger starts in #88

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

Closed
rkeithhill opened this issue Feb 7, 2016 · 9 comments · May be fixed by terrorizer1980/vscode-powershell#1 or terrorizer1980/vscode-powershell#8
Assignees
Labels
Issue-Enhancement A feature request (enhancement).
Milestone

Comments

@rkeithhill
Copy link
Contributor

Technically you can do this without any change to the package.json file but without cwd being defined in the configurationProperties, VSCode will whine when you edit a launch.json file and add the property.

@rkeithhill rkeithhill added the Issue-Enhancement A feature request (enhancement). label Feb 7, 2016
@rkeithhill rkeithhill added this to the 0.4.0 milestone Feb 7, 2016
@rkeithhill rkeithhill self-assigned this Feb 7, 2016
@daviwil
Copy link
Contributor

daviwil commented Feb 7, 2016

Can we add cwd to our default configurations and set it to ${workspaceRoot}? I guess I'm still sort of wondering whether it should be the script's path by default if no other working path is set. We could try setting it to ${file} by default and then grab the directory path of whatever the user passed in.

@rkeithhill
Copy link
Contributor Author

Can we add cwd to our default configurations and set it to ${workspaceRoot} ?

Yes. That's what I was going to do after the PSES side PR got merged. That is what both the mono-debug and node-debug packages do.

I guess I'm still sort of wondering whether it should be the script's path by default if no other working path is set.

Good question. That was the way I was previously going to implement it (and what you get if VSCode passes cwd = null) except that it gets the directory of launchParams.Program (which defaults to ${file}). Hmm, but the user can override Program in their launch.json file so if the user wanted this behavior all they (or we by default) need to do is set cwd to null. Cool. So how should be initialize cwd by default? ${workspaceRoot} or null?

@daviwil
Copy link
Contributor

daviwil commented Feb 7, 2016

I'd say try it with null first to see if it does in fact set the current working directory to the script's path. Since we're auto-executing the currently opened script in the debugger now, I think it'd make more sense if the cwd was the script's path rather than the workspace path. Users who know they need the workspacePath set instead will figure out how to set it.

@rkeithhill
Copy link
Contributor Author

Will do.

@rkeithhill
Copy link
Contributor Author

OK so neither null nor empty string worked as a sentinel value. If I use either of those two values in launch.json, VSCode just passes in the workspace root dir. So I'll default it to ${file}.

@daviwil
Copy link
Contributor

daviwil commented Feb 7, 2016

Sounds good! That may require another change in the PR we just merged in Editor Services, though. I think we need to call Path.GetDirectoryName on this line. I think it's the right thing to do, so it's working making another PR for that.

@daviwil
Copy link
Contributor

daviwil commented Feb 7, 2016

Err, to be more specific, we need to call Path.GetDirectoryName on the value of cwd which came from LaunchRequest.

@rkeithhill
Copy link
Contributor Author

Heheh, yup working on that. Here is what I have:

// Set the working directory for the PowerShell runspace to something reasonable
// such as the cwd passed in via launch.json. And in case that is null, use the
// the folder of the script to be executed.  If cwd is a file path then extract the 
// directory and use that.
string workingDir = launchParams.Cwd ?? launchParams.Program;
try
{
    if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory)
    {
        workingDir = Path.GetDirectoryName(workingDir);
    }
}
catch (Exception ex)
{
    Logger.Write(LogLevel.Error, "cwd path is bad: " + ex.Message);
}

var setWorkingDirCommand = new PSCommand();
setWorkingDirCommand.AddCommand(@"Microsoft.PowerShell.Management\Set-Location")
    .AddParameter("LiteralPath", workingDir);

await editorSession.PowerShellContext.ExecuteCommand(setWorkingDirCommand);

Note that I can't seem to set a default cwd value of ${file} in package.json. VSCode just sends in the workspace root dir as cwd *if cwd is not set in launch.json. I think this is probably OK since the launch.json that will get created should have "cwd": "${file}" set in it. What do you think?

@daviwil
Copy link
Contributor

daviwil commented Feb 7, 2016

Yep, sounds fine. New changes look good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment