Skip to content

Fix checkIfDirectoryExists() so validateCwdSetting() works #4099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export class PowerShellProcess {

// Check every 2 seconds
for (let i = numOfTries; i > 0; i--) {
if (utils.checkIfFileExists(this.sessionFilePath.fsPath)) {
if (await utils.checkIfFileExists(this.sessionFilePath)) {
this.log.write("Session file found");
const sessionDetails = PowerShellProcess.readSessionFile(this.sessionFilePath);
PowerShellProcess.deleteSessionFile(this.sessionFilePath);
Expand Down
4 changes: 2 additions & 2 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export async function validateCwdSetting(): Promise<string> {
let cwd: string = vscode.workspace.getConfiguration(utils.PowerShellLanguageId).get<string>("cwd", null);

// Only use the cwd setting if it exists.
if (utils.checkIfDirectoryExists(cwd)) {
if (await utils.checkIfDirectoryExists(cwd)) {
return cwd;
} else {
// Otherwise use a workspace folder, prompting if necessary.
Expand All @@ -333,7 +333,7 @@ export async function validateCwdSetting(): Promise<string> {
}
// If there were no workspace folders, or somehow they don't exist, use
// the home directory.
if (cwd === undefined || !utils.checkIfDirectoryExists(cwd)) {
if (cwd === undefined || !await utils.checkIfDirectoryExists(cwd)) {
return os.homedir();
}
return cwd;
Expand Down
13 changes: 6 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,19 @@ export function getPipePath(pipeName: string) {
}
}

export function checkIfFileExists(filePath: string): boolean {
export async function checkIfFileExists(filePath: vscode.Uri): Promise<boolean> {
try {
fs.accessSync(filePath, fs.constants.R_OK);
return true;
const stat: vscode.FileStat = await vscode.workspace.fs.stat(filePath);
return stat.type === vscode.FileType.File;
} catch (e) {
return false;
}
}

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