konveyor / analyzer-lsp

Add-on that is focused on providing analysis based on the Language Server Protocol.
Apache License 2.0
12 stars 43 forks source link

[BUG] dotnet-provider method referenced rule not found on sample project #694

Open djzager opened 4 weeks ago

djzager commented 4 weeks ago

Construct a very simple .NET Project.

Start by creating a dotnet-example directory. Then create a project file...so it will build

// Goes in dotnet-example/DotnetExample.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

Then write the program.

// This would go in dotnet-example/Program.cs
using System;
using System.Net;
using System.IO;

// Copied from https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-8.0
public class Test
{
    public static void Main(string[] args)
    {
        if (args == null || args.Length == 0)
        {
            throw new ApplicationException("Specify the URI of the resource to retrieve.");
        }
        using WebClient client = new WebClient();

        // Add a user agent header in case the
        // requested URI contains a query.

        client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        using Stream data = client.OpenRead(args[0]);
        using StreamReader reader = new StreamReader(data);
        string s = reader.ReadToEnd();
        Console.WriteLine(s);
    }
}

Write a very basic rule

// ./dotnet-example/rule-example.yaml
- message: 'System.Net.WebClient found. Consider replacing with System.Net.Http.HttpClient.'
  ruleID: dotnet-deprecated-001
  effort: 2
  when:
    dotnet.referenced:
      namespace: "System.Net"
      pattern: WebClient

Run kantra against this rule: kantra analyze --input ./dotnet-example --output ./report --rules ./dotnet-example/rule-example.yaml --log-level 10.

Provider does not find the rule.

NOTE It is worth pointing out that we can find the "NonPortableMethod" in the HelloWorld example. Maybe this is a problem with how assemblies paths are shown in .net8?

djzager commented 4 weeks ago

@dymurray When I was working through what @shawn-hurley was experiencing I had kind of chalked it up to not being a method call, maybe it was just hard to make the query correct or we should change the query.