An instance of analyzer cannot be created
Flavien opened this issue · 17 comments
Thanks for building this, it looks like a must-have already. Unfortunately I am not able to use it. When adding the NuGet package to a .NET 6.0 console application, I get the following warnings:
Warning CS8032 An instance of analyzer Lombok.NET.AllArgsConstructorGenerator cannot be created from C:\Users\MyName\.nuget\packages\lombok.net\0.3.0\analyzers\dotnet\cs\Lombok.NET.dll: Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified..
It repeats the same warning for the six analyzers.
The analyzer is not running and the code is not being generated. Nothing happens when I add the attributes.
Thanks for reporting this! Can you please share your .csproj file as well as a class you are using an attribute on?
This is Program.cs:
using Lombok.NET;
namespace LombokRepro;
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
[With]
[RequiredArgsConstructor]
public partial class Test
{
private readonly int _value;
}
This is the csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Lombok.NET" Version="0.3.0" />
</ItemGroup>
</Project>
What version of Visual Studio are you using? And have you tried clearing your /bin
and /obj
folders?
It's Visual Studio 2022. And clearing bin and obj doesn't make any difference (this happens on a fresh new console project).
Okay, I think I have the issue pegged onto the generators targeting netstandard2.1, which they're apparently not supposed to do. I've downgraded to netstandard2.0, can you try again with version 0.3.1?
No luck, still the same problem:
Warning CS8032 An instance of analyzer Lombok.NET.AllArgsConstructorGenerator cannot be created from C:\Users\MyName\.nuget\packages\lombok.net\0.3.1\analyzers\dotnet\cs\Lombok.NET.dll: Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.. LombokRepro 1 Active Could not load file or assembly 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
It's still trying to locate netstandard 2.1.0.0
though.
Very weird. Unfortunately I can't reproduce any of the issues since I use Rider, but I might be able to do so tomorrow. Will keep you posted.
So I managed to reproduce your issue with version 0.3.0 in VS 2022. But as soon as I upgraded to 0.3.1 and rebuilt again, the error disappeared. Can you try cleaning and rebuilding?
Yes, actually it works. I though it didn't because this didn't do anthing:
[AllArgsConstructor]
public partial class Test
{
private readonly int _value;
}
Isn't that supposed to create a constructor with a value
parameter?
I wonder if it fails because value
is a reserved keyword and it should write @value
?
@derSchtefan Good point. I actually don't check for reserved keywords yet. This is definitely something I will implement. However value
doesn't seem to be "as reserved" as other keywords. Manually creating a constructor and naming the parameter value
works.
@Flavien The AllArgsConstructor
attribute does create a constructor with a value
parameter. I tested it in VS 2022 with v0.3.1 and it worked for me:
public class Program
{
public static void Main(string[] args)
{
var test = new Test(2);
Console.WriteLine(test.GetValue());
}
}
[AllArgsConstructor]
public partial class Test
{
private readonly int _value;
public int GetValue() => _value;
}
When I run the program, the output is 2.
Is the issue still persisting? Otherwise I'd close the issue.
Actually yes, it works at build time, but Intellisense doesn't recognise the constructor and gives me red squiggles, and if I go to definition in Visual Studio, the generated source file is just an empty partial class.
I have the same issue with Rider (only regarding constructors, IntelliSense recognizes everything else). I assume it's a general issue with IDEs recognizing generated constructors. But as long as it builds and runs correctly, that is part that matters.
Well everyone has different requirements, but in my case tooling support is a hard requirement.
You should probably document this limitation as I suspect a vast majority of users would be like me.
You're right, this is not something to just accept. I will investigate further and see if I can find a workaround. However I will do so in a separate issue, since this is now fixed.