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

Unintended behavior when there are analyzers for multiple Roslyn versions in one nuget package #615

Closed
hadashiA opened this issue Jan 18, 2024 · 2 comments · Fixed by #616

Comments

@hadashiA
Copy link
Contributor

hadashiA commented Jan 18, 2024

Description

A single nuget package can contain multiple Roslyn analyzers with same content but different Roslyn versions.
This is to support older versions of Roslyn users.

Currently, this seems to be a problem in some cases.

For example, if we install System.Text.Json with NuGetForUnity, source generators x3 with the same content are imported.

スクリーンショット 2024-01-18 16 54 03

System.Text.Json 8.0.1/
└── analyzers/
    └── dotnet/
        ├── roslyn3.11/
        │   └── cs/
        │       └── System.Text.Json.SourceGeneration.dll < For Roslyn 3.11 or newer
        ├── roslyn4.0/
        │   └── cs/
        │       └── System.Text.Json.SourceGeneration.dll < For Roslyn 4.0 or newer
        └── roslyn4.4/
            └── cs/
                └── System.Text.Json.SourceGeneration.dll  < For Roslyn 4.4 or newer

I tried the following simple System.Text.Json Source Generator example, getting errors.

public class WeatherForecast
{
    public DateTime Date { get; set; }
    public int TemperatureCelsius { get; set; }
    public string? Summary { get; set; }
}

[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(WeatherForecast))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}

スクリーンショット 2024-01-18 14 48 24

This is because multiple SourceGenerators with the same contents are run.

[Describe the issue you have. If applicable provide logs produced by enabeling verbose logging in the NuGetForUnity settings.]

  • NuGet Package: System.Text.Json
  • NuGetForUnity Version: 4.0.2
  • Unity Version: 2022.2.21f, etc
  • Operating System: macOS

Improvement proposal

This problem can be solved by limiting the number of dlls with the RoslynAnalyzer asset label to one.
Currently, analzyer is disable on any platform, so doing this was sufficient.

So, why not pick one analyzer that can be used in the target Unity editor and change it to only put RoslynAnalyzer there?

According to our research, the analyzer/source generator versions that work with Unity are as follows.
(You can find the Microsoft.CodeAnalysis.CSharp.dll bundled in a folder called DotNetSdkRoslyn in the Unity editor application, so you can check the version.)

  • Unity 2022.3.12f or newer: 4.3.0
  • Unity 2022.2 or newer: 4.1.0
  • Unity 2021.2 or newer: 3.8.0

I have implemented this in #616 and would appreciate your confirmation. Thanks!

@igor84
Copy link
Collaborator

igor84 commented Jan 18, 2024

Unity documentation at https://docs.unity3d.com/Manual/roslyn-analyzers.html says Your source generator must use Microsoft.CodeAnalysis 3.8 to work with Unity even for 2022 and 2023 versions. Do you think their documentation is wrong on this account?

@neuecc
Copy link

neuecc commented Jan 18, 2024

Unity compiler is silently updated, it can find in Editor\Data\DotNetSdkRoslyn\

image

image

Incremental Compiler also works, and the C# version can be updated (C# 11.0).
https://github.com/Cysharp/ZLogger/?tab=readme-ov-file#installation

Unity documentation is often not updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants