diff --git a/src/Build/BuildCheck/Acquisition/BuildCheckAcquisitionModule.cs b/src/Build/BuildCheck/Acquisition/BuildCheckAcquisitionModule.cs index 1d2ff00841e..e8618ca5b9a 100644 --- a/src/Build/BuildCheck/Acquisition/BuildCheckAcquisitionModule.cs +++ b/src/Build/BuildCheck/Acquisition/BuildCheckAcquisitionModule.cs @@ -42,18 +42,17 @@ public List CreateBuildAnalyzerFactories(AnalyzerAcquisiti assembly = Assembly.LoadFrom(analyzerAcquisitionData.AssemblyPath); #endif - IEnumerable analyzerTypes = assembly.GetExportedTypes().Where(t => typeof(BuildAnalyzer).IsAssignableFrom(t)); + IList availableTypes = assembly.GetExportedTypes(); + IList analyzerTypes = availableTypes.Where(t => typeof(BuildAnalyzer).IsAssignableFrom(t)).ToArray(); - foreach (Type analyzerType in analyzerTypes) + foreach (Type analyzerCandidate in analyzerTypes) { - if (Activator.CreateInstance(analyzerType) is BuildAnalyzer instance) - { - analyzersFactories.Add(() => instance); - } - else - { - throw new InvalidOperationException($"Failed to create an instance of type {analyzerType.FullName} as BuildAnalyzer."); - } + analyzersFactories.Add(() => (BuildAnalyzer)Activator.CreateInstance(analyzerCandidate)!); + } + + if (availableTypes.Count != analyzerTypes.Count) + { + availableTypes.Except(analyzerTypes).ToList().ForEach(t => _loggingService.LogComment(buildEventContext, MessageImportance.Normal, "CustomAnalyzerBaseTypeNotAssignable", t.Name, t.Assembly)); } } catch (ReflectionTypeLoadException ex) diff --git a/src/Build/Resources/Strings.resx b/src/Build/Resources/Strings.resx index 62bcd2464c3..b0580578857 100644 --- a/src/Build/Resources/Strings.resx +++ b/src/Build/Resources/Strings.resx @@ -2111,6 +2111,10 @@ Utilization: {0} Average Utilization: {1:###.0} Failed to find the specified custom analyzer assembly: {0}. Please check if it exists. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Task assembly was loaded from '{0}' while the desired location was '{1}'. diff --git a/src/Build/Resources/xlf/Strings.cs.xlf b/src/Build/Resources/xlf/Strings.cs.xlf index cce18578077..93916154ffe 100644 --- a/src/Build/Resources/xlf/Strings.cs.xlf +++ b/src/Build/Resources/xlf/Strings.cs.xlf @@ -212,6 +212,11 @@ Nepodařilo se najít zadané sestavení vlastního analyzátoru: {0}. Zkontrolujte prosím, jestli existuje. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Nepodařilo se zaregistrovat vlastní analyzátor: {0}. diff --git a/src/Build/Resources/xlf/Strings.de.xlf b/src/Build/Resources/xlf/Strings.de.xlf index d1aa6cbc8ec..818a517cb33 100644 --- a/src/Build/Resources/xlf/Strings.de.xlf +++ b/src/Build/Resources/xlf/Strings.de.xlf @@ -212,6 +212,11 @@ Fehler beim Suchen der angegebenen benutzerdefinierten Analysetoolassembly: {0}. Überprüfen Sie, ob sie vorhanden ist. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Fehler beim Registrieren des benutzerdefinierten Analysetools: {0}. diff --git a/src/Build/Resources/xlf/Strings.es.xlf b/src/Build/Resources/xlf/Strings.es.xlf index 90914c626ff..957d7a00204 100644 --- a/src/Build/Resources/xlf/Strings.es.xlf +++ b/src/Build/Resources/xlf/Strings.es.xlf @@ -212,6 +212,11 @@ No se ha podido encontrar el ensamblado del analizador personalizado especificado: {0}. Compruebe si existe. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. No se ha podido registrar el analizador personalizado: {0}. diff --git a/src/Build/Resources/xlf/Strings.fr.xlf b/src/Build/Resources/xlf/Strings.fr.xlf index 09bad2f7e80..fda6250b5b2 100644 --- a/src/Build/Resources/xlf/Strings.fr.xlf +++ b/src/Build/Resources/xlf/Strings.fr.xlf @@ -212,6 +212,11 @@ Impossible de trouver l’assemblée d'analyseur personnalisé spécifié : {0}. Vérifiez s’il existe. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Échec de l’inscription de l’analyseur personnalisé : {0}. diff --git a/src/Build/Resources/xlf/Strings.it.xlf b/src/Build/Resources/xlf/Strings.it.xlf index 8bf8eec2d8d..01587bcd09a 100644 --- a/src/Build/Resources/xlf/Strings.it.xlf +++ b/src/Build/Resources/xlf/Strings.it.xlf @@ -212,6 +212,11 @@ Impossibile trovare l'assembly dell'analizzatore personalizzato specificato: {0}. Verificare se esiste. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Impossibile registrare l'analizzatore personalizzato: {0}. diff --git a/src/Build/Resources/xlf/Strings.ja.xlf b/src/Build/Resources/xlf/Strings.ja.xlf index fbdc02d0590..57f57436280 100644 --- a/src/Build/Resources/xlf/Strings.ja.xlf +++ b/src/Build/Resources/xlf/Strings.ja.xlf @@ -212,6 +212,11 @@ 指定されたカスタム アナライザー アセンブリが見つかりませんでした: {0}。存在するかどうか確認してください。 The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. カスタム アナライザーを登録できませんでした: {0} diff --git a/src/Build/Resources/xlf/Strings.ko.xlf b/src/Build/Resources/xlf/Strings.ko.xlf index dc7c94b9d2f..2a1f32aacdb 100644 --- a/src/Build/Resources/xlf/Strings.ko.xlf +++ b/src/Build/Resources/xlf/Strings.ko.xlf @@ -212,6 +212,11 @@ 지정한 사용자 지정 분석기 어셈블리를 찾지 못했습니다. {0}. 존재하는지 확인하세요. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. 사용자 지정 분석기를 등록하지 못했습니다. {0}. diff --git a/src/Build/Resources/xlf/Strings.pl.xlf b/src/Build/Resources/xlf/Strings.pl.xlf index d8d09cb9c63..e1648a86f99 100644 --- a/src/Build/Resources/xlf/Strings.pl.xlf +++ b/src/Build/Resources/xlf/Strings.pl.xlf @@ -212,6 +212,11 @@ Nie można odnaleźć określonego zestawu analizatora niestandardowego: {0}. Sprawdź, czy istnieje. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Nie można zarejestrować analizatora niestandardowego: {0}. diff --git a/src/Build/Resources/xlf/Strings.pt-BR.xlf b/src/Build/Resources/xlf/Strings.pt-BR.xlf index 389700299cc..093bc053d2b 100644 --- a/src/Build/Resources/xlf/Strings.pt-BR.xlf +++ b/src/Build/Resources/xlf/Strings.pt-BR.xlf @@ -212,6 +212,11 @@ Falha ao localizar o assembly do analisador personalizado especificado: {0}. Verifique se existe. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Falha ao registrar o analisador personalizado: {0}. diff --git a/src/Build/Resources/xlf/Strings.ru.xlf b/src/Build/Resources/xlf/Strings.ru.xlf index 5c04d7194eb..6b0cb9620f8 100644 --- a/src/Build/Resources/xlf/Strings.ru.xlf +++ b/src/Build/Resources/xlf/Strings.ru.xlf @@ -212,6 +212,11 @@ Не удалось найти указанную сборку настраиваемого анализатора: {0}. Убедитесь, что она существует. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Не удалось зарегистрировать настраиваемый анализатор: {0}. diff --git a/src/Build/Resources/xlf/Strings.tr.xlf b/src/Build/Resources/xlf/Strings.tr.xlf index a9dc8a0e065..112adf2dc9d 100644 --- a/src/Build/Resources/xlf/Strings.tr.xlf +++ b/src/Build/Resources/xlf/Strings.tr.xlf @@ -212,6 +212,11 @@ Belirtilen {0} özel çözümleyici derlemesi bulunamadı. Lütfen var olup olmadığını kontrol edin. The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. Özel çözümleyici kaydedilemedi: {0}. diff --git a/src/Build/Resources/xlf/Strings.zh-Hans.xlf b/src/Build/Resources/xlf/Strings.zh-Hans.xlf index 868e495f52b..395891c7c04 100644 --- a/src/Build/Resources/xlf/Strings.zh-Hans.xlf +++ b/src/Build/Resources/xlf/Strings.zh-Hans.xlf @@ -212,6 +212,11 @@ 找不到指定的自定义分析器程序集: {0}。请检查它是否存在。 The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. 无法注册自定义分析器: {0}。 diff --git a/src/Build/Resources/xlf/Strings.zh-Hant.xlf b/src/Build/Resources/xlf/Strings.zh-Hant.xlf index b31a9ad4517..0eaa13e2787 100644 --- a/src/Build/Resources/xlf/Strings.zh-Hant.xlf +++ b/src/Build/Resources/xlf/Strings.zh-Hant.xlf @@ -212,6 +212,11 @@ 找不到指定的自訂分析器組件: {0}。請檢查它是否存在。 The message is emitted when the custom analyzer assembly can not be found. + + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + Failed to load the custom analyzer type: {0} from the assembly: {1}. Make sure it inherits the Microsoft.Build.Experimental.BuildCheck.BuildAnalyzer base class. If it is not intended to be a custom analyzer, than it should not be exposed. More info: https://github.com/dotnet/msbuild/blob/main/documentation/specs/proposed/BuildCheck-Architecture.md#acquisition + The message is emitted when the custom analyzer assembly can not be successfully registered. + Failed to register the custom analyzer: {0}. 無法登錄自訂分析器: {0}。 diff --git a/src/BuildCheck.UnitTests/EndToEndTests.cs b/src/BuildCheck.UnitTests/EndToEndTests.cs index cc2aa1ae612..07a6b55388d 100644 --- a/src/BuildCheck.UnitTests/EndToEndTests.cs +++ b/src/BuildCheck.UnitTests/EndToEndTests.cs @@ -2,12 +2,11 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Collections.Generic; using System.IO; using System.Xml; +using Microsoft.Build.Shared; using Microsoft.Build.UnitTests; using Microsoft.Build.UnitTests.Shared; -using Newtonsoft.Json.Linq; using Shouldly; using Xunit; using Xunit.Abstractions; @@ -26,7 +25,9 @@ public EndToEndTests(ITestOutputHelper output) _env.WithEnvironmentInvariant(); } - private static string TestAssetsRootPath { get; } = Path.Combine(Path.GetDirectoryName(typeof(EndToEndTests).Assembly.Location) ?? AppContext.BaseDirectory, "TestAssets"); + private static string AssemblyLocation { get; } = Path.Combine(Path.GetDirectoryName(typeof(EndToEndTests).Assembly.Location) ?? AppContext.BaseDirectory); + + private static string TestAssetsRootPath { get; } = Path.Combine(AssemblyLocation, "TestAssets"); public void Dispose() => _env.Dispose(); @@ -136,53 +137,33 @@ public void SampleAnalyzerIntegrationTest(bool buildInOutOfProcessNode, bool ana } [Theory] - [InlineData(new[] { "CustomAnalyzer" }, "AnalysisCandidate", new[] { "CustomRule1", "CustomRule2" })] - [InlineData(new[] { "CustomAnalyzer", "CustomAnalyzer2" }, "AnalysisCandidateWithMultipleAnalyzersInjected", new[] { "CustomRule1", "CustomRule2", "CustomRule3" })] - public void CustomAnalyzerTest(string[] customAnalyzerNames, string analysisCandidate, string[] expectedRegisteredRules) + [InlineData("AnalysisCandidate", new[] { "CustomRule1", "CustomRule2" })] + [InlineData("AnalysisCandidateWithMultipleAnalyzersInjected", new[] { "CustomRule1", "CustomRule2", "CustomRule3" }, true)] + public void CustomAnalyzerTest(string analysisCandidate, string[] expectedRegisteredRules, bool expectedRejectedAnalyzers = false) { using (var env = TestEnvironment.Create()) { - var candidatesNugetFullPaths = BuildAnalyzerRules(env, customAnalyzerNames); - - candidatesNugetFullPaths.ShouldNotBeEmpty("Nuget package with custom analyzer was not generated or detected."); - var analysisCandidatePath = Path.Combine(TestAssetsRootPath, analysisCandidate); - AddCustomDataSourceToNugetConfig(analysisCandidatePath, candidatesNugetFullPaths); + AddCustomDataSourceToNugetConfig(analysisCandidatePath); string projectAnalysisBuildLog = RunnerUtilities.ExecBootstrapedMSBuild( $"{Path.Combine(analysisCandidatePath, $"{analysisCandidate}.csproj")} /m:1 -nr:False -restore /p:OutputPath={env.CreateFolder().Path} -analyze -verbosity:d", out bool successBuild); - successBuild.ShouldBeTrue(); + successBuild.ShouldBeTrue(projectAnalysisBuildLog); - foreach (string expectedRegisteredRule in expectedRegisteredRules) + foreach (string registeredRule in expectedRegisteredRules) { - projectAnalysisBuildLog.ShouldContain($"Custom analyzer rule: {expectedRegisteredRule} has been registered successfully."); + projectAnalysisBuildLog.ShouldContain(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("CustomAnalyzerSuccessfulAcquisition", registeredRule)); } - } - } - - private IList BuildAnalyzerRules(TestEnvironment env, string[] customAnalyzerNames) - { - var candidatesNugetFullPaths = new List(); - - foreach (var customAnalyzerName in customAnalyzerNames) - { - var candidateAnalysisProjectPath = Path.Combine(TestAssetsRootPath, customAnalyzerName, $"{customAnalyzerName}.csproj"); - var nugetPackResults = RunnerUtilities.ExecBootstrapedMSBuild( - $"{candidateAnalysisProjectPath} /m:1 -nr:False -restore /p:OutputPath={env.CreateFolder().Path} -getTargetResult:Build", out bool success, attachProcessId: false); - success.ShouldBeTrue(); - - string? candidatesNugetPackageFullPath = (string?)(JObject.Parse(nugetPackResults)?["TargetResults"]?["Build"]?["Items"]?[0]?["RelativeDir"] ?? string.Empty); - - candidatesNugetPackageFullPath.ShouldNotBeNull(); - candidatesNugetFullPaths.Add(candidatesNugetPackageFullPath); + if (expectedRejectedAnalyzers) + { + projectAnalysisBuildLog.ShouldContain(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("CustomAnalyzerBaseTypeNotAssignable", "InvalidAnalyzer", "InvalidCustomAnalyzer, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")); + } } - - return candidatesNugetFullPaths; } - private void AddCustomDataSourceToNugetConfig(string analysisCandidatePath, IList candidatesNugetPackageFullPaths) + private void AddCustomDataSourceToNugetConfig(string analysisCandidatePath) { var nugetTemplatePath = Path.Combine(analysisCandidatePath, "nugetTemplate.config"); @@ -191,10 +172,10 @@ private void AddCustomDataSourceToNugetConfig(string analysisCandidatePath, ILis if (doc.DocumentElement != null) { XmlNode? packageSourcesNode = doc.SelectSingleNode("//packageSources"); - for (int i = 0; i < candidatesNugetPackageFullPaths.Count; i++) - { - AddPackageSource(doc, packageSourcesNode, $"Key{i}", Path.GetDirectoryName(candidatesNugetPackageFullPaths[i]) ?? string.Empty); - } + + // The test packages are generated during the test project build and saved in CustomAnalyzers folder. + string analyzersPackagesPath = Path.Combine(Directory.GetParent(AssemblyLocation)?.Parent?.FullName ?? string.Empty, "CustomAnalyzers"); + AddPackageSource(doc, packageSourcesNode, "Key", analyzersPackagesPath); doc.Save(Path.Combine(analysisCandidatePath, "nuget.config")); } diff --git a/src/BuildCheck.UnitTests/Microsoft.Build.BuildCheck.UnitTests.csproj b/src/BuildCheck.UnitTests/Microsoft.Build.BuildCheck.UnitTests.csproj index ada169a4b49..4986ffdbef4 100644 --- a/src/BuildCheck.UnitTests/Microsoft.Build.BuildCheck.UnitTests.csproj +++ b/src/BuildCheck.UnitTests/Microsoft.Build.BuildCheck.UnitTests.csproj @@ -14,6 +14,12 @@ + + + + + + diff --git a/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidate/nugetTemplate.config b/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidate/nugetTemplate.config index 1097d29bafd..aa5beec8aa0 100644 --- a/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidate/nugetTemplate.config +++ b/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidate/nugetTemplate.config @@ -1,6 +1,5 @@ - - + diff --git a/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/AnalysisCandidateWithMultipleAnalyzersInjected.csproj b/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/AnalysisCandidateWithMultipleAnalyzersInjected.csproj index 9e71d7ff38f..0ffaa8d26b7 100644 --- a/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/AnalysisCandidateWithMultipleAnalyzersInjected.csproj +++ b/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/AnalysisCandidateWithMultipleAnalyzersInjected.csproj @@ -8,6 +8,7 @@ + diff --git a/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/nugetTemplate.config b/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/nugetTemplate.config index 1097d29bafd..1c0c23b1405 100644 --- a/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/nugetTemplate.config +++ b/src/BuildCheck.UnitTests/TestAssets/AnalysisCandidateWithMultipleAnalyzersInjected/nugetTemplate.config @@ -1,6 +1,5 @@ - diff --git a/src/BuildCheck.UnitTests/TestAssets/Common/CommonTest.props b/src/BuildCheck.UnitTests/TestAssets/Common/CommonTest.props new file mode 100644 index 00000000000..4e4cb1c0796 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/Common/CommonTest.props @@ -0,0 +1,17 @@ + + + + + true + false + 1.0.0 + $(ArtifactsBinDir)Microsoft.Build.BuildCheck.UnitTests\CustomAnalyzers + NU5101;NU5128;MSB3277;NU1507;NU1701;NU1702;NU5104 + + + + + + + + diff --git a/src/BuildCheck.UnitTests/TestAssets/Common/CommonTest.targets b/src/BuildCheck.UnitTests/TestAssets/Common/CommonTest.targets new file mode 100644 index 00000000000..a1ea76b439e --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/Common/CommonTest.targets @@ -0,0 +1,41 @@ + + + + + + + %(ReferencePath.OriginalItemSpec) + + + + <_PackagesToPack Include="@(ResolvedProjectReference->'%(OutputPath)')"> + @(ResolvedProjectReference->'%(AssemblyName)') + + + + + + + + + + + + + <_PackagesToPack Remove="@(_PackagesToPack)" Condition="%(NuGetPackageId) == 'NETStandard.Library'" /> + <_PackagesToPack Remove="@(_PackagesToPack)" Condition="%(_PackagesToPack.IncludeInPackage) != 'true'" /> + + + + + + + + + + + + + + diff --git a/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/CustomAnalyzer.csproj b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/CustomAnalyzer.csproj index f780e9eb213..f27ab620690 100644 --- a/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/CustomAnalyzer.csproj +++ b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/CustomAnalyzer.csproj @@ -1,29 +1,17 @@ + + netstandard2.0 - True - false - - NU5101;NU5128;MSB3277 + true + - - - - $(MSBuildProjectDirectory)\..\..\Microsoft.Build.dll - - - - - - - - - + diff --git a/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/README.md b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/README.md new file mode 100644 index 00000000000..4f29145e7f0 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer/README.md @@ -0,0 +1,21 @@ +# MSBuild Custom Analyzer Template + +## Overview +MSBuild Custom Analyzer Template is a .NET template designed to streamline the creation of MSBuild analyzer libraries. This template facilitates the development of custom analyzers targeting .NET Standard, enabling developers to inspect and enforce conventions, standards, or patterns within their MSBuild builds. + +## Features +- Simplified template for creating MSBuild analyzer libraries. +- Targeting .NET Standard for cross-platform compatibility. +- Provides a starting point for implementing custom analysis rules. + +## Getting Started +To use the MSBuild Custom Analyzer Template, follow these steps: +1. Install the template using the following command: + ```bash + dotnet new install msbuildanalyzer +2. Instantiate a custom template: + ```bash + dotnet new msbuildanalyzer -n + +### Prerequisites +- .NET SDK installed on your machine. \ No newline at end of file diff --git a/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/CustomAnalyzer2.csproj b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/CustomAnalyzer2.csproj index 17007b03785..a405546f1cc 100644 --- a/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/CustomAnalyzer2.csproj +++ b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/CustomAnalyzer2.csproj @@ -1,28 +1,17 @@ + + netstandard2.0 - True - - NU5101;NU5128;MSB3277 + true + - - - - $(MSBuildProjectDirectory)\..\..\Microsoft.Build.dll - - - - - - - - - - + + diff --git a/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/README.md b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/README.md new file mode 100644 index 00000000000..4f29145e7f0 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/CustomAnalyzer2/README.md @@ -0,0 +1,21 @@ +# MSBuild Custom Analyzer Template + +## Overview +MSBuild Custom Analyzer Template is a .NET template designed to streamline the creation of MSBuild analyzer libraries. This template facilitates the development of custom analyzers targeting .NET Standard, enabling developers to inspect and enforce conventions, standards, or patterns within their MSBuild builds. + +## Features +- Simplified template for creating MSBuild analyzer libraries. +- Targeting .NET Standard for cross-platform compatibility. +- Provides a starting point for implementing custom analysis rules. + +## Getting Started +To use the MSBuild Custom Analyzer Template, follow these steps: +1. Install the template using the following command: + ```bash + dotnet new install msbuildanalyzer +2. Instantiate a custom template: + ```bash + dotnet new msbuildanalyzer -n + +### Prerequisites +- .NET SDK installed on your machine. \ No newline at end of file diff --git a/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidAnalyzer.cs b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidAnalyzer.cs new file mode 100644 index 00000000000..e5f466ce414 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidAnalyzer.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace InvalidCustomAnalyzer +{ + public sealed class InvalidAnalyzer + { + public string FriendlyName => "InvalidAnalyzer"; + } +} diff --git a/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidCustomAnalyzer.csproj b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidCustomAnalyzer.csproj new file mode 100644 index 00000000000..4ef7f0674ca --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidCustomAnalyzer.csproj @@ -0,0 +1,17 @@ + + + + + + netstandard2.0 + true + + + + + + + + + + diff --git a/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidCustomAnalyzer.props b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidCustomAnalyzer.props new file mode 100644 index 00000000000..52a9bab57c0 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/InvalidCustomAnalyzer.props @@ -0,0 +1,6 @@ + + + + $([MSBuild]::RegisterBuildCheck($(MSBuildThisFileDirectory)InvalidCustomAnalyzer.dll)) + + diff --git a/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/README.md b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/README.md new file mode 100644 index 00000000000..4f29145e7f0 --- /dev/null +++ b/src/BuildCheck.UnitTests/TestAssets/InvalidCustomAnalyzer/README.md @@ -0,0 +1,21 @@ +# MSBuild Custom Analyzer Template + +## Overview +MSBuild Custom Analyzer Template is a .NET template designed to streamline the creation of MSBuild analyzer libraries. This template facilitates the development of custom analyzers targeting .NET Standard, enabling developers to inspect and enforce conventions, standards, or patterns within their MSBuild builds. + +## Features +- Simplified template for creating MSBuild analyzer libraries. +- Targeting .NET Standard for cross-platform compatibility. +- Provides a starting point for implementing custom analysis rules. + +## Getting Started +To use the MSBuild Custom Analyzer Template, follow these steps: +1. Install the template using the following command: + ```bash + dotnet new install msbuildanalyzer +2. Instantiate a custom template: + ```bash + dotnet new msbuildanalyzer -n + +### Prerequisites +- .NET SDK installed on your machine. \ No newline at end of file