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

How to use these with VSCode? #122

Closed
marcospgp opened this issue Dec 11, 2020 · 17 comments
Closed

How to use these with VSCode? #122

marcospgp opened this issue Dec 11, 2020 · 17 comments
Labels
other project This issue is not related to this project

Comments

@marcospgp
Copy link

Is there a simple way to enable these in VSCode?

@sailro
Copy link
Member

sailro commented Dec 11, 2020

See OmniSharp/omnisharp-roslyn#1711

You can use our diagnostics, but you cannot use our suppressors so far.

@sailro sailro closed this as completed Dec 11, 2020
@sailro sailro added the other project This issue is not related to this project label Dec 11, 2020
@marcospgp
Copy link
Author

@sailro I had checked out that thread actually (you can see the latest comment is from me). I couldn't find the information there on how to get the Unity analyzers to be run on VSCode.

Do I have to build the .dlls from this repository? Can I download them using NuGet? Do I have to install Visual Studio? How did you do it?

@sailro
Copy link
Member

sailro commented Dec 11, 2020

No you don't need to install Visual Studio.

We have a nuget here:
https://www.nuget.org/packages/Microsoft.Unity.Analyzers

Check this page:
https://www.strathweb.com/2019/04/roslyn-analyzers-in-code-fixes-in-omnisharp-and-vs-code/

@marcospgp
Copy link
Author

@sailro I used this VSCode extension to install the Microsoft.Unity.Analyzers package, and added an omnisharp.json file to the project after enabling the setting mentioned in the page you linked.

I still am not able to see the Unity analyzers in action, however:

image

The code above should trigger UNT0001 Empty Unity message.

This is what my omnisharp.json looks like:

{
    "RoslynExtensionsOptions": {
        "EnableAnalyzersSupport": true
    }
}

Do I need to add a direct link to the package there? If so, how do I figure out where the nuget extension is installing packages to?

Thanks!

@marcospgp
Copy link
Author

marcospgp commented Dec 12, 2020

Oh wow! I went on a journey but I finally did it!

Ok so trying to install packages using any NuGet installer extension is pointless because it seems Unity won't allow anything to mess with its .csproj files. One extension failed silently and made omnisharp prompt the following message everytime the editor was opened:

There are unresolved dependencies. Please execute the restore command to continue.

Another extension threw a rather obscure error everytime I attempted to install the package:

Error while adding package [...] The project does not support adding package references through the add package command

This all points to Unity trying to keep its .csproj files intact just because it relies so much on manipulating them automatically.

So then I tried downloading the package manually from the NuGet website, then extracting Microsoft.Unity.Analyzers.dll from the package (using 7zip) onto my project's assets folder, and finally referencing it in the omnisharp.json file at the root of the project:

{
    "RoslynExtensionsOptions": {
        "EnableAnalyzersSupport": true,
        "LocationPaths": [
            "./Assets/NuGet/microsoft.unity.analyzers.1.9.0/analyzers/dotnet/cs"
        ]
    }
}

And now it works:

image

This was definitely not obvious and way harder than it needed to be. Shame that there's no clear step by step tutorial on how to do this anywhere 😢 Maybe we should add this to the readme?

@1hko
Copy link

1hko commented Jan 31, 2021

@marcospgp there are clear instructions on this page: https://code.visualstudio.com/docs/other/unity#_enabling-unity-warnings

The page also recommends you do not put NuGet in your Assets folder. You should probably move it to your project's root instead.

@marcospgp
Copy link
Author

Thanks @1hko , though it was I who wrote that section of the guide ahaha

@danvim
Copy link

danvim commented Feb 27, 2022

https://github.com/vad710/UnityEngineAnalyzer#jetbrains-rider-integration specifies an alternative way that also works with Rider.

If we follow the guide to place the .dll in ./NuGet/microsoft.unity.analyzers.1.12.0/Microsoft.Unity.Analyzers.dll, then Assets/csc.rsp would contain -a:"NuGet/microsoft.unity.analyzers.1.12.0/Microsoft.Unity.Analyzers.dll". This should also work without needing omnisharp.json for VSCode.

@Energiz3r
Copy link

@danvim, did you intend to reference different versions of the analyzers (1.9.0 vs 1.12.0)?

@danvim
Copy link

danvim commented Mar 4, 2022

@danvim, did you intend to reference different versions of the analyzers (1.9.0 vs 1.12.0)?

That was unintentional. I was directly copying code from my project. Thanks for reminding. 😅. Edited.

@ghost
Copy link

ghost commented May 4, 2022

So then I tried downloading the package manually from the NuGet website, then extracting Microsoft.Unity.Analyzers.dll from the package (using 7zip) onto my project's assets folder, and finally referencing it in the omnisharp.json file at the root of the project:

Does this mean you need to make a copy and extract the analysers everytime you create a new unity project?

@WeirdBeardDev
Copy link

Yup, that's what that means.

@boyestrous
Copy link

@marcospgp Thanks for these instructions (and the updates to the vscode docs). They are still helpful 2 years later!

@Chizaruu
Copy link

So then I tried downloading the package manually from the NuGet website, then extracting Microsoft.Unity.Analyzers.dll from the package (using 7zip) onto my project's assets folder, and finally referencing it in the omnisharp.json file at the root of the project:

Does this mean you need to make a copy and extract the analysers everytime you create a new unity project?

Not anymore. I forked the project and ensured that omnisharp.json and the nuget dll were automatically generated.
https://github.com/Chizaruu/com.tsk.ide.vscode

@Darender
Copy link

Oh wow! I went on a journey but I finally did it!

Crazy genius, you have no idea how much I had to search the internet to find your solution, thank you!

@sailro
Copy link
Member

sailro commented Aug 17, 2023

We updated VSCode documentation after the release of the new Unity extension for VSCode

@marcospgp
Copy link
Author

We updated VSCode documentation after the release of the new Unity extension for VSCode

So now there are no instructions on how to add third party analyzers to a Unity project so that the warnings will be displayed in VSCode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
other project This issue is not related to this project
Projects
None yet
Development

No branches or pull requests

9 participants