Skip to content

Workspace folder pick pops up if a predefined variable is used for the powershell.cwd setting #4163

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
5 of 6 tasks
lewis-yeung opened this issue Sep 2, 2022 · 6 comments · Fixed by #4171
Closed
5 of 6 tasks
Assignees
Labels

Comments

@lewis-yeung
Copy link

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

The description for the powershell.cwd setting says:

Predefined variables can be used (i.e. ${fileDirname} to use the current opened file's directory).

However, even if a predefined variable (e.g., ${fileDirname} or ${fileWorkspaceFolder}) is used for powershell.cwd, whenever I open a PowerShell (.ps1) file in a multi-root workspace for the first time, it prompts me to choose a workspace folder as the working directory:

Select a folder to use as the PowerShell extension's working directory.

This is so annoying and should not happen. While if I use a literal path such as C:\, it works fine and the workspace folder pick does not pop up.

Other possibly related settings I use:

{
  "powershell.startAutomatically": true,
  "powershell.integratedConsole.showOnStartup": false,
  "powershell.integratedConsole.startInBackground": true
}

Possibly related code:

let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", undefined);
// Only use the cwd setting if it exists.
if (await utils.checkIfDirectoryExists(cwd)) {
return cwd;
}

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.2.6
PSEdition                      Core
GitCommitId                    7.2.6
OS                             Microsoft Windows 10.0.22000
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.71.0
784b0177c56c607789f9638da7b6bf3230d47a8c
x64

Extension Version

Steps to Reproduce

  1. Apply the following settings:
    {
      "powershell.cwd": "${fileWorkspaceFolder}",
      "powershell.startAutomatically": true,
      "powershell.integratedConsole.showOnStartup": false,
      "powershell.integratedConsole.startInBackground": true
    }
    where any other predefined variable (e.g., ${fileDirname}) can be used for powershell.cwd.
  2. Open a multi-root workspace in a new VS Code window.
  3. Open a PowerShell (.ps1) file, and the workspace folder pick will pop up.

Visuals

No response

Logs

No response

@lewis-yeung lewis-yeung added the Issue-Bug A bug to squash. label Sep 2, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label Sep 2, 2022
@andyleejordan
Copy link
Member

Hm, I'll have to check when the pre-defined variable is getting interpolated.

@SydneyhSmith SydneyhSmith added Area-Workspaces and removed Needs: Triage Maintainer attention needed! labels Sep 8, 2022
@andyleejordan
Copy link
Member

andyleejordan commented Sep 8, 2022

@lewis-yeung can you confirm that this used to work as you expected in a prior version of the extension?

@andyleejordan
Copy link
Member

I actually can't imagine that ${fileWorkspaceFolder} or ${fileDirname} could work, because the cwd setting is an initialization setting, it's read and processed long before any file is opened. Put another way: it's a workspace-scoped variable, not a file-scoped variable.

@lewis-yeung
Copy link
Author

lewis-yeung commented Sep 9, 2022

I actually can't imagine that ${fileWorkspaceFolder} or ${fileDirname} could work, because the cwd setting is an initialization setting, it's read and processed long before any file is opened. Put another way: it's a workspace-scoped variable, not a file-scoped variable.

@andschwa In fact, the problem can arise with all predefined variables. I've tested ${workspaceFolder}, the workspace folder pick still popped up. With ${userHome}, it even showed an error:

The terminal process failed to launch: Starting directory (cwd) "${userHome}" does not exist.

Or worst, if a predefined variable is not designed to be used here at all, the following description should be adjusted since it's really misleading, IMO.

"description": "An explicit start path where the PowerShell Extension Terminal will be launched. Both the PowerShell process and the shell's location will be set to this directory. Predefined variables can be used (i.e. ${fileDirname} to use the current opened file's directory)."

@lewis-yeung
Copy link
Author

can you confirm that this used to work as you expected in a prior version of the extension?

@andschwa I can confirm that v2022.6.3 works fine, even I don't customize powershell.cwd. I believe the bug has appeared since v2022.7.1-preview as I see an entry in the changelog:

Add multi-root choice experience to powershell.cwd.

which is related to #4064.

@andyleejordan
Copy link
Member

I don't think that v2022.6.3 worked as you expected. While I believe that there was no error being thrown, I think the given value was still not having the predefined variables resolved. In that version, this was a silent error and the value was just passed along unvalidated, and the server ignored it since it wasn't a proper path. In the latest version, the setting is now validated and an error thrown.

Unfortunately, VS Code has yet to provide an API for extension's to resolve their pre-defined variables in our contributed settings: microsoft/vscode#46471

Until that API is available, this sadly isn't something we can support. I don't know why the existing setting says it was ever supported; that must just be a historical error. The best I can do right now is update package.json to stop lying about that.

@andyleejordan andyleejordan moved this from Todo to In Progress in American Pharoah Sep 13, 2022
Repository owner moved this from In Progress to Done in American Pharoah Sep 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants