From 39e1cf78943f8394058fac1969bf991479f3aed9 Mon Sep 17 00:00:00 2001 From: cx-Margarita-LevitM Date: Tue, 28 Oct 2025 20:36:15 +0200 Subject: [PATCH 1/2] Improve Visual Studio Plugin Behavior When No Projects Are Detected in Solution (AST-AST-116483) --- .../CxExtension/Toolbar/CxToolbar.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs b/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs index 6009b7f1..ef804c2b 100644 --- a/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs +++ b/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs @@ -50,6 +50,35 @@ internal class CxToolbar public Func, Dictionary> CreateStateMenuItems { get; set; } private static bool initPolling = false; + + public static bool IsValidSourceProject(string sourcePath) + { + if (string.IsNullOrEmpty(sourcePath)) + { + return false; + } + + try + { + string directoryPath = System.IO.Path.GetDirectoryName(sourcePath); + if (string.IsNullOrEmpty(directoryPath)) + { + return false; + } + + bool hasProjectFiles = false; + + hasProjectFiles |= System.IO.Directory.GetFiles(directoryPath, "*.sln", System.IO.SearchOption.TopDirectoryOnly).Any(); + hasProjectFiles |= System.IO.Directory.GetFiles(directoryPath, "*.csproj", System.IO.SearchOption.TopDirectoryOnly).Any(); + + return hasProjectFiles; + } + catch (Exception ex) + { + UpdateStatusBar("Checkmarx: Error validating project directory" + ex.Message); + return false; + } + } private const string DevOrTestFilterName = "SCA Dev & Test Dependencies"; @@ -286,6 +315,13 @@ public async Task ScanStart_ClickAsync() return; } + if (!IsValidSourceProject(dte.Solution.FullName)) + { + CxUtils.DisplayMessageInInfoWithLinkBar(Package, "No valid project found in directory", KnownMonikers.StatusError, "Project Error", "", false); + ScanStartButton.IsEnabled = true; + return; + } + var currentGitBranch = await GetCurrentGitBranchAsync(dte); var checkmarxBranch = SettingsUtils.GetToolbarValue(Package, SettingsUtils.branchProperty); var matchProject = await ASTProjectMatchesWorkspaceProjectAsync(dte); From a369dc9dec25184be4d557c5aa8fed57eb754a37 Mon Sep 17 00:00:00 2001 From: cx-Margarita-LevitM Date: Tue, 4 Nov 2025 16:02:21 +0200 Subject: [PATCH 2/2] Improve Visual Studio Plugin Behavior When No Projects Are Detected in Solution (AST-AST-116483) --- .../CxExtension/Toolbar/CxToolbar.cs | 43 ++++++++++++++++--- .../CxExtension/Utils/CxConstants.cs | 4 +- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs b/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs index ef804c2b..99d141bd 100644 --- a/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs +++ b/ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs @@ -60,18 +60,30 @@ public static bool IsValidSourceProject(string sourcePath) try { - string directoryPath = System.IO.Path.GetDirectoryName(sourcePath); - if (string.IsNullOrEmpty(directoryPath)) + string searchPath; + if (System.IO.File.Exists(sourcePath)) + { + searchPath = System.IO.Path.GetDirectoryName(sourcePath); + } + else if (System.IO.Directory.Exists(sourcePath)) + { + searchPath = sourcePath; + } + else { return false; } - bool hasProjectFiles = false; + string[] projectExtensions = { "*.sln", "*.csproj", "*.vbproj", "*.fsproj", "*.vcxproj" }; - hasProjectFiles |= System.IO.Directory.GetFiles(directoryPath, "*.sln", System.IO.SearchOption.TopDirectoryOnly).Any(); - hasProjectFiles |= System.IO.Directory.GetFiles(directoryPath, "*.csproj", System.IO.SearchOption.TopDirectoryOnly).Any(); + foreach (string extension in projectExtensions) + { + var files = System.IO.Directory.GetFiles(searchPath, extension, System.IO.SearchOption.AllDirectories); + if (files.Any(file => IsValidProjectFile(file))) + return true; + } - return hasProjectFiles; + return false; } catch (Exception ex) { @@ -79,6 +91,20 @@ public static bool IsValidSourceProject(string sourcePath) return false; } } + + + private static bool IsValidProjectFile(string filePath) + { + try + { + var fileInfo = new System.IO.FileInfo(filePath); + return fileInfo.Exists && fileInfo.Length > 0; + } + catch + { + return false; + } + } private const string DevOrTestFilterName = "SCA Dev & Test Dependencies"; @@ -317,7 +343,7 @@ public async Task ScanStart_ClickAsync() if (!IsValidSourceProject(dte.Solution.FullName)) { - CxUtils.DisplayMessageInInfoWithLinkBar(Package, "No valid project found in directory", KnownMonikers.StatusError, "Project Error", "", false); + CxUtils.DisplayMessageInInfoWithLinkBar(Package, CxConstants.NOT_A_VALID_PROJECT, KnownMonikers.StatusError, "Project Error", "", false); ScanStartButton.IsEnabled = true; return; } @@ -358,6 +384,9 @@ private static async Task GetCurrentGitBranchAsync(EnvDTE.DTE dte) try { string workingDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName); + if (string.IsNullOrEmpty(workingDir) || !System.IO.Directory.Exists(workingDir)) + return null; + RepositoryInformation repository = RepositoryInformation.GetRepositoryInformation(workingDir); if (repository == null) diff --git a/ast-visual-studio-extension/CxExtension/Utils/CxConstants.cs b/ast-visual-studio-extension/CxExtension/Utils/CxConstants.cs index 39b377ab..2697cc66 100644 --- a/ast-visual-studio-extension/CxExtension/Utils/CxConstants.cs +++ b/ast-visual-studio-extension/CxExtension/Utils/CxConstants.cs @@ -93,7 +93,9 @@ internal class CxConstants public static string PROJECT_AND_BRANCH_DO_NOT_MATCH => "The Git branch and files open in your workspace don't match the branch and project that were previously scanned in this Checkmarx project. Do you want to scan anyway?"; public static string RUN_SCAN => "Run scan"; public static string RUN_SCAN_ACTION => "RUN_SCAN_ACTION"; - + public static string NOT_A_VALID_PROJECT => "No.NET project files(.sln, .csproj, .vbproj, .fsproj, .vcxproj) found in directory"; + + /** LEARN MORE AND REMEDIATION **/ public static string CODE_SAMPLE_TITLE => "{0} using {1}"; public static string NO_INFORMATION => "No information";