Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot load Analyzers programatically from MSBuildWorkspace with Microsoft.CodeAnalysis 1.1.1 #7654

Closed
yaakov-h opened this issue Dec 22, 2015 · 6 comments
Milestone

Comments

@yaakov-h
Copy link
Member

Scenario:

  1. Create a new .NET 4.6 command-line project
  2. Add Microsoft.CodeAnalysis 1.1.1 and StyleCop.Analyzers 1.0.0-rc2 to a project with NuGet
  3. Project now has 5 Analyzer references:
    1. Microsoft.CodeAnalysis.Analyzers
    2. Microsoft.CodeAnalysis.CSharp.Analyzers
    3. Newtonsoft.Json (empty)
    4. StyleCop.Analyzers
    5. StyleCop.Analyzers.CodeFixes
  4. Add the following code to the project and run it:
var workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(projectPath);

var allLanguageAnalyzers = project.AnalyzerReferences.SelectMany(r => r.GetAnalyzersForAllLanguages());
var languageSpecificAnalyzers = project.AnalyzerReferences.SelectMany(r => r.GetAnalyzers(project.Language));

Where projectPath is the path to this project.

Expectations:

  • allLanguageAnalyzers is not empty
  • languageSpecificAnalyzers is not empty

Actual Results:

  • allLanguageAnalyzers is empty
  • languageSpecificAnalyzers is empty

Note:

  • When using Microsoft.CodeAnalysis 1.0.0 instead of 1.1.1, this all works. I have attempted 1.1.1 with the following assembly redirects, but to no avail.
  • The internal exception seems to be a FileNotFoundException which is caught.
  • Even without adding StyleCop, the Microsoft.CodeAnalysis analyzers are not returned on 1.1.1. This I find exceptionally odd.
  <runtime>
    <assemblyBinding>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.CSharp.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.VisualBasic" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces.Desktop" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.27.0-1.1.65535.65535" newVersion="1.1.37.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Composition.AttributedModel" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.65535.65535" newVersion="1.0.27.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Composition.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.65535.65535" newVersion="1.0.27.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.99.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
@davkean davkean added this to the 1.2 milestone Dec 22, 2015
@jmarolf
Copy link
Contributor

jmarolf commented Dec 22, 2015

@yaakov-h are you using VS 2015 RTM or Update 1?

@jmarolf jmarolf added the Need More Info The issue needs more information to proceed. label Dec 22, 2015
@yaakov-h
Copy link
Member Author

Update 1.

@jmarolf jmarolf removed the Need More Info The issue needs more information to proceed. label Dec 22, 2015
@jmarolf
Copy link
Contributor

jmarolf commented Dec 22, 2015

Looks like a problem in the Update 1 assemblies. I'll try and get a fix out today.

@jmarolf
Copy link
Contributor

jmarolf commented Jan 11, 2016

@yaakov-h add xmlns="urn:schemas-microsoft-com:asm.v1 to you assemblyBinding definition in App.config

 <runtime>
    <assemblyBinding  xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.CSharp" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.CSharp.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.VisualBasic" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.CodeAnalysis.Workspaces.Desktop" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.7.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.27.0-1.1.65535.65535" newVersion="1.1.37.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Composition.AttributedModel" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.65535.65535" newVersion="1.0.27.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Composition.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.65535.65535" newVersion="1.0.27.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Reflection.Metadata" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.99.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

That will allow the clr to load analyzers that depend on 1.0 binaries.

@yaakov-h
Copy link
Member Author

Thanks @jmarolf, that did the trick.

@shaggygi
Copy link
Contributor

shaggygi commented Dec 14, 2018

@jmarolf (also //cc-ing @sharwell based on his knowledge of StyleCop). I was trying something similar with current Nuget packages. Seems like it won't read the analyzers and just returns 0. I'm using latest VS 2017. What are the steps again to fix this? Thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants