Search Unity

Cannot get code completion to work in Visual Studio Code (OmniSharp issue?)

Discussion in 'Editor & General Support' started by Waarten, Sep 22, 2020.

  1. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    I've been trying all day to get code completion to work in Visual Studio Code, which at some point worked, but now it doesn't. Does anyone have the same problem? Or could someone post the version numbers of their working configuration? I'm on Windows 10 x64.

    Installed extensions

    upload_2020-9-22_13-17-12.png

    Installed .NET SDKS

    upload_2020-9-22_13-21-21.png

    Visual Studio Code Version

    upload_2020-9-22_13-18-3.png

    Unity package installed

    I've tried both versions 1.2.2 (latest) and 1.2.1
    upload_2020-9-22_13-24-32.png

    Additional

    I've also installed .NET Framework 4.6 Targeting Pack as per the suggestion of this guide (which I followed): https://code.visualstudio.com/docs/other/unity

    Problem Description
    Code completion does not work (see first image). This is caused I think by OmniSharp not understanding the project (?) and crashing.

    I'm posting the OmniSharp log. Note that the crash is not related to
    SimpleCameraController.cs
    , as it happens with any script. Note that it happens in any new or existing project. I've also tried to 'regenerate project files' in the external tools section.

    Any help would be greatly appreciated!

    Code (CSharp):
    1. /*
    2. Starting OmniSharp server at 9/22/2020, 1:11:31 PM
    3.     Target: l:\repositories\newtestproject\newtestproject\newtestproject.sln
    4.  
    5. OmniSharp server started.
    6.     Path: c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\OmniSharp.exe
    7.     PID: 1384
    8.  
    9. [info]: OmniSharp.Stdio.Host
    10.         Starting OmniSharp on Windows 6.2.9200.0 (x64)
    11. [info]: OmniSharp.Services.DotNetCliService
    12.         DotNetPath set to dotnet
    13. [info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
    14.         Located 2 MSBuild instance(s)
    15.             1: Visual Studio Community 2017 15.9.28307.1259 - "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin"
    16.             2: StandAlone 16.8.0 - "c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild\Current\Bin"
    17. [warn]: OmniSharp.CompositionHostBuilder
    18.         It looks like you have Visual Studio lower than VS 2019 16.3 installed.
    19. Try updating Visual Studio to the most recent release to enable better MSBuild support.
    20. [info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
    21.         MSBUILD_EXE_PATH environment variable set to 'c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild\Current\Bin\MSBuild.exe'
    22. [info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
    23.         Registered MSBuild instance: StandAlone 16.8.0 - "c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild\Current\Bin"
    24.             CscToolExe = csc.exe
    25.             CscToolPath = c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild\Current\Bin\Roslyn
    26.             MSBuildExtensionsPath = c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild
    27.             MSBuildToolsPath = c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild\Current\Bin
    28. [info]: OmniSharp.Cake.CakeProjectSystem
    29.         Detecting Cake files in 'l:\repositories\newtestproject\newtestproject'.
    30. [info]: OmniSharp.Cake.CakeProjectSystem
    31.         Could not find any Cake files
    32. [info]: OmniSharp.MSBuild.ProjectSystem
    33.         Detecting projects in 'l:\repositories\newtestproject\newtestproject\newtestproject.sln'.
    34. [info]: OmniSharp.MSBuild.ProjectManager
    35.         Queue project update for 'l:\repositories\newtestproject\newtestproject\Assembly-CSharp.csproj'
    36. [info]: OmniSharp.MSBuild.ProjectManager
    37.         Queue project update for 'l:\repositories\newtestproject\newtestproject\Assembly-CSharp-Editor.csproj'
    38. [info]: OmniSharp.Script.ScriptProjectSystem
    39.         Detecting CSX files in 'l:\repositories\newtestproject\newtestproject'.
    40. [info]: OmniSharp.Script.ScriptProjectSystem
    41.         Could not find any CSX files
    42. [info]: OmniSharp.WorkspaceInitializer
    43.         Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
    44. [info]: OmniSharp.MSBuild.ProjectManager
    45.         Loading project: l:\repositories\newtestproject\newtestproject\Assembly-CSharp.csproj
    46. [info]: OmniSharp.WorkspaceInitializer
    47.         Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionOptionsProvider, Order: 0
    48. [info]: OmniSharp.WorkspaceInitializer
    49.         Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100
    50. [info]: OmniSharp.WorkspaceInitializer
    51.         Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110
    52. [info]: OmniSharp.WorkspaceInitializer
    53.         Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.BlockStructureWorkspaceOptionsProvider, Order: 140
    54. [info]: OmniSharp.WorkspaceInitializer
    55.         Configuration finished.
    56. [info]: OmniSharp.Stdio.Host
    57.         Omnisharp server running using Stdio at location 'l:\repositories\newtestproject\newtestproject' on host 7824.
    58. [warn]: OmniSharp.MSBuild.ProjectManager
    59.         Failed to load project file 'l:\repositories\newtestproject\newtestproject\Assembly-CSharp.csproj'.
    60. l:\repositories\newtestproject\newtestproject\Assembly-CSharp.csproj(1,1)
    61. Microsoft.Build.Exceptions.InvalidProjectFileException: The expression ""Assets\Scripts\SimpleCameraController.cs".GetPathsOfAllDirectoriesAbove()" cannot be evaluated. Method 'System.String.GetPathsOfAllDirectoriesAbove' not found.  c:\Users\Username\.vscode\extensions\ms-dotnettools.csharp-1.23.2\.omnisharp\1.37.1\.msbuild\Current\Bin\Roslyn\Microsoft.Managed.Core.targets
    62.    at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject(String errorSubCategoryResourceName, IElementLocation elementLocation, String resourceName, Object[] args)
    63.    at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject[T1,T2](IElementLocation elementLocation, String resourceName, T1 arg0, T2 arg1)
    64.    at Microsoft.Build.Evaluation.Expander`2.Function`1.Execute(Object objectInstance, IPropertyProvider`1 properties, ExpanderOptions options, IElementLocation elementLocation)
    65.    at Microsoft.Build.Evaluation.Expander`2.ItemExpander.IntrinsicItemFunctions`1.<ExecuteStringFunction>d__13.MoveNext()
    66.    at Microsoft.Build.Evaluation.Expander`2.ItemExpander.<Transform>d__0`1.MoveNext()
    67.  
    68. (...the call stack does on for a bit, then an similar crash, then...)
    69.  
    70. [fail]: OmniSharp.MSBuild.ProjectManager
    71.         Attempted to update project that is not loaded: l:\repositories\newtestproject\newtestproject\Assembly-CSharp.csproj
    72. [fail]: OmniSharp.MSBuild.ProjectManager
    73.         Attempted to update project that is not loaded: l:\repositories\newtestproject\newtestproject\Assembly-CSharp-Editor.csproj
    74. [info]: OmniSharp.OmniSharpWorkspace
    75.         Miscellaneous file: l:\repositories\newtestproject\newtestproject\Assets\testscript.cs added to workspace
    76. */
     

    Attached Files:

    FoxyCornelius likes this.
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    What is your external editor set to in Unity?
     
  3. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    I've both tried "Visual Studio Code" and the custom option where I pointed it the .exe. Unity detects that and sets it to "Visual Studio Code". As mentioned, I have regenerated project files (also tried first removing them).

    upload_2020-9-22_15-35-33.png
     
  4. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    After 1,5 days of struggling with this, I've given up and did a complete reinstall of Windows 10 and all programs involved. The good news is that it works now.

    Did I mention I hate computers?

    For anyone encountering the same problem in the future, I'm sorry that I can't be of any help. All I know now is that a clean OS install plus this guide works, currently. The guide is a bit untidily written regarding different unity versions, but I can confirm it works with the latest 2020 release.
     
    Lorrak, LizhengheChen and LucaPlescia like this.
  5. rousgidraph

    rousgidraph

    Joined:
    Oct 27, 2020
    Posts:
    1
    I've also been experiencing the same whenever I load a script on vs code.

    My current vscode version
    Version: 1.49.3

    My current unity version: 2020.1.3f1

    I am running this on pop os 20.04 and will post updates if I find a fix.
     

    Attached Files:

  6. Samael777

    Samael777

    Joined:
    Oct 28, 2020
    Posts:
    1
  7. RChrispy

    RChrispy

    Joined:
    Dec 18, 2013
    Posts:
    71
    @Samael777 thanks installing version 4.7.1 worked for me I had 4.8 installed and this is not compatible with unity!
    Installing this allowed omnishark to understand the unity output :)
     
  8. Maximetinu

    Maximetinu

    Joined:
    May 25, 2016
    Posts:
    5
  9. klvngnn

    klvngnn

    Joined:
    Nov 6, 2020
    Posts:
    1
  10. FoxyCornelius

    FoxyCornelius

    Joined:
    Jan 14, 2020
    Posts:
    2
    Downloading the .NET Framework almost worked for me, but I was still getting a lot of the omnisharp errors listed here. The problem for me seemed to be that I had the latest versions of everything installed. When I first downloaded Unity I might not have gone through the Unity Hub, but I didn't end up with Visual Studio installed, so I downloaded the latest version. That does not seem to work, in that it results in all of these errors. I uninstalled everything. It turns out that Unity 2020.2 wants Visual Studio 2019, and (I think) only .NET Core 3.1 SDK (not the latest version, which is .Net 5.0 at this time).
    Installing Unity 2020.2 through Unity Hub seemed to include the 2019 version of Visual Studio - yay! - and then I downloaded and installed .NET Core 3.1. I had already installed the .NET Framework 4.7.1 Developer Pack listed above, so that may have helped as well.
    The following page was quite helpful but didn't list the versions of everything that are needed: https://code.visualstudio.com/docs/...-code-completion-for-recent-versions-of-unity
     
  11. unity_in80cAQOY7Dz7Q

    unity_in80cAQOY7Dz7Q

    Joined:
    Jan 12, 2021
    Posts:
    1
    Thanks! Installing 4.7.1 and regenerate the project files solved the issue for me.
     
    KledsonZG and Gibbeh like this.
  12. matsimo

    matsimo

    Joined:
    May 16, 2021
    Posts:
    1
    Thank you a lot! I struggled for several days with this, and now it seems to work fine!!!
     
    Gibbeh likes this.
  13. Virtuoz_KilLoFal

    Virtuoz_KilLoFal

    Joined:
    Jul 26, 2021
    Posts:
    1
    Gibbeh likes this.
  14. Gibbeh

    Gibbeh

    Joined:
    Feb 21, 2019
    Posts:
    1
  15. QTheBrawler

    QTheBrawler

    Joined:
    Aug 16, 2022
    Posts:
    1
  16. GrassWhooper

    GrassWhooper

    Joined:
    Mar 25, 2016
    Posts:
    109
    for anyone on linux (i am on ubuntu 20.04/mint 20.3), i got the same error, i ended up installing the .net framework 6.x

    and then i installed the C# extension version 1.24.4
    the 1.25.0 version (latest) was so buggy, and didn't work.
     
  17. younessfahmii

    younessfahmii

    Joined:
    Mar 10, 2019
    Posts:
    1
    try downloading .NET Framework 4.7.1 developer pack, it solved all my issues