microsoft / vscode-dotnettools

This is a feedback repository to capture issues logged for the C# Dev Kit and related extensions from Visual Studio Code
Other
219 stars 9 forks source link

Unity Package fails to locate Analyzers when VS Code is installed by Scoop #331

Open Soroosh-Sharif opened 1 year ago

Soroosh-Sharif commented 1 year ago

Describe the Issue

I am encountering an issue with "Unity for Visual Studio Code". Intellisense suggests that a private variable decorated with "SerializeField" should be readonly. However, making the field readonly causes it to disappear from the unity editor, which seems incorrect behavior. Is there a way to hide this suggestion?

Code_k6X8CrfdOx

Steps To Reproduce

  1. Create a new Unity project
  2. Create a monobehaviour scirpt
  3. Add a private variable decorated with "SerializeField"
  4. IntelliSense complains that it should be "readonly"

Expected Behavior

Intellisense should not complain about making a variable readonly if it is decorated with "SerializeField".

Environment Information

jbevain commented 1 year ago

Hi! This looks to be very similar to #322. Could you make sure your C# and C# Dev Kit extensions are updated to at least:

Thanks!

Soroosh-Sharif commented 1 year ago

image image image

I made sure to restart vscode and unity yet the problem still exists. Perhaps downgrading the C# to v2.0.320 would help?

jbevain commented 1 year ago

Thanks for checking this. In Unity's Preferences -> External Tools, could you do “Regenerate project files” just in case? Then check the generated csproj (like Assembly-CSharp.csproj), you should have a line like (but not with my username 😄):

    <Analyzer Include="/Users/jbevain/.vscode/extensions/visualstudiotoolsforunity.vstuc-0.9.0/Analyzers/Microsoft.Unity.Analyzers.dll" />

You could also try to invoke the command .NET Restart Language Server.

Soroosh-Sharif commented 1 year ago

Regenerating project files and restarting the language server using .NET Restart Language Server didn't solved the problem. My csproj doesn't have a line like yours.

If I set Visual Studio as the external editor, Analyser line will appear in csproj but when I switch back to Visual Studio Code the line disappears. I added the line manually to the csproj but it doesn't work.

jbevain commented 1 year ago

That points to a setup issue.

@Soroosh-Sharif, please make sure that in Unity's Package Manager, the Visual Studio Editor package is updated to version 2.0.20, then regenerate the project files?

Soroosh-Sharif commented 1 year ago

Sorry for my late response. Visual Studio Editor is already the latest version.

image

I suspect there is something wrong with my vscode installation because I face same problems in Blazor projects. I will test it and report back as soon as possible.

jbevain commented 1 year ago

If you don't have an <Analyzer> in your generated csproj, it's indeed possible that our Visual Studio Editor package somehow fails to find the analyzers.

On Linux and macOS we're looking at:

~/.vscode/extensions/visualstudiotoolsforunity.vstuc-0.9.0/Analyzers\*.dll

On Windows we're looking at:

%USERPROFILE%\.vscode\extensions\visualstudiotoolsforunity.vstuc-0.9.0\Analyzers\*.dll

Are you extensions installed elsewhere?

Soroosh-Sharif commented 1 year ago

I see. I manage my vscode installation using Scoop. A quick search on my drive revealed that Microsoft.Unity.Analyzers.dll can be found at the following locations:

Is it possible to set a custom path for extension directory?

I also found one at C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\Extensions\Microsoft\Visual Studio Tools for Unity\Analyzers\Microsoft.Unity.Analyzers.dll. I assume that's why it works fine for Visual Studio.

jbevain commented 1 year ago

@Soroosh-Sharif Hah! That explains it! Thank you so much for the help and for the details. Now we need to find a better way to find our analyzers relative to your Visual Studio Code installation.

jbevain commented 1 year ago

@Soroosh-Sharif what's the full path of the exe you give to Unity as an external tool?

Soroosh-Sharif commented 1 year ago

C:\Users\<user>\scoop\apps\vscode\current\Code.exe

jbevain commented 1 year ago

@Soroosh-Sharif hi! Just wanted to provide a quick update. Today we don't support scenarios where VS Code is not using its standard extensions folder. We have an idea on how to address that but it will require some work outside of the Unity extension. We're keeping this issue opened as a suggestion as this is something that we want to address. Thanks!

Soroosh-Sharif commented 1 year ago

Thank you.

I temporarily solved the problem by copying the visualstudiotoolsforunity.vstuc-0.9.0 folder from: C:\Users\<user>\scoop\persist\vscode\data\extensions and pasting it into: C:\Users\<user>\.vscode\extensions