Skip to content

Commit c239d6b

Browse files
authored
Change fs.existsSync to check[File|Directory]ExistsSync for cases where it matters. (#14258)
1 parent a66064b commit c239d6b

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

Extension/src/Debugger/configurationProvider.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* ------------------------------------------------------------------------------------------ */
55

66
import * as jsonc from 'comment-json';
7-
import * as fs from 'fs';
87
import * as glob from 'glob';
98
import * as os from 'os';
109
import * as path from 'path';
@@ -22,8 +21,8 @@ import * as logger from '../logger';
2221
import { PlatformInformation } from '../platform';
2322
import { rsync, scp, ssh } from '../SSH/commands';
2423
import * as Telemetry from '../telemetry';
25-
import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess';
2624
import { AttachItem, showQuickPick } from './attachQuickPick';
25+
import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess';
2726
import { ConfigMenu, ConfigMode, ConfigSource, CppDebugConfiguration, DebuggerEvent, DebuggerType, DebugType, IConfiguration, IConfigurationSnippet, isDebugLaunchStr, MIConfigurations, PipeTransportConfigurations, TaskStatus, WindowsConfigurations, WSLConfigurations } from './configurations';
2827
import { NativeAttachItemsProviderFactory } from './nativeAttach';
2928
import { Environment, ParsedEnvironmentFile } from './ParsedEnvironmentFile';
@@ -303,7 +302,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
303302

304303
// Validate LLDB-MI
305304
if (os.platform() === 'darwin' && // Check for macOS
306-
fs.existsSync(lldb_mi_10_x_path) && // lldb-mi 10.x exists
305+
util.checkFileExistsSync(lldb_mi_10_x_path) && // lldb-mi 10.x exists
307306
(!macOSMIMode || macOSMIMode === 'lldb') &&
308307
!macOSMIDebuggerPath // User did not provide custom lldb-mi
309308
) {
@@ -657,7 +656,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
657656
];
658657

659658
for (const searchPath of searchPaths) {
660-
if (fs.existsSync(path.join(searchPath, LLDBFramework))) {
659+
if (util.checkDirectoryExistsSync(path.join(searchPath, LLDBFramework))) {
661660
// Found a framework that 'lldb-mi' can use.
662661
return searchPath;
663662
}

Extension/src/LanguageServer/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,7 +1285,7 @@ export class DefaultClient implements Client {
12851285
}
12861286

12871287
this.rootFolder = workspaceFolder;
1288-
this.rootRealPath = this.RootPath ? fs.existsSync(this.RootPath) ? fs.realpathSync(this.RootPath) : this.RootPath : "";
1288+
this.rootRealPath = this.RootPath ? util.checkDirectoryExistsSync(this.RootPath) ? fs.realpathSync(this.RootPath) : this.RootPath : "";
12891289

12901290
this.workspaceStoragePath = util.extensionContext?.storageUri?.fsPath ?? "";
12911291
if (this.workspaceStoragePath.length > 0) {
@@ -1612,7 +1612,7 @@ export class DefaultClient implements Client {
16121612
this.currentCaseSensitiveFileSupport = new PersistentWorkspaceState<boolean>("CPP.currentCaseSensitiveFileSupport", false);
16131613
let resetDatabase: boolean = false;
16141614
const serverModule: string = getLanguageServerFileName();
1615-
const exeExists: boolean = fs.existsSync(serverModule);
1615+
const exeExists: boolean = util.checkFileExistsSync(serverModule);
16161616
if (!exeExists) {
16171617
telemetry.logLanguageServerEvent("missingLanguageServerBinary");
16181618
throw String('Missing binary at ' + serverModule);

Extension/src/LanguageServer/configurations.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class CppProperties {
254254
// to the language server until the default include paths and frameworks have been sent.
255255

256256
const configFilePath: string = path.join(this.configFolder, "c_cpp_properties.json");
257-
if (this.rootUri !== null && fs.existsSync(configFilePath)) {
257+
if (this.rootUri !== null && util.checkFileExistsSync(configFilePath)) {
258258
this.propertiesFile = vscode.Uri.file(configFilePath);
259259
} else {
260260
this.propertiesFile = null;
@@ -481,9 +481,9 @@ export class CppProperties {
481481
list.forEach((entry) => {
482482
if (entry !== "vcpkg") {
483483
const pathToCheck: string = path.join(vcpkgRoot, entry);
484-
if (fs.existsSync(pathToCheck)) {
484+
if (util.checkDirectoryExistsSync(pathToCheck)) {
485485
let p: string = path.join(pathToCheck, "include");
486-
if (fs.existsSync(p)) {
486+
if (util.checkDirectoryExistsSync(p)) {
487487
p = p.replace(/\\/g, "/");
488488
p = p.replace(vcpkgRoot, "${vcpkgRoot}");
489489
this.vcpkgIncludes.push(p);
@@ -1171,7 +1171,7 @@ export class CppProperties {
11711171
this.configurationJson.configurations.forEach(c => {
11721172
c.compileCommands?.forEach((path: string) => {
11731173
const compileCommandsFile: string = this.resolvePath(path);
1174-
if (fs.existsSync(compileCommandsFile)) {
1174+
if (util.checkFileExistsSync(compileCommandsFile)) {
11751175
filePaths.add(compileCommandsFile);
11761176
}
11771177
});
@@ -1672,10 +1672,10 @@ export class CppProperties {
16721672
if (!isCl && compilerPathAndArgs.compilerPath) {
16731673
const compilerPathMayNeedQuotes: boolean = !resolvedCompilerPath.startsWith('"') && resolvedCompilerPath.includes(" ") && compilerPathAndArgs.compilerArgsFromCommandLineInPath.length > 0;
16741674
let pathExists: boolean = true;
1675-
const existsWithExeAdded: (path: string) => boolean = (path: string) => isWindows && !path.startsWith("/") && fs.existsSync(path + ".exe");
1675+
const existsWithExeAdded: (path: string) => boolean = (path: string) => isWindows && !path.startsWith("/") && util.checkFileExistsSync(path + ".exe");
16761676

16771677
resolvedCompilerPath = compilerPathAndArgs.compilerPath;
1678-
if (!fs.existsSync(resolvedCompilerPath)) {
1678+
if (!util.checkFileExistsSync(resolvedCompilerPath)) {
16791679
if (existsWithExeAdded(resolvedCompilerPath)) {
16801680
resolvedCompilerPath += ".exe";
16811681
} else {
@@ -1686,7 +1686,7 @@ export class CppProperties {
16861686
} else if (rootUri) {
16871687
// Test if it was a relative path.
16881688
const absolutePath: string = rootUri.fsPath + path.sep + resolvedCompilerPath;
1689-
if (!fs.existsSync(absolutePath)) {
1689+
if (!util.checkFileExistsSync(absolutePath)) {
16901690
if (existsWithExeAdded(absolutePath)) {
16911691
resolvedCompilerPath = absolutePath + ".exe";
16921692
} else {

Extension/src/LanguageServer/editorConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import * as fs from 'fs';
88
import { Minimatch } from 'minimatch';
99
import * as path from 'path';
10+
import * as util from '../common';
1011
import { isWindows } from '../constants';
1112

1213
export const cachedEditorConfigSettings: Map<string, any> = new Map<string, any>();
@@ -144,7 +145,7 @@ function getEditorConfig(filePath: string): any {
144145
// Traverse from the file's directory to the root directory.
145146
for (; ;) {
146147
const editorConfigPath: string = path.join(currentDir, '.editorconfig');
147-
if (fs.existsSync(editorConfigPath)) {
148+
if (util.checkFileExistsSync(editorConfigPath)) {
148149
const configFileContent: string = fs.readFileSync(editorConfigPath, 'utf-8');
149150
const configData = parseEditorConfigContent(configFileContent);
150151

Extension/src/LanguageServer/settings.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
'use strict';
66

77
import { execSync } from 'child_process';
8-
import * as fs from 'fs';
98
import * as os from 'os';
109
import * as path from 'path';
1110
import * as semver from 'semver';
1211
import { quote } from 'shell-quote';
1312
import * as vscode from 'vscode';
1413
import * as nls from 'vscode-nls';
1514
import * as which from 'which';
15+
import * as util from '../common';
1616
import { getCachedClangFormatPath, getCachedClangTidyPath, getExtensionFilePath, getRawSetting, isArray, isArrayOfString, isBoolean, isNumber, isObject, isString, isValidMapping, setCachedClangFormatPath, setCachedClangTidyPath } from '../common';
1717
import { isWindows } from '../constants';
1818
import * as telemetry from '../telemetry';
@@ -925,7 +925,7 @@ export class CppSettings extends Settings {
925925
let foundEditorConfigWithVcFormatSettings: boolean = false;
926926
const findConfigFile: (parentPath: string) => boolean = (parentPath: string) => {
927927
const editorConfigPath: string = path.join(parentPath, ".editorconfig");
928-
if (fs.existsSync(editorConfigPath)) {
928+
if (util.checkFileExistsSync(editorConfigPath)) {
929929
const editorConfigSettings: any = getEditorConfigSettings(document.uri.fsPath);
930930
const keys: string[] = Object.keys(editorConfigSettings);
931931
for (let i: number = 0; i < keys.length; ++i) {
@@ -954,11 +954,11 @@ export class CppSettings extends Settings {
954954
}
955955
}
956956
const clangFormatPath1: string = path.join(parentPath, ".clang-format");
957-
if (fs.existsSync(clangFormatPath1)) {
957+
if (util.checkFileExistsSync(clangFormatPath1)) {
958958
return true;
959959
}
960960
const clangFormatPath2: string = path.join(parentPath, "_clang-format");
961-
return fs.existsSync(clangFormatPath2);
961+
return util.checkFileExistsSync(clangFormatPath2);
962962
};
963963
// Scan parent paths to see which we find first, ".clang-format" or ".editorconfig"
964964
const fsPath: string = document.uri.fsPath;

Extension/src/common.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ export function getVcpkgRoot(): string {
146146
if (!vcpkgRoot && vcpkgRoot !== "") {
147147
vcpkgRoot = "";
148148
// Check for vcpkg instance.
149-
if (fs.existsSync(getVcpkgPathDescriptorFile())) {
149+
if (checkFileExistsSync(getVcpkgPathDescriptorFile())) {
150150
let vcpkgRootTemp: string = fs.readFileSync(getVcpkgPathDescriptorFile()).toString();
151151
vcpkgRootTemp = vcpkgRootTemp.trim();
152-
if (fs.existsSync(vcpkgRootTemp)) {
152+
if (checkDirectoryExistsSync(vcpkgRootTemp)) {
153153
vcpkgRoot = path.join(vcpkgRootTemp, "/installed").replace(/\\/g, "/");
154154
}
155155
}

Extension/src/platform.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as fs from 'fs';
77
import * as os from 'os';
88
import * as plist from 'plist';
99
import * as nls from 'vscode-nls';
10+
import * as util from './common';
1011
import { LinuxDistribution } from './linuxDistribution';
1112
import * as logger from './logger';
1213
import { SessionState, SupportedWindowsVersions } from './sessionState';
@@ -68,7 +69,7 @@ export class PlatformInformation {
6869
let productDarwinVersion: string = "";
6970
let errorMessage: string = "";
7071

71-
if (fs.existsSync(DARWIN_SYSTEM_VERSION_PLIST)) {
72+
if (util.checkFileExistsSync(DARWIN_SYSTEM_VERSION_PLIST)) {
7273
const systemVersionPListBuffer: Buffer = fs.readFileSync(DARWIN_SYSTEM_VERSION_PLIST);
7374
const systemVersionData: any = plist.parse(systemVersionPListBuffer.toString());
7475
if (systemVersionData) {

0 commit comments

Comments
 (0)