@@ -295,6 +295,7 @@ export async function change(
295
295
configurationTarget ?: vscode . ConfigurationTarget | boolean ) : Promise < void > {
296
296
297
297
const configuration = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
298
+ // TODO: Consider wrapping with try/catch, but we can't log the error.
298
299
await configuration . update ( settingName , newValue , configurationTarget ) ;
299
300
}
300
301
@@ -313,34 +314,46 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
313
314
return defaultSettings ;
314
315
}
315
316
317
+ // We don't want to query the user more than once, so this is idempotent.
318
+ let hasPrompted : boolean = false ;
319
+
316
320
export async function validateCwdSetting ( ) : Promise < string > {
317
- let cwd : string = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" , null ) ;
321
+ let cwd : string = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" , undefined ) ;
318
322
319
323
// Only use the cwd setting if it exists.
320
324
if ( await utils . checkIfDirectoryExists ( cwd ) ) {
321
325
return cwd ;
322
- } else {
323
- // If there is no workspace, or there is but it has no folders, fallback.
324
- if ( vscode . workspace . workspaceFolders === undefined
325
- || vscode . workspace . workspaceFolders ?. length === 0 ) {
326
- cwd = undefined ;
327
- // If there is exactly one workspace folder, use that.
328
- } if ( vscode . workspace . workspaceFolders ?. length === 1 ) {
329
- cwd = vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ;
330
- // If there is more than one workspace folder, prompt the user.
331
- } else if ( vscode . workspace . workspaceFolders ?. length > 1 ) {
332
- const options : vscode . WorkspaceFolderPickOptions = {
333
- placeHolder : "Select a folder to use as the PowerShell extension's working directory." ,
334
- }
335
- cwd = ( await vscode . window . showWorkspaceFolderPick ( options ) ) ?. uri . fsPath ;
336
- // Save the picked 'cwd' to the workspace settings.
337
- await change ( "cwd" , cwd ) ;
326
+ }
327
+
328
+ // If there is no workspace, or there is but it has no folders, fallback.
329
+ if ( vscode . workspace . workspaceFolders === undefined
330
+ || vscode . workspace . workspaceFolders ?. length === 0 ) {
331
+ cwd = undefined ;
332
+ // If there is exactly one workspace folder, use that.
333
+ } if ( vscode . workspace . workspaceFolders ?. length === 1 ) {
334
+ cwd = vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ;
335
+ // If there is more than one workspace folder, prompt the user once.
336
+ } else if ( vscode . workspace . workspaceFolders ?. length > 1 && ! hasPrompted ) {
337
+ hasPrompted = true ;
338
+ const options : vscode . WorkspaceFolderPickOptions = {
339
+ placeHolder : "Select a folder to use as the PowerShell extension's working directory." ,
338
340
}
339
- // If there were no workspace folders, or somehow they don't exist, use
340
- // the home directory.
341
- if ( cwd === undefined || ! await utils . checkIfDirectoryExists ( cwd ) ) {
342
- return os . homedir ( ) ;
341
+ cwd = ( await vscode . window . showWorkspaceFolderPick ( options ) ) ?. uri . fsPath ;
342
+ // Save the picked 'cwd' to the workspace settings.
343
+ // We have to check again because the user may not have picked.
344
+ if ( await utils . checkIfDirectoryExists ( cwd ) ) {
345
+ try {
346
+ await change ( "cwd" , cwd ) ;
347
+ } catch {
348
+ // Could fail if workspace file is invalid.
349
+ }
343
350
}
344
- return cwd ;
345
351
}
352
+
353
+ // If there were no workspace folders, or somehow they don't exist, use
354
+ // the home directory.
355
+ if ( cwd === undefined || ! await utils . checkIfDirectoryExists ( cwd ) ) {
356
+ return os . homedir ( ) ;
357
+ }
358
+ return cwd ;
346
359
}
0 commit comments