@@ -134,10 +134,10 @@ export interface INotebooksSettings {
134134 saveMarkdownCellsAs ?: CommentType ;
135135}
136136
137+ // TODO: This could probably be async, and call `validateCwdSetting()` directly.
137138export function load ( ) : ISettings {
138139 const configuration : vscode . WorkspaceConfiguration =
139- vscode . workspace . getConfiguration (
140- utils . PowerShellLanguageId ) ;
140+ vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
141141
142142 const defaultBugReportingSettings : IBugReportingSettings = {
143143 project : "https://github.com/PowerShell/vscode-powershell" ,
@@ -265,17 +265,17 @@ export function load(): ISettings {
265265 // is the reason terminals on macOS typically run login shells by default which set up
266266 // the environment. See http://unix.stackexchange.com/a/119675/115410"
267267 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 ) ,
270270 } ;
271271}
272272
273273// Get the ConfigurationTarget (read: scope) of where the *effective* setting value comes from
274274export async function getEffectiveConfigurationTarget ( settingName : string ) : Promise < vscode . ConfigurationTarget > {
275275 const configuration = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
276-
277276 const detail = configuration . inspect ( settingName ) ;
278277 let configurationTarget = null ;
278+
279279 if ( typeof detail . workspaceFolderValue !== "undefined" ) {
280280 configurationTarget = vscode . ConfigurationTarget . WorkspaceFolder ;
281281 }
@@ -294,7 +294,6 @@ export async function change(
294294 configurationTarget ?: vscode . ConfigurationTarget | boolean ) : Promise < void > {
295295
296296 const configuration = vscode . workspace . getConfiguration ( utils . PowerShellLanguageId ) ;
297-
298297 await configuration . update ( settingName , newValue , configurationTarget ) ;
299298}
300299
@@ -312,3 +311,30 @@ function getWorkspaceSettingsWithDefaults<TSettings>(
312311 }
313312 return defaultSettings ;
314313}
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+ } else {
329+ cwd = vscode . workspace . workspaceFolders ?. [ 0 ] . uri . fsPath ;
330+ }
331+ // If there were no workspace folders, or somehow they don't exist, use
332+ // the current process's cwd (but don't save it).
333+ if ( cwd === undefined || ! utils . checkIfDirectoryExists ( cwd ) ) {
334+ return process . cwd ( ) ;
335+ }
336+ // Save the valid 'cwd' to the workspace settings.
337+ await change ( "cwd" , cwd ) ;
338+ return cwd ;
339+ }
340+ }
0 commit comments