@@ -134,10 +134,10 @@ export interface INotebooksSettings {
134
134
saveMarkdownCellsAs ?: CommentType ;
135
135
}
136
136
137
+ // TODO: This could probably be async, and call `validateCwdSetting()` directly.
137
138
export function load ( ) : ISettings {
138
139
const configuration : vscode . WorkspaceConfiguration =
139
- vscode . workspace . getConfiguration (
140
- utils . PowerShellLanguageId ) ;
140
+ vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
141
141
142
142
const defaultBugReportingSettings : IBugReportingSettings = {
143
143
project : "https://github.com/PowerShell/vscode-powershell" ,
@@ -265,17 +265,17 @@ export function load(): ISettings {
265
265
// is the reason terminals on macOS typically run login shells by default which set up
266
266
// the environment. See http://unix.stackexchange.com/a/119675/115410"
267
267
configuration . get < IStartAsLoginShellSettings > ( "startAsLoginShell" , defaultStartAsLoginShellSettings ) ,
268
- cwd : // TODO: Should we resolve this path and/or default to a workspace folder?
269
- configuration . get < string > ( "cwd" , null ) ,
268
+ cwd : // NOTE: This must be validated at startup via `validateCwdSetting()`. There's probably a better way to do this.
269
+ configuration . get < string > ( "cwd" , undefined ) ,
270
270
} ;
271
271
}
272
272
273
273
// Get the ConfigurationTarget (read: scope) of where the *effective* setting value comes from
274
274
export async function getEffectiveConfigurationTarget ( settingName : string ) : Promise < vscode . ConfigurationTarget > {
275
275
const configuration = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
276
-
277
276
const detail = configuration . inspect ( settingName ) ;
278
277
let configurationTarget = null ;
278
+
279
279
if ( typeof detail . workspaceFolderValue !== "undefined" ) {
280
280
configurationTarget = vscode . ConfigurationTarget . WorkspaceFolder ;
281
281
}
@@ -294,7 +294,6 @@ export async function change(
294
294
configurationTarget ?: vscode . ConfigurationTarget | boolean ) : Promise < void > {
295
295
296
296
const configuration = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
297
-
298
297
await configuration . update ( settingName , newValue , configurationTarget ) ;
299
298
}
300
299
@@ -312,3 +311,30 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
312
311
}
313
312
return defaultSettings ;
314
313
}
314
+
315
+ export async function validateCwdSetting ( ) : Promise < string > {
316
+ let cwd : string = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) . get < string > ( "cwd" , null ) ;
317
+
318
+ // Only use the cwd setting if it exists.
319
+ if ( utils . checkIfDirectoryExists ( cwd ) ) {
320
+ return cwd ;
321
+ } else {
322
+ // Otherwise use a workspace folder, prompting if necessary.
323
+ if ( vscode . workspace . workspaceFolders ?. length > 1 ) {
324
+ const options : vscode . WorkspaceFolderPickOptions = {
325
+ placeHolder : "Select a folder to use as the PowerShell extension's working directory." ,
326
+ }
327
+ cwd = ( await vscode . window . showWorkspaceFolderPick ( options ) ) ?. uri . fsPath ;
328
+ // Save the picked 'cwd' to the workspace settings.
329
+ await change ( "cwd" , cwd ) ;
330
+ } else {
331
+ cwd = vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ;
332
+ }
333
+ // If there were no workspace folders, or somehow they don't exist, use
334
+ // the home directory.
335
+ if ( cwd === undefined || ! utils . checkIfDirectoryExists ( cwd ) ) {
336
+ return process . cwd ( ) ;
337
+ }
338
+ return cwd ;
339
+ }
340
+ }
0 commit comments