diff options
author | Matt Ward <matt.ward@microsoft.com> | 2020-01-03 17:02:05 +0300 |
---|---|---|
committer | Matt Ward <ward.matt@gmail.com> | 2020-01-18 16:38:13 +0300 |
commit | dd49233341410b9eb5e04b53301615a7b5a39221 (patch) | |
tree | 4caba5518884e744c1a3f9330eca7193137bc8c7 /main/src/addins/MonoDevelop.PackageManagement | |
parent | 30e46306561aad819166523a1de8351bac6a5afb (diff) |
[NuGet] Fix analyzers not used by SDK style projects
Analyzer items were not being resolved so no analyzers were being
passed to the type system for SDK style projects. Non-SDK style
PackageReference projects were resolving Analyzer items. Projects
that use a packages.config have to explicitly use Analzyer items
in the project file.
To fix this the CoreCompileDependsOn targets is changed to include
the ResolveLockFileAnalyzers which resolves the Analyzer items.
This was already working for non-SDK style PackageReference projects
since the NuGet addin was adding the ResolveNuGetPackageAssets
target to the CoreCompileDependsOn targets and that resolves the
Analyzer items.
Fixes VSTS #1047893 - Analyzers not used by SDK style projects
Diffstat (limited to 'main/src/addins/MonoDevelop.PackageManagement')
3 files changed, 74 insertions, 2 deletions
diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj index b96a78138b..0e2ce50d9e 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests.csproj @@ -160,6 +160,7 @@ <Compile Include="MonoDevelop.PackageManagement.Tests\UpdateMultipleNuGetPackagesActionTests.cs" /> <Compile Include="MonoDevelop.PackageManagement.Tests.Helpers\TestableUpdateMultipleNuGetPackagesAction.cs" /> <Compile Include="MonoDevelop.PackageManagement.Tests\MonoDevelopNuGetResourceProviderFactoryTests.cs" /> + <Compile Include="MonoDevelop.PackageManagement.Tests\AnalyzerTests.cs" /> </ItemGroup> <ItemGroup> <ProjectReference Include="..\..\..\core\MonoDevelop.Core\MonoDevelop.Core.csproj"> diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/AnalyzerTests.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/AnalyzerTests.cs new file mode 100644 index 0000000000..0bd05d19ce --- /dev/null +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement.Tests/MonoDevelop.PackageManagement.Tests/AnalyzerTests.cs @@ -0,0 +1,68 @@ +// +// AnalyzerTests.cs +// +// Author: +// Matt Ward <matt.ward@microsoft.com> +// +// Copyright (c) 2020 Microsoft Corporation +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using MonoDevelop.PackageManagement.Tests.Helpers; +using MonoDevelop.Projects; +using NUnit.Framework; +using UnitTests; + +namespace MonoDevelop.PackageManagement.Tests +{ + [TestFixture] + public class AnalyzerTests : RestoreTestBase + { + /// <summary> + /// Tests that SDK and non-SDK project with PackageReferences resolves the Analyzers used by the project. + /// </summary> + [Test] + public async Task StyleCopAnalyzer () + { + string solutionFileName = Util.GetSampleProject ("StyleCopAnalyzer", "StyleCopAnalyzer.sln"); + solution = (Solution)await Services.ProjectService.ReadWorkspaceItem (Util.GetMonitor (), solutionFileName); + CreateNuGetConfigFile (solution.BaseDirectory); + var netStandardProject = (DotNetProject)solution.FindProjectByName ("NetStandard"); + var packageReferenceProject = (DotNetProject)solution.FindProjectByName ("NetFramework"); + + var process = Process.Start ("msbuild", $"/t:Restore /p:RestoreDisableParallel=true \"{solutionFileName}\""); + Assert.IsTrue (process.WaitForExit (120000), "Timeout restoring NuGet packages."); + Assert.AreEqual (0, process.ExitCode); + + var netStandardProjectAnalyzers = await netStandardProject.GetAnalyzerFilesAsync (ConfigurationSelector.Default); + var packageReferenceProjectAnalyzers = await packageReferenceProject.GetAnalyzerFilesAsync (ConfigurationSelector.Default); + + var netStandardProjectAnalyzerFileNames = netStandardProjectAnalyzers.Select (f => f.FileName).ToArray (); + var packageReferenceProjectAnalyzerFileNames = packageReferenceProjectAnalyzers.Select (f => f.FileName).ToArray (); + + Assert.That (packageReferenceProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.dll")); + Assert.That (packageReferenceProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.CodeFixes.dll")); + Assert.That (netStandardProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.dll")); + Assert.That (netStandardProjectAnalyzerFileNames, Contains.Item ("StyleCop.Analyzers.CodeFixes.dll")); + } + } +} diff --git a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/PackageManagementMSBuildExtension.cs b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/PackageManagementMSBuildExtension.cs index 05ff593afa..0272139f4b 100644 --- a/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/PackageManagementMSBuildExtension.cs +++ b/main/src/addins/MonoDevelop.PackageManagement/MonoDevelop.PackageManagement/PackageManagementMSBuildExtension.cs @@ -105,17 +105,20 @@ namespace MonoDevelop.PackageManagement /// <summary>
/// Ensures any NuGet package content files are included when CoreCompileDependsOn is evaluated.
- /// Visual Studio 2017 does not run the RunProductContentAssets target directly but runs a set of
+ /// Visual Studio 2017 does not run the RunProduceContentAssets target directly but runs a set of
/// targets which indirectly run RunProductContentAssets. Have to run ResolvePackageAssets before
/// RunProduceContentAssets since the .NET Core 3.0 SDK will not run RunProduceContentAssets if no
/// content items are available to process. The content items are generated by ResolvePackageAssets.
/// Whilst RunProduceContentAssets depends on ResolvePackageAssets it will not run ResolvePackageAssets
/// since the RunProduceContentAssets condition is false. This was not the case in .NET Core 2.1 SDK
/// where there was no condition for the RunProduceContentAssets.
+ ///
+ /// ResolveLockFileAnalyzers is used to generate Analyzer msbuild items for the type system service.
+ /// For non-SDK style projects the Analyzer MSBuild items are generated by ResolveNuGetPackageAssets.
/// </summary> Task<TargetEvaluationResult> OnRunDotNetCoreProjectTarget (ProgressMonitor monitor, string target, ConfigurationSelector configuration, TargetEvaluationContext context) {
- target += ";ResolvePackageAssets;RunProduceContentAssets"; + target += ";ResolvePackageAssets;ResolveLockFileAnalyzers;RunProduceContentAssets"; return base.OnRunTarget (monitor, target, configuration, context); } |