From f9acc87b29278a26729cfcedef02e154fc2c5d85 Mon Sep 17 00:00:00 2001 From: Vasileios Naskos Date: Thu, 12 Dec 2024 14:58:17 +0100 Subject: [PATCH 1/2] SLVS-1669 Handle the case of C/C++ files outside the projects --- .../Analysis/SLCoreAnalyzerTests.cs | 14 ++++++++++---- src/SLCore/Analysis/SLCoreAnalyzer.cs | 14 ++++++++++++-- src/SLCore/SLCoreStrings.Designer.cs | 9 +++++++++ src/SLCore/SLCoreStrings.resx | 3 +++ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/SLCore.UnitTests/Analysis/SLCoreAnalyzerTests.cs b/src/SLCore.UnitTests/Analysis/SLCoreAnalyzerTests.cs index 95b2d145b..4a451040b 100644 --- a/src/SLCore.UnitTests/Analysis/SLCoreAnalyzerTests.cs +++ b/src/SLCore.UnitTests/Analysis/SLCoreAnalyzerTests.cs @@ -19,6 +19,7 @@ */ using NSubstitute.ExceptionExtensions; +using SonarLint.VisualStudio.Core; using SonarLint.VisualStudio.Core.Analysis; using SonarLint.VisualStudio.Core.CFamily; using SonarLint.VisualStudio.Core.ConfigurationScope; @@ -43,6 +44,7 @@ public class SLCoreAnalyzerTests private ICurrentTimeProvider currentTimeProvider; private IAggregatingCompilationDatabaseProvider compilationDatabaseLocator; private IAnalysisStatusNotifier notifier; + private ILogger logger; private SLCoreAnalyzer testSubject; [TestInitialize] @@ -58,11 +60,13 @@ public void TestInitialize() SetUpDefaultNotifier(); currentTimeProvider = Substitute.For(); compilationDatabaseLocator = Substitute.For(); + logger = new TestLogger(); testSubject = new SLCoreAnalyzer(slCoreServiceProvider, activeConfigScopeTracker, analysisStatusNotifierFactory, currentTimeProvider, - compilationDatabaseLocator); + compilationDatabaseLocator, + logger); void SetUpDefaultNotifier() => analysisStatusNotifierFactory.Create(nameof(SLCoreAnalyzer), FilePath, analysisId).Returns(notifier); } @@ -74,7 +78,8 @@ public void MefCtor_CheckIsExported() => MefTestHelpers.CreateExport(), MefTestHelpers.CreateExport(), MefTestHelpers.CreateExport(), - MefTestHelpers.CreateExport()); + MefTestHelpers.CreateExport(), + MefTestHelpers.CreateExport()); [TestMethod] public void MefCtor_CheckIsSingleton() => MefTestHelpers.CheckIsSingletonMefComponent(); @@ -206,7 +211,7 @@ public void ExecuteAnalysis_ForCFamily_AnalysisThrows_CompilationDatabaaseDispos } [TestMethod] - public void ExecuteAnalysis_ForCFamily_WithoutCompilationDatabase_DoesNotPassExtraProperty() + public void ExecuteAnalysis_ForCFamily_WithoutCompilationDatabase_PassesEmptyStringAsExtraProperty() { const string filePath = @"C:\file\path\myclass.cpp"; SetUpCompilationDatabaseLocator(filePath, null); @@ -216,7 +221,8 @@ public void ExecuteAnalysis_ForCFamily_WithoutCompilationDatabase_DoesNotPassExt analysisService.Received().AnalyzeFilesAndTrackAsync(Arg.Is(a => a.extraProperties != null - && !a.extraProperties.ContainsKey("sonar.cfamily.compile-commands")), + && a.extraProperties.ContainsKey("sonar.cfamily.compile-commands") + && a.extraProperties["sonar.cfamily.compile-commands"] == ""), Arg.Any()); } diff --git a/src/SLCore/Analysis/SLCoreAnalyzer.cs b/src/SLCore/Analysis/SLCoreAnalyzer.cs index d183fb126..49b087374 100644 --- a/src/SLCore/Analysis/SLCoreAnalyzer.cs +++ b/src/SLCore/Analysis/SLCoreAnalyzer.cs @@ -20,6 +20,7 @@ using System.ComponentModel.Composition; using Microsoft.VisualStudio.Threading; +using SonarLint.VisualStudio.Core; using SonarLint.VisualStudio.Core.Analysis; using SonarLint.VisualStudio.Core.CFamily; using SonarLint.VisualStudio.Core.ConfigurationScope; @@ -41,6 +42,7 @@ public class SLCoreAnalyzer : IAnalyzer private readonly IAnalysisStatusNotifierFactory analysisStatusNotifierFactory; private readonly ICurrentTimeProvider currentTimeProvider; private readonly IAggregatingCompilationDatabaseProvider compilationDatabaseLocator; + private readonly ILogger logger; [ImportingConstructor] public SLCoreAnalyzer( @@ -48,13 +50,15 @@ public SLCoreAnalyzer( IActiveConfigScopeTracker activeConfigScopeTracker, IAnalysisStatusNotifierFactory analysisStatusNotifierFactory, ICurrentTimeProvider currentTimeProvider, - IAggregatingCompilationDatabaseProvider compilationDatabaseLocator) + IAggregatingCompilationDatabaseProvider compilationDatabaseLocator, + ILogger logger) { this.serviceProvider = serviceProvider; this.activeConfigScopeTracker = activeConfigScopeTracker; this.analysisStatusNotifierFactory = analysisStatusNotifierFactory; this.currentTimeProvider = currentTimeProvider; this.compilationDatabaseLocator = compilationDatabaseLocator; + this.logger = logger; } public bool IsAnalysisSupported(IEnumerable languages) => true; @@ -134,7 +138,13 @@ private IDisposable EnrichPropertiesForCFamily(Dictionary proper } var compilationDatabaseHandle = compilationDatabaseLocator.GetOrNull(path); - if (compilationDatabaseHandle != null) + if (compilationDatabaseHandle == null) + { + logger.WriteLine(SLCoreStrings.CompilationDatabaseNotFound, path); + // Pass empty compilation database path in order to get a more helpful message and not break the analyzer + properties[CFamilyCompileCommandsProperty] = ""; + } + else { properties[CFamilyCompileCommandsProperty] = compilationDatabaseHandle.FilePath; } diff --git a/src/SLCore/SLCoreStrings.Designer.cs b/src/SLCore/SLCoreStrings.Designer.cs index 940e845c0..eaabdca2e 100644 --- a/src/SLCore/SLCoreStrings.Designer.cs +++ b/src/SLCore/SLCoreStrings.Designer.cs @@ -95,6 +95,15 @@ public static string CertificateValidator_FailureReasonTemplate { } } + /// + /// Looks up a localized string similar to [SLCoreAnalyzer] No compilation database found for file: {0}. + /// + public static string CompilationDatabaseNotFound { + get { + return ResourceManager.GetString("CompilationDatabaseNotFound", resourceCulture); + } + } + /// /// Looks up a localized string similar to Configuration scope conflict. /// diff --git a/src/SLCore/SLCoreStrings.resx b/src/SLCore/SLCoreStrings.resx index d31c4ad3e..bcee6ab9c 100644 --- a/src/SLCore/SLCoreStrings.resx +++ b/src/SLCore/SLCoreStrings.resx @@ -177,4 +177,7 @@ [CertificateChainValidator] {0}: {1} + + [SLCoreAnalyzer] No compilation database found for file: {0} + \ No newline at end of file From 5781e233be86a8618cc3a72eea19e2abb4c31f33 Mon Sep 17 00:00:00 2001 From: Vasileios Naskos Date: Fri, 13 Dec 2024 12:29:26 +0100 Subject: [PATCH 2/2] Add hint that the file is not part of the solution --- src/SLCore/SLCoreStrings.Designer.cs | 2 +- src/SLCore/SLCoreStrings.resx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SLCore/SLCoreStrings.Designer.cs b/src/SLCore/SLCoreStrings.Designer.cs index eaabdca2e..b30a31447 100644 --- a/src/SLCore/SLCoreStrings.Designer.cs +++ b/src/SLCore/SLCoreStrings.Designer.cs @@ -96,7 +96,7 @@ public static string CertificateValidator_FailureReasonTemplate { } /// - /// Looks up a localized string similar to [SLCoreAnalyzer] No compilation database found for file: {0}. + /// Looks up a localized string similar to [SLCoreAnalyzer] No compilation database found for file: {0}. Check that the file is part of a supported project type in the current solution.. /// public static string CompilationDatabaseNotFound { get { diff --git a/src/SLCore/SLCoreStrings.resx b/src/SLCore/SLCoreStrings.resx index bcee6ab9c..52774ce0c 100644 --- a/src/SLCore/SLCoreStrings.resx +++ b/src/SLCore/SLCoreStrings.resx @@ -178,6 +178,6 @@ [CertificateChainValidator] {0}: {1} - [SLCoreAnalyzer] No compilation database found for file: {0} + [SLCoreAnalyzer] No compilation database found for file: {0}. Check that the file is part of a supported project type in the current solution. \ No newline at end of file