Skip to content

Commit bdea0c2

Browse files
Use one lstat call instead of calling exists first
1 parent 44327ca commit bdea0c2

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

extensions/ql-vscode/src/common/vscode/file-path-discovery.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
import { MultiFileSystemWatcher } from "./multi-file-system-watcher";
1010
import { AppEventEmitter } from "../events";
1111
import { extLogger } from "..";
12-
import { lstat, pathExists } from "fs-extra";
13-
import { containsPath } from "../../pure/files";
12+
import { lstat } from "fs-extra";
13+
import { containsPath, isIOError } from "../../pure/files";
1414
import { getOnDiskWorkspaceFoldersObjects } from "./workspace-folders";
1515

1616
interface PathData {
@@ -140,13 +140,24 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
140140
}
141141

142142
private async handledChangedPath(path: string): Promise<boolean> {
143-
if (!(await pathExists(path)) || !this.pathIsInWorkspace(path)) {
144-
return this.handleRemovedPath(path);
145-
}
146-
if ((await lstat(path)).isDirectory()) {
147-
return await this.handleChangedDirectory(path);
143+
try {
144+
// If the path is not in the workspace then we don't want to be
145+
// tracking or displaying it, so treat it as if it doesn't exist.
146+
if (!this.pathIsInWorkspace(path)) {
147+
return this.handleRemovedPath(path);
148+
}
149+
150+
if ((await lstat(path)).isDirectory()) {
151+
return await this.handleChangedDirectory(path);
152+
} else {
153+
return this.handleChangedFile(path);
154+
}
155+
} catch (e) {
156+
if (isIOError(e) && e.code === "ENOENT") {
157+
return this.handleRemovedPath(path);
158+
}
159+
throw e;
148160
}
149-
return this.handleChangedFile(path);
150161
}
151162

152163
private pathIsInWorkspace(path: string): boolean {

extensions/ql-vscode/src/pure/files.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,14 @@ export async function* walkDirectory(
107107
}
108108
}
109109
}
110+
111+
/**
112+
* Error thrown from methods from the 'fs' module.
113+
*/
114+
export interface IOError {
115+
readonly code: string;
116+
}
117+
118+
export function isIOError(e: any): e is IOError {
119+
return e.code !== undefined && typeof e.code === "string";
120+
}

0 commit comments

Comments
 (0)