@@ -6,6 +6,7 @@ import utils = require("./utils");
66import os = require( "os" ) ;
77import { ILogger } from "./logging" ;
88import untildify from "untildify" ;
9+ import path = require( "path" ) ;
910
1011// TODO: Quite a few of these settings are unused in the client and instead
1112// exist just for the server. Those settings do not need to be represented in
@@ -214,47 +215,66 @@ export async function changeSetting(
214215}
215216
216217// We don't want to query the user more than once, so this is idempotent.
217- let hasPrompted = false ;
218- export let chosenWorkspace : vscode . WorkspaceFolder | undefined = undefined ;
219-
220- export async function validateCwdSetting ( logger : ILogger | undefined ) : Promise < string > {
221- let cwd : string | undefined = utils . stripQuotePair (
222- vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" ) ) ;
223-
224- // Only use the cwd setting if it exists.
225- if ( cwd !== undefined ) {
226- cwd = untildify ( cwd ) ;
227- if ( await utils . checkIfDirectoryExists ( cwd ) ) {
228- return cwd ;
229- }
218+ let hasChosen = false ;
219+ let chosenWorkspace : vscode . WorkspaceFolder | undefined = undefined ;
220+ export async function getChosenWorkspace ( logger : ILogger | undefined ) : Promise < vscode . WorkspaceFolder | undefined > {
221+ if ( hasChosen ) {
222+ return chosenWorkspace ;
230223 }
231224
232225 // If there is no workspace, or there is but it has no folders, fallback.
233226 if ( vscode . workspace . workspaceFolders === undefined
234227 || vscode . workspace . workspaceFolders . length === 0 ) {
235- cwd = undefined ;
228+ chosenWorkspace = undefined ;
236229 // If there is exactly one workspace folder, use that.
237230 } else if ( vscode . workspace . workspaceFolders . length === 1 ) {
238- cwd = vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ;
231+ chosenWorkspace = vscode . workspace . workspaceFolders [ 0 ] ;
239232 // If there is more than one workspace folder, prompt the user once.
240- } else if ( vscode . workspace . workspaceFolders . length > 1 && ! hasPrompted ) {
241- hasPrompted = true ;
233+ } else if ( vscode . workspace . workspaceFolders . length > 1 ) {
242234 const options : vscode . WorkspaceFolderPickOptions = {
243235 placeHolder : "Select a workspace folder to use for the PowerShell Extension." ,
244236 } ;
237+
245238 chosenWorkspace = await vscode . window . showWorkspaceFolderPick ( options ) ;
246- cwd = chosenWorkspace ?. uri . fsPath ;
247- // Save the picked 'cwd' to the workspace settings.
248- // We have to check again because the user may not have picked.
249- if ( cwd !== undefined && await utils . checkIfDirectoryExists ( cwd ) ) {
250- await changeSetting ( "cwd" , cwd , undefined , logger ) ;
239+ logger ?. writeVerbose ( `User selected workspace: '${ chosenWorkspace ?. name } '` ) ;
240+ }
241+
242+ // NOTE: We don't rely on checking if `chosenWorkspace` is undefined because
243+ // that may be the case if the user dismissed the prompt, and we don't want
244+ // to show it again this session.
245+ hasChosen = true ;
246+ return chosenWorkspace ;
247+ }
248+
249+ export async function validateCwdSetting ( logger : ILogger | undefined ) : Promise < string > {
250+ let cwd : string | undefined = utils . stripQuotePair (
251+ vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" ) ) ;
252+
253+ // Use the resolved cwd setting if it exists. We're checking truthiness
254+ // because it could be an empty string, in which case we ignore it.
255+ if ( cwd ) {
256+ cwd = path . resolve ( untildify ( cwd ) ) ;
257+ if ( await utils . checkIfDirectoryExists ( cwd ) ) {
258+ return cwd ;
251259 }
252260 }
253261
262+ // Otherwise get a cwd from the workspace, if possible.
263+ const workspace = await getChosenWorkspace ( logger ) ;
264+ cwd = workspace ?. uri . fsPath ;
265+
266+ // Save the picked 'cwd' to the workspace settings.
267+ if ( cwd && await utils . checkIfDirectoryExists ( cwd ) ) {
268+ // TODO: Stop saving this to settings! Instead, save the picked
269+ // workspace (or save this, but in a cache).
270+ await changeSetting ( "cwd" , cwd , undefined , logger ) ;
271+ }
272+
254273 // If there were no workspace folders, or somehow they don't exist, use
255274 // the home directory.
256- if ( cwd === undefined || ! await utils . checkIfDirectoryExists ( cwd ) ) {
275+ if ( ! cwd || ! await utils . checkIfDirectoryExists ( cwd ) ) {
257276 return os . homedir ( ) ;
258277 }
278+
259279 return cwd ;
260280}
0 commit comments