Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get solution path in .NET code analyzer

Tags:

c#

.net

roslyn

How do you get access the file path to the project/solution being compiled inside a Roslyn code analyzer? I need to verify the code against some spec files stored relative to the code. Things that do NOT work:

SyntaxTreeAnalysisContext.Tree.FilePath
Assembly.GetExecutingAssembly().Location
AppDomain.CurrentDomain.BaseDirectory
Environment.CurrentDirectory
Path.GetFullPath(relativePath)
like image 783
Morten Christiansen Avatar asked Aug 03 '15 20:08

Morten Christiansen


People also ask

How do I see code analysis results in Visual Studio?

In Solution Explorer, select the project. On the Analyze menu, select Run Code Analysis on [Project Name].

What is run code analysis on solution?

Web project node and select Analyze | Run Code Analysis. The Code Analysis feature runs through static code analysis rules as defined by Microsoft and displays the results in the Code Analysis window. Scroll through the list of results and read a few of them.

How do I add an Analysisr in .NET core?

To install the package, right-click on the project, and select "Manage Dependencies". From the NuGet explorer, search for "Microsoft. NetFramework. Analyzers".

What is a Roslyn analyzer?

NET Compiler Platform (Roslyn) Analyzers inspect your C# or Visual Basic code for style, quality, maintainability, design, and other issues. This inspection or analysis happens during design time in all open files. Analyzers are divided into the following groups: Code style analyzers are built into Visual Studio.


3 Answers

Analyzers exist below the Workspace level (they're run directly by the compiler), so the solution may not exist.

For complicated reasons, they aren't created by MEF, so there is no easy way to get to it even if it does exist.

From within VS, you can find the global service provider (eg, ServiceProvider.GlobalProvider), then get SComponentModel (the root of VS's own MEF graph) and grab Roslyn's VisualStudioWorkspace from that. Beware that this is a somewhat brittle approach, and will not work at all outside VS.

Even within VS, this will break in strange ways for analyses in Preview panes, Miscellaneous Files, and other contexts that are not part of the global solution.

like image 143
SLaks Avatar answered Nov 15 '22 13:11

SLaks


It's not possible to get solution from analyzer or fixer without reflection.

Use additional files to store settings.

In project:

<ItemGroup>
  <AdditionalFiles Include="MyConfig.config" />
</ItemGroup>

In analyzer:

private const string ConfigFileName = "MyConfig.config";

private static string LoadConfig(ImmutableArray<AdditionalText> additionalFiles, CancellationToken cancellationToken)
{
    var file = additionalFiles.SingleOrDefault(f => string.Compare(Path.GetFileName(f.Path), ConfigFileName, StringComparison.OrdinalIgnoreCase) == 0);
    if (file == null)
    {
        return null;
    }

    var fileText = file.GetText(cancellationToken);

    using (var stream = new MemoryStream())
    {
        using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8, 1024, true))
        {
            fileText.Write(writer, cancellationToken);
        }

        stream.Position = 0;

        using (var reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
}

private static void HandleCompilationStart(CompilationStartAnalysisContext context)
{
    var config = LoadConfig(context.Options.AdditionalFiles, context.CancellationToken);
}
like image 39
Der_Meister Avatar answered Nov 15 '22 13:11

Der_Meister


I figured out a way to do this through reflection, I've only tested this in windows environment.

public static class RoslynExtensions
{
    public static Solution GetSolution(this SyntaxNodeAnalysisContext context)
    {
        var workspace = context.Options.GetPrivatePropertyValue<object>("Workspace");
        return workspace.GetPrivatePropertyValue<Solution>("CurrentSolution");
    }

    public static T GetPrivatePropertyValue<T>(this object obj, string propName)
    {
        if (obj == null)
        {
            throw new ArgumentNullException(nameof(obj));
        }

        var pi = obj.GetType().GetRuntimeProperty(propName);

        if (pi == null)
        {
            throw new ArgumentOutOfRangeException(nameof(propName), $"Property {propName} was not found in Type {obj.GetType().FullName}");
        }

        return (T)pi.GetValue(obj, null);
    }
}

Called from an analyzer like so:

public override void Initialize(AnalysisContext context)
{
    context.RegisterSyntaxNodeAction(AnalyzeConstDeclaration, SyntaxKind.FieldDeclaration);
}

public static void AnalyzeConstDeclaration(SyntaxNodeAnalysisContext context)
{
     var solution = context.GetSolution();
}
like image 4
johnny 5 Avatar answered Nov 15 '22 13:11

johnny 5