dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.84k stars 666 forks source link

Incorrectly reporting errors in unity project #2534

Closed Aciho closed 3 days ago

Aciho commented 5 years ago

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.4)

Product Information: Version: 1.0.4 Commit SHA-1 hash: af1e6684fd

Runtime Environment: OS Name: Mac OS X OS Version: 10.13 OS Platform: Darwin RID: osx.10.11-x64 Base Path: /usr/local/share/dotnet/sdk/1.0.4

VS Code version:

1.27.2

C# Extension version:

1.16.1

Steps to reproduce

I don't have a simple repro. In my project, if I open the "Shooter.cs" file then it's fine. If I open the "Rifle.cs" file it's still fine. If I then click back on the open Shooter.cs file I get about 400 spurious errors.

Expected behavior

Only real errors should be reported.

Actual behavior

Almost every line of the file is reported as an error. This is the error on the rifle class itself:

The type 'Rifle' in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assets/Scripts/Rifle.cs' conflicts with the imported type 'Rifle' in 'Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assets/Scripts/Rifle.cs'. [Assembly-CSharp]
The type 'Rifle' in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assets/Scripts/Rifle.cs' conflicts with the imported type 'Rifle' in 'Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assets/Scripts/Rifle.cs'. [Assembly-CSharp-Editor]

This is from a reference to the rifle in the Shooter class:

The type or namespace name 'Rifle' could not be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp, Assembly-CSharp-firstpass, Assembly-CSharp-Editor]

(This is most of the errors)

This is a reference in Shooter to an external enum:

Cannot implicitly convert type 'Shooter.LaserType [Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]' to 'Shooter.LaserType [Assembly-CSharp-Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]'. An explicit conversion exists (are you missing a cast?) [Assembly-CSharp-Editor]

This is the output from omnisharp when loading up:

Starting OmniSharp server at 9/17/2018, 4:57:48 PM
    Target: /Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/swatson_OSX_SVT2.sln

OmniSharp server started with Mono 5.14.0.
    Path: /Users/simonwatson/.vscode/extensions/ms-vscode.csharp-1.16.1/.omnisharp/1.32.5/omnisharp/OmniSharp.exe
    PID: 12532

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on MacOS 10.13.6 (x64)
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 2 MSBuild instance(s)
            1: Mono 15.0 - "/usr/local/Cellar/mono/5.14.0.177/lib/mono/msbuild/15.0/bin"
            2: StandAlone 15.0 - "/Users/simonwatson/.vscode/extensions/ms-vscode.csharp-1.16.1/.omnisharp/1.32.5/omnisharp/msbuild/15.0/Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Mono 15.0 - "/usr/local/Cellar/mono/5.14.0.177/lib/mono/msbuild/15.0/bin"
            CscToolPath = /Users/simonwatson/.vscode/extensions/ms-vscode.csharp-1.16.1/.omnisharp/1.32.5/omnisharp/msbuild/15.0/Bin/Roslyn
            CscToolExe = csc.exe
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Could not find any Cake files
[info]: OmniSharp.WorkspaceInitializer
        Project system 'OmniSharp.DotNet.DotNetProjectSystem' is disabled in the configuration.
[info]: OmniSharp.MSBuild.ProjectSystem
        Detecting projects in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/swatson_OSX_SVT2.sln'.
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-firstpass.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-Editor.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2'.
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: /Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp.csproj
[info]: OmniSharp.MSBuild.ProjectManager
        Successfully loaded project file '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp.csproj'.
[info]: OmniSharp.MSBuild.ProjectManager
        Adding project '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: /Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-firstpass.csproj
[info]: OmniSharp.Script.ScriptProjectSystem
        Could not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpWorkspaceOptionsProvider
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2' on host 12486.
[info]: OmniSharp.MSBuild.ProjectManager
        Successfully loaded project file '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-firstpass.csproj'.
[info]: OmniSharp.MSBuild.ProjectManager
        Adding project '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-firstpass.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: /Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-Editor.csproj
[info]: OmniSharp.MSBuild.ProjectManager
        Successfully loaded project file '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-Editor.csproj'.
[info]: OmniSharp.MSBuild.ProjectManager
        Adding project '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Assembly-CSharp-Editor.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Update project: Assembly-CSharp
[warn]: OmniSharp.MSBuild.ProjectManager
        Unable to resolve assembly '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Temp/bin/Debug/Assembly-CSharp-firstpass.dll'
[info]: OmniSharp.MSBuild.ProjectManager
        Update project: Assembly-CSharp-firstpass
[info]: OmniSharp.MSBuild.ProjectManager
        Update project: Assembly-CSharp-Editor
[warn]: OmniSharp.MSBuild.ProjectManager
        Unable to resolve assembly '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Temp/bin/Debug/Assembly-CSharp-firstpass.dll'
[warn]: OmniSharp.MSBuild.ProjectManager
        Unable to resolve assembly '/Users/simonwatson/Documents/Projects/swatson_OSX_SVT2/Temp/bin/Debug/Assembly-CSharp.dll'
akshita31 commented 5 years ago

@Aciho I think I might know what is the problem here. Do you have symbolic links in your project? Can you try setting omnisharp.path: "1.32.2" in your settings and then Ctrl+Shift+P --> Restart OmniSharp and see if the error still reproduces ?

llint commented 5 years ago

I've got similar intellisense errors in my dotnet core 2.1 / C# based project within vscode / omnisharp (windows 10 host, vscode 1.27.2, C# plugin 1.16.1, with git). My current repro: after opening a git diff view, hover my mouse on different symbols on the changed lines a few times, intellisense seems messed up by either not showing the correct information of the symbols or hovering on symbols away from the changed lines, the intellisense seems reporting something totally unrelated. Then after closing the diff view, a bunch of C# errors now are reported in the "Problems" tab, something along the line of "class does not contain a definition of something", seems the intellisense database is totally messed up.

Reopen vscode would solve the intellisense problems temporarily, until the repro steps are tried again.

Aciho commented 5 years ago

@akshita31 I checked, and there are no symbolic links. The only unusual thing about this project as far as I know is that there was a folder full of old copies of some of the files in the root for a while. I deleted it, so unless omnisharp has a cache somewhere that shouldn't be a problem any more.

I tried to set the path to 1.32.2, but I get the following error:

Error occured in loading omnisharp from omnisharp.path
Could not start the server due to Error: 404

I'm going to try downloading and installing manually, once I find some instructions.

e: Installed 1.32.2 manually, the errors are the same as before

Giometric commented 5 years ago

I've been seeing those same errors; I think this may actually be a Unity issue. All those paths that are being referred to are from the various .csproj files. They're all pointing to dlls that are expected to be in the Temp/bin/Debug folder, but Unity doesn't appear to be placing any dlls in there. At least 2017 and 2018 don't - on my machine all those dlls end up in Library/ScriptAssemblies. Unity is generating these .csproj files automatically, so it's possible they're sticking the wrong paths in there by mistake.

What's odd is that the Intellisense eventually starts working anyway for me, just takes longer than it used to to start up, and shows the little red error flame icon forever.

zvinless commented 5 years ago

I think the red flame icon part of this issue is #2521 since there are no visible errors in the log. I'm having a similar intellisense issue in Unity, although I'm using the incremental compiler preview package, and it looks like OmniSharp is saying that my project is set to C#6 while trying to use C#7 features (even though all the .csproj files say <LangVersion>7</LangVersion>. Maybe it's related if there's an issue with the paths? I do also get the Unable to resolve assembly warnings at the end of my log on startup.

ccadori commented 5 years ago

Any updates about this issue? I still getting it on 1.17.1

nextgLoki commented 5 years ago

Any updates about this issue? I still getting it on 1.17.1

Do you mean this problem? "Error occured in loading omnisharp from omnisharp.path Could not start the server due to Error: 404" Today i got it in my Unity project. Settings omnisharp.path in VS Code was "latest".

Next way work for me:

  1. Download omnisharp from this https://github.com/OmniSharp/omnisharp-vscode/releases/download/v1.18.0-beta2/csharp-1.18.0-beta2.vsix
  2. Turn off auto-update in VS Code using: press ctrl+shift+p and choose "Extensions: Disable Auto Updating Extensions"
  3. Install downloaded csharp-1.18.0-beta2.vsix: press ctrl+shift+p in VS Code and choose "Extensions: Install from VSIX".
  4. Change in VS Code User Settings to "omnisharp.path": "1.32.8"
  5. Restart VS Code.
BiosElement commented 5 years ago

This error still persists as of OmniSharp 1.32.9-beta. nextgLoki's advice was tested and the incorrect error remains.

rchande commented 5 years ago

@BiosElement Which error?

BiosElement commented 5 years ago

@rchande Oh perhaps not. Here's what I'm getting.

The type 'MetroDuder' in 'c:\Users\William\Documents\Unity Projects\BiosHub\Assets\Scripts\MetroDuder.cs' conflicts with the imported type 'MetroDuder' in 'Mirror, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'c:\Users\William\Documents\Unity Projects\BiosHub\Assets\Scripts\MetroDuder.cs'. [Assembly-CSharp] MetroDuder

I can confirm it doesn't matter what new class I add, any name is always in conflict. This doesn't appear to be the case in Visual Studio Community but I cannot rule out user error. Note that it 'does' resolve after reloading Omnisharp or restarting VSCode but this has to be repeated for each new class.

rchande commented 5 years ago

@BiosElement Ok, it sounds like you may have found an issue where files get "added" twice to OmniSharp. I'll take a look.

rchande commented 5 years ago

@BiosElement I'm having trouble reproducing this with .net core. How are you "adding a class"?

RDeluxe commented 5 years ago

I'm having the same issue here. Creating a script from Unity, and yes I'm under the impression the file gets added twice (appears twice in unity UI too).

BiosElement commented 5 years ago

@rchande I'm afraid I can't think of a better way to explain it, but for example copying a library from another project in with VSCode open results in a import 'conflict' that doesn't actually exist with other packages or namespaces. In fact the namespaces are entirely different. I'm not actually seeing the file appear twice within Unity, but I'd expect the error to be different if that was the case.

Here's a screenshot of exactly what I'm seeing. https://imgur.com/eyDupK1

rchande commented 5 years ago

@BiosElement From that error message (and from what you said), it sounds like your project references the package/assembly PhotonUnityNetworking.Utilties which contains a type in the global namespace called WebCache. Your project itself defines a type called WebCache in the global namespace (?) which the compiler reports as conflicting with the one from you reference. If that is what happening, that's what I would expect the compiler to say and this doesn't sound like a bug.

If my understanding is not correct, would it be possible for you to create a small project that reproduces the issue and share it with me?

RDeluxe commented 5 years ago

Hello. I'm getting the same issue, for all the classes I'm creating. Your point is correct, that would normally be the problem : creating a class with the same name in the global namespace, however that's not the case here. Forcing a reload solve the issue, until the next class creation, etc.

BiosElement commented 5 years ago

@rchande I can certainly put together a demo project, but as @RDeluxe mentioned, it resolves itself when Omnisharp is restarted. Anything I create wouldn't show the issue until changes were made with imports or new classes and it makes no sense. Happy to put together a video and a sample project over the week though if that'd help, I'm just not sure it'd be very helpful to display the bug.

And yes, I agree the error message indicates a conflict, but even using custom namespaces and totally random function or class names cause the same problem and it doesn't seem tied to any specific packages, but it 'might' be tied to Unity and how it generates the project files for vscode.

rchande commented 5 years ago

@RDeluxe @BiosElement We created a beta that might help with this. Can you install it and let me know? https://github.com/OmniSharp/omnisharp-vscode/releases/tag/v1.18.0-beta5

BiosElement commented 5 years ago

@rchande Thanks for the fast response, I'm sure this is annoying since I can't exactly narrow down the issue. I've installed the beta and will test it today/tomorrow, see how it goes and reports back.

mihnescu commented 5 years ago

Hello @rchande, I installed the 1.18.0-beta5 of omnisharp, and tried but still get the conflict.

I know that firstpass is only creted when using StandardAssets and Plugins code, and I am indeed doing that. But neither of the classes I am working with right now are part of those specific folders.

Anyway, hope this gets fixed soon because I love VS Code. In the mean time back to the heavier Visual Studio :-)

Thanks!

zilppuri commented 5 years ago

Having the same issue. The type 'AnyClassName' in '/Users/me/dev/project/Assets/AnyClassName.cs' conflicts with the imported type 'AnyClassName' in 'Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '/Users/me/dev/project/Assets/AnyClassName.cs'. [Assembly-CSharp-Editor]

This started happening once I updated to Unity 2018. Interestingly same as @BiosElement I also have Photon in the project use and was adding Coroutines. I wonder if this is a crazy coincidence or if these are some of the elements that can make this happen? However the issue now persists even after I removed the new class with Coroutine. Each new class I try to add to the project gets this waring displayed.

xinaesthete commented 5 years ago

I was running into this on Mac, with Unity 5.6.5 yesterday... indeed, was trying to do some refactoring etc in readiness to upgrade Unity version but just adding some pretty basic class seemed to be coming up with it.

Sorry to not provide more detail, but pretty sure Photon & Coroutines were in no way implicated in my case.

lukikow commented 5 years ago

I have the same issue but it apperas only after I upload my repo to unity repository. It's really annoying, always need to reopen vscode.

mihnescu commented 5 years ago

Quick Update: Unity 2019.2 officially added support for VS Code via package manager. To ensure projects are generated correctly, remove all csproj files that are related to the unity project and reopen the the project from Unity in order to properly generated the proj files. You might also want to remove any old .vscode directory just to be sure that a clean vs-code compatible is generated from unity.

Will come with updates after I test this for a few days

lukikow commented 5 years ago

Unity 2019.2 doesn't helped me. Still have the same issue.

BUT using csharp-1.18.0-beta2.vsix works great! Thank you nextgLoki!

JoeRobich commented 3 days ago

There have been lots of improvements since this issue was opened. Please open a new issue with logs if you are still running into this.