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

Unity support #1229

Closed
bdovaz opened this issue Oct 20, 2023 · 10 comments · Fixed by #1349
Closed

Unity support #1229

bdovaz opened this issue Oct 20, 2023 · 10 comments · Fixed by #1349

Comments

@bdovaz
Copy link
Contributor

bdovaz commented Oct 20, 2023

Unity only supports version 3.8 of Microsoft.CodeAnalysis.CSharp: https://docs.unity3d.com/Manual/roslyn-analyzers.html

Roslynator from what I see is directly using version 4.4.0 right now:

https://github.com/dotnet/roslynator/blob/7bc89125ea3793993a9e47f89872daf212bebe57/src/Directory.Build.props#L16C1-L16C1

This makes it not compatible with Unity:

warning CS8032: An instance of analyzer Roslynator.CSharp.Analysis.AddBracesAnalyzer cannot be created from {UNITY_PROJECT_PATH}\Library\PackageCache\org.nuget.roslynator.analyzers@4.6.0\dotnet\cs\Roslynator.CSharp.Analyzers.dll :
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified..

Another very powerful Analyzer is Meziantou, in this case it is compatible with Unity because it offers the analyzers in different versions of Roslyn:

https://github.com/meziantou/Meziantou.Analyzer/blob/cfa7ab74ad7b31a49efcd467a585e39853f2706b/src/Meziantou.Analyzer.pack.csproj#L19

@josefpihrt would it be possible to do the same with Roslynator?

Thank you!

@josefpihrt
Copy link
Collaborator

Unfortunately, it's not possible to downgrade Roslynator to Roslyn 3.8 since there is a lot new syntax (e.g. file-scoped namespace) that depends on Roslyn 4.x

To me the question is why Unity references Roslyn 3.8. Roslyn is backward compatible so updating to 4.4 should not be an issue.

@bdovaz Do you know why Unity uses Roslyn 3.8 and not higher version?

Btw. Roslyn 4.4 was released almost year ago (https://www.nuget.org/packages/Microsoft.CodeAnalysis/4.4.0). So I would say Roslynator tries to be compatible by not immediately using newest Roslyn version.

@bdovaz
Copy link
Contributor Author

bdovaz commented Oct 23, 2023

I think I can answer you with these links:

https://github.com/Unity-Technologies/mono-unity

https://github.com/Unity-Technologies/roslyn

As you see, Unity has its own forks of mono and roslyn in older versions so they are anchored to that and in Roslyn it is equivalent to 3.8 from what it says in the documentation (link in my first post).

I would love for Unity to use .NET 7, the latest version of Roslyn, etc.... But the reality is not like that...

Meziantou has managed to use the 3.3.x version of the Roslyn packages: https://github.com/meziantou/Meziantou.Analyzer/blob/main/Directory.Build.targets

And those analyzers are very powerful and very similar to Roslynator: https://github.com/meziantou/Meziantou.Analyzer/tree/main/docs

You might be inspired to see how he did it.

I would like to help (I have a good contribution history and I am a mantainer in several repositories) but my experience in Roslyn analyzers is practically null...

@Merichbier
Copy link

Merichbier commented Jan 4, 2024

I'm using VS Code as an external tool for Unity. I've tried to set up Roslynator (VS Code extension) on the project, but nothing is reported. Is it for the same reason mentioned in this report?
Setup:

  • VS Code 1.85.1
  • C# Dev Kit 1.1.16
  • Roslynator 4.8.0

@josefpihrt
Copy link
Collaborator

@Merichbier Please see #1338

@Merichbier
Copy link

@josefpihrt Thanks for the tips!
I've enabled C# Dev Kit preview 1.2.5, but unfortunately still no report from Roslynator. I've tried on a different (non-Unity) project, a MAUI solution. Same there no report. I've opened the same MAUI project with Visual Studio 2022 (with Roslynator extension) and to my surprise the issues are reported.
image
image

Any idea what might going wrong ?

@josefpihrt
Copy link
Collaborator

josefpihrt commented Jan 7, 2024

Can you please do the following steps and let me know if it helps?

  • Set dotnet.server.useOmnisharp to true
  • Disable C# Dev Kit

Related issue: dotnet/vscode-csharp#6790

@Merichbier
Copy link

Thanks for the support and help !

For the MAUI project, it indeeds help:
image
In the unity project however this doesn't work. I guess this is because unity is not using standard .Net but Mono framework.
In any case a solution which would allow C# Dev Kit to stay enabled is preferable as both Unity and MAUI package for VS Code depends on it.

@bdovaz
Copy link
Contributor Author

bdovaz commented Jan 8, 2024

The solution to all the problems as I said is that Roslynator would depend on 3.8 and not on 4.x because Unity is not compatible with it...

But it is @josefpihrt who has to evaluate how much impact it has to make that downgrade... The Unity developer base is very large, keep that in mind.

If Meziantou has managed it, I don't see why @josefpihrt won't be able to do it.

@josefpihrt
Copy link
Collaborator

@bdovaz Do you think you could you download artifacts nuget_packages from #1349, use that nuget in your local nuget feed and let me know if it works in Unity?

@bdovaz
Copy link
Contributor Author

bdovaz commented Jan 8, 2024

@josefpihrt great job! It seems to be working properly:

image

image

image

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