Bug 58788 - Roslyn Analyzers no longer work with MSBuild Versions on Mac past Mono 5.0.1
Summary: Roslyn Analyzers no longer work with MSBuild Versions on Mac past Mono 5.0.1
Status: RESOLVED DOWNSTREAM
Alias: None
Product: Tools
Classification: Mono
Component: msbuild ()
Version: 5.2 (2017-04)
Hardware: Macintosh Mac OS
: --- critical
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2017-08-15 19:34 UTC by Kevin Ford
Modified: 2017-08-16 21:15 UTC (History)
4 users (show)

Tags: 15.3I
Is this bug a regression?: ---
Last known good build:


Attachments
Test case (minimal) (3.86 KB, application/zip)
2017-08-15 23:35 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DOWNSTREAM

Description Kevin Ford 2017-08-15 19:34:22 UTC
# Steps to reproduce
- Have a Xamarin iOS or Android project that references Roslyn analyzers through Nuget. 
- Build Project in Visual Studio for Mac with MSBuild selected or using MSBuild on the Mac directly in Terminal.

# Expected behavior
Roslyn analyzers are run and evaluate code correctly. 

# Actual behavior
Errors similar to the following for are generated for each analyzer and the analyzers are not run
CSC : error CS8032: An instance of analyzer CSharpGuidelinesAnalyzer.Rules.Naming.AvoidMisleadingNamesAnalyzer cannot be created from /Users/kevinford/Documents/Projects/VSLiveCICDDemo/Code/BuildTest/packages/CSharpGuidelinesAnalyzer.1.0.1/analyzers/dotnet/cs/CSharpGuidelinesAnalyzer.dll : Exception has been thrown by the target of an invocation.. [/Users/kevinford/Documents/Projects/VSLiveCICDDemo/Code/BuildTest/iOS/BuildTest.iOS.csproj]

Because of this no project with references to Roslyn analyzer DLLs compile in the newest Visual Studio for Mac.

# Supplemental info (logs, images, videos)
This works fine in the version of MSBuild that comes with Mono 5.0.1. Since Visual Studio Mobile Center is still using Mono 5.0.1, such projects still compile correctly there. Here is a link to a repository that Contains projects that use Roslyn analyzers (See the BuildTest.Droid and BuildTest.iOS projects under the Code\BuildTest directory):

https://github.com/Bowman74/VSLiveCICDDemoPublic

# Test environment (full version information)
Mono Framework 5.2.0.215
Visual Studio for Mac 7.1.0.1297
Xamarin.Android 7.4.0.19
Xamarin.iOS 10.12.0.18
Comment 1 Brendan Zagaeski (Xamarin Team, assistant) 2017-08-15 21:02:12 UTC
## Bookkeeping note

I will prepare a non-engineering team preliminary quick review for this report.
Comment 2 Brendan Zagaeski (Xamarin Team, assistant) 2017-08-15 23:35:19 UTC
Created attachment 24211 [details]
Test case (minimal)

This appears to be either an upstream bug or intentional change in the Roslyn `csc.exe` compiler.  The same behavior also occurs with the latest .NET toolchain on Windows with no involvement of Xamarin or Mono.

One other observation is that I don't see the same "An instance of analyzer" warnings when using the Analyzer assemblies from the "Microsoft.CodeAnalysis.Analyzers" NuGet package, so there's a chance that the CSharpGuidelinesAnalyzer NuGet package specifically might have a bug or an outdated dependency.

Point #2 from the following excerpt (found via a quick web search) might be relevant:

> > What can be the various reasons for a CS8032 error to be thrown
> 
> The primary reasons are:
> 
> 1. You have an older compiler toolset/VS then the version of
>    Microsoft.CodeAnalysis package that your analyzer was built against.
>    You need to upgrade your compiler toolset/VS if you face this.
> 
> 2. One of your analyzer dependencies is not marked as an analyzer, and
>    hence can't be found at runtime.
(https://github.com/dotnet/roslyn/issues/2683#issuecomment-227265007)

And here's another similar comment from the issue tracker of a particular analyzer library:
https://github.com/tastott/AnalyzeThis/issues/1




## Bug status

Because the observations so far indicate that this issue specifically affects the CSharpGuidelinesAnalyzer NuGet package, I will tentatively mark this bug as RESOLVED DOWNSTREAM at this time.  I suspect the CSharpGuidelinesAnalyzer maintainers will be interested to track down whether this is an issue in their library or an incorrect upstream change in Roslyn.




## Steps followed to test

1. Download the console test case attached to this comment.

2. Open the project in Visual Studio for Mac to restore the NuGet package.

3. msbuild /t:Build ConsoleApp1.sln


OR create your own project:

1. Create a new "Other > .NET > Console Project".

2. Add the CSharpGuidelinesAnalyzer NuGet package (currently version 1.0.1).

3. Control-click the project name in the Solution pad and select "Add > Add Files", and then add a link to "packages/CSharpGuidelinesAnalyzer.1.0.1/analyzers/dotnet/cs/CSharpGuidelinesAnalyzer.dll".

4. Set the "Build action" of the file to "Analyzer".

5. Build the Console project in the "Debug" configuration.




## "Bad?" Results with `csc.exe` version 2.3.2.61928 (ec1cde8b) and `msbuild.exe` version 15.3.409.57025 on Windows

The build produces warnings as described in Comment 0:

CSC : warning CS8032: An instance of analyzer CSharpGuidelinesAnalyzer.Rules.Naming.AvoidMisleadingNamesAnalyzer cannot be created from C:\source\ConsoleApp1\packages\CSharpGuidelinesAnalyzer.1.0.1\analyzers\dotnet\cs\CSharpGuidelinesAnalyzer.dll : Exception has been thrown by the target of an invocation.. [C:\source\ConsoleApp1\ConsoleApp1\ConsoleApp1.csproj]




## "Bad?" Results with Mono 5.2.0.215 (d15-3/da80840) or Mono 5.4.0.135 (2017-06/6425f06)

The build produces warnings as described in Comment 0:

/Users/Shared/Projects/ConsoleApp1/ConsoleApp1/CSC: Warning CS8032: An instance of analyzer CSharpGuidelinesAnalyzer.Rules.Naming.AvoidMisleadingNamesAnalyzer cannot be created from /Users/Shared/Projects/ConsoleApp1/packages/CSharpGuidelinesAnalyzer.1.0.1/analyzers/dotnet/cs/CSharpGuidelinesAnalyzer.dll : Exception has been thrown by the target of an invocation..

(The "VSLiveCICDDemoPublic" test case mentioned in Comment 0 has `<TreatWarningsAsErrors>true</TreatWarningsAsErrors>`, so it displays these as errors.)




## "Good?" Results with Mono 5.0.0.100 (2017-02/9667aa6)

The build produces no warnings that mention "An instance of analyzer" (although in this simple test case, I do get some (probably unrelated) warnings about "Assembly-level attribute ... missing or empty").
Comment 3 Kevin Ford 2017-08-16 21:15:56 UTC
Thank you. I will let the package author know. The warning you received with Mono 5.0.0.100 was the analyzer working correctly (that's one of the things it checks for). Thank you for resolving so quickly.