CollinAlpert / Lombok.NET

.NET adaptation for Java's Lombok using Source Generators.
MIT License
309 stars 16 forks source link

Cannot build when using constructor, can't create analyzer instance #51

Closed ThePlay3r closed 2 months ago

ThePlay3r commented 2 months ago

Describe the bug I've just found out about this project and was exited to use it, coming from Java, this is something I would really appreciate having in C# as well.

To my issue now, it seems I can't build my project, if I'm using the generated constructors (I'm using the [RequiredArgsConstructor] one), weirdly enough, Intellisense itself doesn't give any error, and even navigates me to the generated constructor in /tmp/JetBrains.., only the build itself fails.

To Reproduce Steps to reproduce the behavior:

  1. Create a new class with readonly field
  2. Add the [RequiredArgsConstructor] attribute
  3. Use the generated constructor
  4. Try to build

Expected behavior I expect the project to build successfully.

Screenshots This is the code:

[RequiredArgsConstructor]
public partial class AddPlayerToGameCallback : IBaseObjectCallback<IPlayer>
{
    private readonly GameManager _gameManager;

    public void OnBaseObject(IPlayer baseObject)
    {
        _gameManager.AddPlayer(baseObject);
    }
}
        // Add all players
        var callback = new AddPlayerToGameCallback(serviceProvider.GetService<GameManager>());
        Alt.ForEachPlayers(callback);

This is the .csproj, if its of any significance:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="AltV.Net.Async" Version="16.1.1" />
      <PackageReference Include="DiamondCrew.AltV.Core.Server" Version="1.2.0" />
      <PackageReference Include="Lombok.NET" Version="2.4.0" />
      <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
    </ItemGroup>

    <ItemGroup>
        <ProjectReference Include="..\DiamondCrew.AltV.TTT.Shared\DiamondCrew.AltV.TTT.Shared.csproj" />
    </ItemGroup>

    <ItemGroup>
      <Folder Include="Model\" />
    </ItemGroup>

</Project>

Environment info

Additional context I'm also getting these errors in the build log:

2>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.ConstructorGenerators.AllArgsConstructorGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
2>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.ConstructorGenerators.NoArgsConstructorGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
..
..
2>TTTServer.cs(33,32): Error CS1729 : 'AddPlayerToGameCallback' does not contain a constructor that takes 1 arguments

Seems to be issue with all analyzers. I'm using the latest JetBrains Rider.

CollinAlpert commented 2 months ago

Hi, thanks for the detailed report. Unfortunately I will only have time to look into this next week.

CollinAlpert commented 2 months ago

I managed to build your reproduction without any errors. Can you check if this project works on your machine? LombokTest.zip

I will need to investigate some more as to how this error could occur, but like I said that will most likely happen next week.

ThePlay3r commented 2 months ago

I managed to build your reproduction without any errors. Can you check if this project works on your machine? LombokTest.zip

I will need to investigate some more as to how this error could occur, but like I said that will most likely happen next week.

Unfortunately, I get the same error.

Logs:

0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.ConstructorGenerators.AllArgsConstructorGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.ConstructorGenerators.NoArgsConstructorGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.ConstructorGenerators.RequiredArgsConstructorGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.DecoratorGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.AsyncGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.AsyncOverloadsGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.FreezablePatternGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.SerializationGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.ToStringGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.ToTextGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.MethodGenerators.WithMethodsGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.PropertyGenerators.EnumValuesGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.PropertyGenerators.LazyGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.PropertyGenerators.NotifyPropertyChangedGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.PropertyGenerators.NotifyPropertyChangingGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.PropertyGenerators.PropertyGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>CSC: Warning CS8032 : An instance of analyzer Lombok.NET.PropertyGenerators.SingletonGenerator cannot be created from /home/raj/.nuget/packages/lombok.net/2.4.0/analyzers/dotnet/cs/Lombok.NET.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..
0>Program.cs(7,13): Error CS1729 : 'AddPlayerToGameCallback' does not contain a constructor that takes 1 arguments
0>AddPlayerToGameCallback.cs(10,34): Warning CS8618 : Non-nullable field '_gameManager' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
0>AddPlayerToGameCallback.cs(10,34): Warning CS0649 : Field 'AddPlayerToGameCallback._gameManager' is never assigned to, and will always have its default value null
ThePlay3r commented 2 months ago

Upon further investigation, I've probably found the reason.

Using .NET SDK 6.0.132 for build causes the error for me. When I build it using .NET SDK 8.0.107 it builds successfully. (Even when targeting net6.0)

For me, this solves the issue, but I don't really understand the reason for the failure.

CollinAlpert commented 2 months ago

That's very interesting. But I'm glad it works now.