From 779e65bbdb3ccd72d6623d7e95380f347f4d9817 Mon Sep 17 00:00:00 2001 From: Sean McManus Date: Tue, 10 Mar 2026 09:00:45 -0700 Subject: [PATCH] Change fs.existsSync to check[File|Directory]ExistsSync for cases where it matters. --- Extension/src/Debugger/configurationProvider.ts | 7 +++---- Extension/src/LanguageServer/client.ts | 4 ++-- Extension/src/LanguageServer/configurations.ts | 14 +++++++------- Extension/src/LanguageServer/editorConfig.ts | 3 ++- Extension/src/LanguageServer/settings.ts | 8 ++++---- Extension/src/common.ts | 4 ++-- Extension/src/platform.ts | 3 ++- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Extension/src/Debugger/configurationProvider.ts b/Extension/src/Debugger/configurationProvider.ts index 7786594e8..8f0ae0c7c 100644 --- a/Extension/src/Debugger/configurationProvider.ts +++ b/Extension/src/Debugger/configurationProvider.ts @@ -4,7 +4,6 @@ * ------------------------------------------------------------------------------------------ */ import * as jsonc from 'comment-json'; -import * as fs from 'fs'; import * as glob from 'glob'; import * as os from 'os'; import * as path from 'path'; @@ -22,8 +21,8 @@ import * as logger from '../logger'; import { PlatformInformation } from '../platform'; import { rsync, scp, ssh } from '../SSH/commands'; import * as Telemetry from '../telemetry'; -import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess'; import { AttachItem, showQuickPick } from './attachQuickPick'; +import { AttachItemsProvider, AttachPicker, RemoteAttachPicker } from './attachToProcess'; import { ConfigMenu, ConfigMode, ConfigSource, CppDebugConfiguration, DebuggerEvent, DebuggerType, DebugType, IConfiguration, IConfigurationSnippet, isDebugLaunchStr, MIConfigurations, PipeTransportConfigurations, TaskStatus, WindowsConfigurations, WSLConfigurations } from './configurations'; import { NativeAttachItemsProviderFactory } from './nativeAttach'; import { Environment, ParsedEnvironmentFile } from './ParsedEnvironmentFile'; @@ -303,7 +302,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv // Validate LLDB-MI if (os.platform() === 'darwin' && // Check for macOS - fs.existsSync(lldb_mi_10_x_path) && // lldb-mi 10.x exists + util.checkFileExistsSync(lldb_mi_10_x_path) && // lldb-mi 10.x exists (!macOSMIMode || macOSMIMode === 'lldb') && !macOSMIDebuggerPath // User did not provide custom lldb-mi ) { @@ -657,7 +656,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv ]; for (const searchPath of searchPaths) { - if (fs.existsSync(path.join(searchPath, LLDBFramework))) { + if (util.checkDirectoryExistsSync(path.join(searchPath, LLDBFramework))) { // Found a framework that 'lldb-mi' can use. return searchPath; } diff --git a/Extension/src/LanguageServer/client.ts b/Extension/src/LanguageServer/client.ts index 1cc7e087c..9fd268013 100644 --- a/Extension/src/LanguageServer/client.ts +++ b/Extension/src/LanguageServer/client.ts @@ -1285,7 +1285,7 @@ export class DefaultClient implements Client { } this.rootFolder = workspaceFolder; - this.rootRealPath = this.RootPath ? fs.existsSync(this.RootPath) ? fs.realpathSync(this.RootPath) : this.RootPath : ""; + this.rootRealPath = this.RootPath ? util.checkDirectoryExistsSync(this.RootPath) ? fs.realpathSync(this.RootPath) : this.RootPath : ""; this.workspaceStoragePath = util.extensionContext?.storageUri?.fsPath ?? ""; if (this.workspaceStoragePath.length > 0) { @@ -1612,7 +1612,7 @@ export class DefaultClient implements Client { this.currentCaseSensitiveFileSupport = new PersistentWorkspaceState("CPP.currentCaseSensitiveFileSupport", false); let resetDatabase: boolean = false; const serverModule: string = getLanguageServerFileName(); - const exeExists: boolean = fs.existsSync(serverModule); + const exeExists: boolean = util.checkFileExistsSync(serverModule); if (!exeExists) { telemetry.logLanguageServerEvent("missingLanguageServerBinary"); throw String('Missing binary at ' + serverModule); diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index 6f3c1588a..814ccb41a 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -254,7 +254,7 @@ export class CppProperties { // to the language server until the default include paths and frameworks have been sent. const configFilePath: string = path.join(this.configFolder, "c_cpp_properties.json"); - if (this.rootUri !== null && fs.existsSync(configFilePath)) { + if (this.rootUri !== null && util.checkFileExistsSync(configFilePath)) { this.propertiesFile = vscode.Uri.file(configFilePath); } else { this.propertiesFile = null; @@ -481,9 +481,9 @@ export class CppProperties { list.forEach((entry) => { if (entry !== "vcpkg") { const pathToCheck: string = path.join(vcpkgRoot, entry); - if (fs.existsSync(pathToCheck)) { + if (util.checkDirectoryExistsSync(pathToCheck)) { let p: string = path.join(pathToCheck, "include"); - if (fs.existsSync(p)) { + if (util.checkDirectoryExistsSync(p)) { p = p.replace(/\\/g, "/"); p = p.replace(vcpkgRoot, "${vcpkgRoot}"); this.vcpkgIncludes.push(p); @@ -1171,7 +1171,7 @@ export class CppProperties { this.configurationJson.configurations.forEach(c => { c.compileCommands?.forEach((path: string) => { const compileCommandsFile: string = this.resolvePath(path); - if (fs.existsSync(compileCommandsFile)) { + if (util.checkFileExistsSync(compileCommandsFile)) { filePaths.add(compileCommandsFile); } }); @@ -1672,10 +1672,10 @@ export class CppProperties { if (!isCl && compilerPathAndArgs.compilerPath) { const compilerPathMayNeedQuotes: boolean = !resolvedCompilerPath.startsWith('"') && resolvedCompilerPath.includes(" ") && compilerPathAndArgs.compilerArgsFromCommandLineInPath.length > 0; let pathExists: boolean = true; - const existsWithExeAdded: (path: string) => boolean = (path: string) => isWindows && !path.startsWith("/") && fs.existsSync(path + ".exe"); + const existsWithExeAdded: (path: string) => boolean = (path: string) => isWindows && !path.startsWith("/") && util.checkFileExistsSync(path + ".exe"); resolvedCompilerPath = compilerPathAndArgs.compilerPath; - if (!fs.existsSync(resolvedCompilerPath)) { + if (!util.checkFileExistsSync(resolvedCompilerPath)) { if (existsWithExeAdded(resolvedCompilerPath)) { resolvedCompilerPath += ".exe"; } else { @@ -1686,7 +1686,7 @@ export class CppProperties { } else if (rootUri) { // Test if it was a relative path. const absolutePath: string = rootUri.fsPath + path.sep + resolvedCompilerPath; - if (!fs.existsSync(absolutePath)) { + if (!util.checkFileExistsSync(absolutePath)) { if (existsWithExeAdded(absolutePath)) { resolvedCompilerPath = absolutePath + ".exe"; } else { diff --git a/Extension/src/LanguageServer/editorConfig.ts b/Extension/src/LanguageServer/editorConfig.ts index 4f330073a..4cccd367f 100644 --- a/Extension/src/LanguageServer/editorConfig.ts +++ b/Extension/src/LanguageServer/editorConfig.ts @@ -7,6 +7,7 @@ import * as fs from 'fs'; import { Minimatch } from 'minimatch'; import * as path from 'path'; +import * as util from '../common'; import { isWindows } from '../constants'; export const cachedEditorConfigSettings: Map = new Map(); @@ -144,7 +145,7 @@ function getEditorConfig(filePath: string): any { // Traverse from the file's directory to the root directory. for (; ;) { const editorConfigPath: string = path.join(currentDir, '.editorconfig'); - if (fs.existsSync(editorConfigPath)) { + if (util.checkFileExistsSync(editorConfigPath)) { const configFileContent: string = fs.readFileSync(editorConfigPath, 'utf-8'); const configData = parseEditorConfigContent(configFileContent); diff --git a/Extension/src/LanguageServer/settings.ts b/Extension/src/LanguageServer/settings.ts index 3ee17d794..881423789 100644 --- a/Extension/src/LanguageServer/settings.ts +++ b/Extension/src/LanguageServer/settings.ts @@ -5,7 +5,6 @@ 'use strict'; import { execSync } from 'child_process'; -import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; import * as semver from 'semver'; @@ -13,6 +12,7 @@ import { quote } from 'shell-quote'; import * as vscode from 'vscode'; import * as nls from 'vscode-nls'; import * as which from 'which'; +import * as util from '../common'; import { getCachedClangFormatPath, getCachedClangTidyPath, getExtensionFilePath, getRawSetting, isArray, isArrayOfString, isBoolean, isNumber, isObject, isString, isValidMapping, setCachedClangFormatPath, setCachedClangTidyPath } from '../common'; import { isWindows } from '../constants'; import * as telemetry from '../telemetry'; @@ -925,7 +925,7 @@ export class CppSettings extends Settings { let foundEditorConfigWithVcFormatSettings: boolean = false; const findConfigFile: (parentPath: string) => boolean = (parentPath: string) => { const editorConfigPath: string = path.join(parentPath, ".editorconfig"); - if (fs.existsSync(editorConfigPath)) { + if (util.checkFileExistsSync(editorConfigPath)) { const editorConfigSettings: any = getEditorConfigSettings(document.uri.fsPath); const keys: string[] = Object.keys(editorConfigSettings); for (let i: number = 0; i < keys.length; ++i) { @@ -954,11 +954,11 @@ export class CppSettings extends Settings { } } const clangFormatPath1: string = path.join(parentPath, ".clang-format"); - if (fs.existsSync(clangFormatPath1)) { + if (util.checkFileExistsSync(clangFormatPath1)) { return true; } const clangFormatPath2: string = path.join(parentPath, "_clang-format"); - return fs.existsSync(clangFormatPath2); + return util.checkFileExistsSync(clangFormatPath2); }; // Scan parent paths to see which we find first, ".clang-format" or ".editorconfig" const fsPath: string = document.uri.fsPath; diff --git a/Extension/src/common.ts b/Extension/src/common.ts index 9ee2cb46c..849f4d6f3 100644 --- a/Extension/src/common.ts +++ b/Extension/src/common.ts @@ -146,10 +146,10 @@ export function getVcpkgRoot(): string { if (!vcpkgRoot && vcpkgRoot !== "") { vcpkgRoot = ""; // Check for vcpkg instance. - if (fs.existsSync(getVcpkgPathDescriptorFile())) { + if (checkFileExistsSync(getVcpkgPathDescriptorFile())) { let vcpkgRootTemp: string = fs.readFileSync(getVcpkgPathDescriptorFile()).toString(); vcpkgRootTemp = vcpkgRootTemp.trim(); - if (fs.existsSync(vcpkgRootTemp)) { + if (checkDirectoryExistsSync(vcpkgRootTemp)) { vcpkgRoot = path.join(vcpkgRootTemp, "/installed").replace(/\\/g, "/"); } } diff --git a/Extension/src/platform.ts b/Extension/src/platform.ts index 5b5e3c1ab..09ddce3e9 100644 --- a/Extension/src/platform.ts +++ b/Extension/src/platform.ts @@ -7,6 +7,7 @@ import * as fs from 'fs'; import * as os from 'os'; import * as plist from 'plist'; import * as nls from 'vscode-nls'; +import * as util from './common'; import { LinuxDistribution } from './linuxDistribution'; import * as logger from './logger'; import { SessionState, SupportedWindowsVersions } from './sessionState'; @@ -68,7 +69,7 @@ export class PlatformInformation { let productDarwinVersion: string = ""; let errorMessage: string = ""; - if (fs.existsSync(DARWIN_SYSTEM_VERSION_PLIST)) { + if (util.checkFileExistsSync(DARWIN_SYSTEM_VERSION_PLIST)) { const systemVersionPListBuffer: Buffer = fs.readFileSync(DARWIN_SYSTEM_VERSION_PLIST); const systemVersionData: any = plist.parse(systemVersionPListBuffer.toString()); if (systemVersionData) {