MCPcopy
hub / github.com/PowerShell/vscode-powershell / validateCwdSetting

Function validateCwdSetting

src/settings.ts:280–337  ·  view source on GitHub ↗
(
    logger: ILogger | undefined,
)

Source from the content-addressed store, hash-verified

278}
279
280export async function validateCwdSetting(
281 logger: ILogger | undefined,
282): Promise<string> {
283 let cwd =
284 utils.stripQuotePair(
285 vscode.workspace
286 .getConfiguration(utils.PowerShellLanguageId)
287 .get<string>("cwd"),
288 ) ?? "";
289
290 // Replace ~ with home directory.
291 cwd = untildify(cwd);
292
293 // Use the cwd setting if it's absolute and exists. We don't use or resolve
294 // relative paths here because it'll be relative to the Code process's cwd,
295 // which is not what the user is expecting.
296 if (path.isAbsolute(cwd) && (await utils.checkIfDirectoryExists(cwd))) {
297 return cwd;
298 }
299
300 // If the cwd matches the name of a workspace folder, use it. Essentially
301 // "choose" a workspace folder based off the cwd path, and so set the state
302 // appropriately for `getChosenWorkspace`.
303 if (vscode.workspace.workspaceFolders) {
304 for (const workspaceFolder of vscode.workspace.workspaceFolders) {
305 // TODO: With some more work, we could support paths relative to a
306 // workspace folder name too.
307 if (cwd === workspaceFolder.name) {
308 hasChosen = true;
309 chosenWorkspace = workspaceFolder;
310 cwd = "";
311 }
312 }
313 }
314
315 // Otherwise get a cwd from the workspace, if possible.
316 const workspace = await getChosenWorkspace(logger);
317 if (workspace === undefined) {
318 logger?.writeDebug("Workspace was undefined, using homedir!");
319 return os.homedir();
320 }
321
322 const workspacePath = workspace.uri.fsPath;
323
324 // Use the chosen workspace's root to resolve the cwd.
325 const relativePath = path.join(workspacePath, cwd);
326 if (await utils.checkIfDirectoryExists(relativePath)) {
327 return relativePath;
328 }
329
330 // Just use the workspace path.
331 if (await utils.checkIfDirectoryExists(workspacePath)) {
332 return workspacePath;
333 }
334
335 // If all else fails, use the home directory.
336 return os.homedir();
337}

Callers 4

settings.test.tsFile · 0.90
startLanguageClientMethod · 0.90
startMethod · 0.90

Calls 2

getChosenWorkspaceFunction · 0.85
writeDebugMethod · 0.65

Tested by

no test coverage detected