Skip to content

Commit 58b7d95

Browse files
Fix checkIfDirectoryExists() so validateCwdSetting() works (#4099)
Oops, Node APIs are weird. Moved to VS Code APIs.
1 parent 585ea80 commit 58b7d95

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

src/process.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ export class PowerShellProcess {
211211

212212
// Check every 2 seconds
213213
for (let i = numOfTries; i > 0; i--) {
214-
if (utils.checkIfFileExists(this.sessionFilePath.fsPath)) {
214+
if (await utils.checkIfFileExists(this.sessionFilePath)) {
215215
this.log.write("Session file found");
216216
const sessionDetails = PowerShellProcess.readSessionFile(this.sessionFilePath);
217217
PowerShellProcess.deleteSessionFile(this.sessionFilePath);

src/settings.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ export async function validateCwdSetting(): Promise<string> {
317317
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", null);
318318

319319
// Only use the cwd setting if it exists.
320-
if (utils.checkIfDirectoryExists(cwd)) {
320+
if (await utils.checkIfDirectoryExists(cwd)) {
321321
return cwd;
322322
} else {
323323
// Otherwise use a workspace folder, prompting if necessary.
@@ -333,7 +333,7 @@ export async function validateCwdSetting(): Promise<string> {
333333
}
334334
// If there were no workspace folders, or somehow they don't exist, use
335335
// the home directory.
336-
if (cwd === undefined || !utils.checkIfDirectoryExists(cwd)) {
336+
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
337337
return os.homedir();
338338
}
339339
return cwd;

src/utils.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,19 @@ export function getPipePath(pipeName: string) {
3737
}
3838
}
3939

40-
export function checkIfFileExists(filePath: string): boolean {
40+
export async function checkIfFileExists(filePath: vscode.Uri): Promise<boolean> {
4141
try {
42-
fs.accessSync(filePath, fs.constants.R_OK);
43-
return true;
42+
const stat: vscode.FileStat = await vscode.workspace.fs.stat(filePath);
43+
return stat.type === vscode.FileType.File;
4444
} catch (e) {
4545
return false;
4646
}
4747
}
4848

49-
export function checkIfDirectoryExists(directoryPath: string): boolean {
49+
export async function checkIfDirectoryExists(directoryPath: string): Promise<boolean> {
5050
try {
51-
// tslint:disable-next-line:no-bitwise
52-
fs.accessSync(directoryPath, fs.constants.R_OK | fs.constants.O_DIRECTORY);
53-
return true;
51+
const stat: vscode.FileStat = await vscode.workspace.fs.stat(vscode.Uri.file(directoryPath));
52+
return stat.type === vscode.FileType.Directory;
5453
} catch (e) {
5554
return false;
5655
}

0 commit comments

Comments
 (0)