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 #31797

Closed
shaggygi opened this issue Dec 14, 2018 · 12 comments
Closed

Cannot load Analyzers programatically from MSBuildWorkspace #31797

shaggygi opened this issue Dec 14, 2018 · 12 comments
Milestone

Comments

@shaggygi
Copy link
Contributor

Version Used:
VS 2017 (15.9.4)

Scenario:

Create a new .NET command-line project.
Add latest Microsoft.Build.Locator Nuget to project.
Add latest Microsoft.CodeAnalysis and StyleCop.Analyzers Nuget to project.
Project now has 4 Analyzer references:

Microsoft.CodeAnalysis.Analyzers
Microsoft.CodeAnalysis.CSharp.Analyzers
StyleCop.Analyzers
StyleCop.Analyzers.CodeFixes

// Add the following code to the project and run it:
MSBuildLocator.RegisterDefaults();
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

This might be similar to this older issue, but not sure, so I'm pinging the following for help.

@yaakov-h
@jmarolf
@davkean
@sharwell

@jmarolf
Copy link
Contributor

jmarolf commented Dec 15, 2018

@shaggygi what is projectPath pointing to? Also, that is the XML for you project file.

@shaggygi
Copy link
Contributor Author

@jmarolf sorry for not showing that. It is a path to a .csproj file. I’ve tried console and wpf projects with same result.

@jmarolf
Copy link
Contributor

jmarolf commented Dec 15, 2018

@shaggygi and the project pointed to in projectPath has which analyzer references?

@shaggygi
Copy link
Contributor Author

@jmarolf The app that runs the code includes Microsoft.CodeAnalysis and StyleCop.Analyzers and the project the code is check only has StyleCop.Analyzers added.

@shaggygi
Copy link
Contributor Author

shaggygi commented Dec 15, 2018

@jmarolf I just published a sample here if this helps. https://github.com/shaggygi/TryingAnalyzerReferences

Just need to update the path of where the other project is located.

@shaggygi
Copy link
Contributor Author

shaggygi commented Jan 8, 2019

Not sure if this is related. I noticed when I do thing following emit, the only diagnostics returned are with Hidden severity that point to things like .g. files. It seems like you would also get diagnostics that come from StyleCop.Analyzers.

EmitResult emitResult = compilation.Emit(stream);

@jinujoseph jinujoseph modified the milestones: 16.0, 16.1 Jan 18, 2019
@sharwell sharwell added the IDE-MSBuildWorkspace MSBuildWorkspace label Jan 27, 2019
@JPCMarques
Copy link

Any updates on this?

@jasonmalinowski
Copy link
Member

Analyzers should be loading; this was probably some sort of binding redirect issue or something else specific to that setup.

@jasonmalinowski jasonmalinowski closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2025
@filzrev
Copy link

filzrev commented Apr 4, 2025

Unresolved analyzers are represented as UnresolvedAnalyzerReference.
And GetAnalyzers/GetAnalyzersForAllLanguages returns empty results.

So, if it need to detect unresolved analyzers.
It need manuall handle UnresolvedAnalyzerReference.

foreach (var unresolvedAnalyzer in project.AnalyzerReferences.OfType<UnresolvedAnalyzerReference>())
{
    Console.WriteLine($"Failed to resolve analyzer:  {unresolvedAnalyzer.FullPath}");
}

@jasonmalinowski
Copy link
Member

@filzrev I wouldn't expect unresolved analyzers to be happening unless we ended up with a file path that doesn't exist. If you're seeing that then maybe we have a new bug (and please file one!)

@filzrev
Copy link

filzrev commented Apr 4, 2025

I wouldn't expect unresolved analyzers to be happening unless we ended up with a file path that doesn't exist.

Yes, that's right, it happens when the analyzer DLL is not found.

On some use cases. analyzer silently failed to load. (dotnet/docfx#9619 and #77255)
So It need to manually handle these cases.

@jasonmalinowski
Copy link
Member

@filzrev Ah yes, that's another good case to capture in #77980.

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

8 participants