Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Bug Unity seems to break Visual Studio 2019 project files (can not attach to Unity)

Discussion in '2020.1 Beta' started by schaefsky, May 6, 2020.

  1. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    89
    Second post is more up to date, both .csproj and .sln get changed.

    EDIT 2: it is getting weirder, it seems to depend on which script I try to edit with VS. Sometimes the attach fucntion is there, then I close VS, double click the same script and now it is gone. I noticed a "the project file was changed outside VS - Reload Ignore" message popping up in VS sometimes. So to me it currently looks like Unity is somewhat randomly messing up the VS project file.

    EDIT3:
    Actually it seems after changing a .cs file, saving and quitting VS, there is a "asset database refresh" popup in Unity and then the project file is messed up.
    I have attached a .zip with two project files, the one prefixed with WORKING is the working one, the other the messed up one.
    A quick diff shows quite some moving around of stuff, what I noticed are:
    From the working one:
    Code (CSharp):
    1. <LangVersion>7.3</LangVersion>
    2. ...
    3. <PropertyGroup>
    4.     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    5.     <UnityProjectGenerator>Package</UnityProjectGenerator>
    6.     <UnityProjectType>Game:1</UnityProjectType>
    7. <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
    8.     <UnityVersion>2020.1.0b8</UnityVersion>
    9. </PropertyGroup>
    10. ...
    11. <Target Name="GenerateTargetFrameworkMonikerAttribute" />
    those seem to be completey missing in the messed up one.

    The messed up one also has paths like this:
    Code (CSharp):
    1.     <Reference Include="UnityEngine">
    2.       <HintPath>C:\Program Files\Unity\Hub\Editor\2020.1.0b8\Editor\Data\Managed/UnityEngine/UnityEngine.dll</HintPath>
    3.     </Reference>
    4.  
    where \ and / are mixed in the path.
    Generally the messed up one seems to have replaced the \ with / in the paths, but not all.

    EDIT: uninstalling and reinstalling "Visual Studio Editor" from the package manager in Unity fixes the issue temporarily for me. After some editing it will get messed up again.


    I just updated from 2020.1.0b6 to 2020.1.0b8. Now when I double click on a .cs file Visual Studio 2019 will open as before, but I can not see the solution on the right and I can not attach to Unity anymore. Also Intellisense stopped working.
    I just tried another project and there it still works as before.
    So I assume there is some project related setting that is responsible for this behaviour, how do I get VS back to work properly with Unity?
    The Unity extensions is installed and activated in VS and VS is set as editor in Unity. I have already tried deactivating / activating the extension, uninstalling / reinstalling it, uninstalling / reinstalling both VS and Unity
     

    Attached Files:

    Last edited: May 7, 2020
  2. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    89
    Ok, Unity actually changes the .sln file to a VS 2010 version one and the .csproj. When it does that, every double click on a script will open a new VS instance "Solution 1" or open the file in an already running VS instance. Intellisense for Unity and Attach to Unity will be broken.
    Copying back backup versions and double clicking the .csproj from the filesystem will open a VS instance with Intellisense and Attach to Unity working. Double clicking on .cs file in Unity opens new solution with that not working.

    I have attached a zip with .csproj and .sln after reinstalling the Visual Studio Editor tools from the project manager and opening a .cs file by doubleclicking on it in Unity where everything worked.
    Those files are included in the zip prefixed with WORKING.

    I then doubleclicked a .cs file from Unity, deleted an empty line from that in VS, hit save and closed VS.

    Unity did some background stuff including changing the .csproj and .sln files. Those new files are also included in the zip.

    I also included a crude logfile (WatcherLog_20200507_202410.txt) in the zip from a FileSystemWatcher program I quickly hacked together, the change of .csproj and .sln occur near the end at 20:25:11

    .sln WORKING
    Code (CSharp):
    1.  
    2. Microsoft Visual Studio Solution File, Format Version 12.00
    3. # Visual Studio 15
    4. Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp", "Assembly-CSharp.csproj", "{7E13491F-4318-2326-A9EF-CB65F7DD00DB}"
    5. EndProject
    6. Global
    7.     GlobalSection(SolutionConfigurationPlatforms) = preSolution
    8.         Debug|Any CPU = Debug|Any CPU
    9.         Release|Any CPU = Release|Any CPU
    10.     EndGlobalSection
    11.     GlobalSection(ProjectConfigurationPlatforms) = postSolution
    12.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    13.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
    14.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
    15.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Release|Any CPU.Build.0 = Release|Any CPU
    16.     EndGlobalSection
    17.     GlobalSection(SolutionProperties) = preSolution
    18.         HideSolutionNode = FALSE
    19.     EndGlobalSection
    20. EndGlobal
    .sln file after Unity did something:
    Code (CSharp):
    1.  
    2. Microsoft Visual Studio Solution File, Format Version 11.00
    3. # Visual Studio 2010
    4. Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Assembly-CSharp", "Assembly-CSharp.csproj", "{7E13491F-4318-2326-A9EF-CB65F7DD00DB}"
    5. EndProject
    6. Global
    7.     GlobalSection(SolutionConfigurationPlatforms) = preSolution
    8.         Debug|Any CPU = Debug|Any CPU
    9.         Release|Any CPU = Release|Any CPU
    10.     EndGlobalSection
    11.     GlobalSection(ProjectConfigurationPlatforms) = postSolution
    12.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
    13.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
    14.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
    15.         {7E13491F-4318-2326-A9EF-CB65F7DD00DB}.Release|Any CPU.Build.0 = Release|Any CPU
    16.     EndGlobalSection
    17.     GlobalSection(SolutionProperties) = preSolution
    18.         HideSolutionNode = FALSE
    19.     EndGlobalSection
    20. EndGlobal
    I never had VS 2010 on this machine, only 2019.

    Versions:
    Unity 2020.1.0b8.3654
    Visual Studio Editor 2.0.1

    Visual Studio Community 2019 Version 16.5.4
    Visual Studio 2019 Tools for Unity 4.5.1.0

    Removing Visual Studio Editor via package manager and reinstalling it fixes the problem temporarily.
     

    Attached Files:

    Last edited: May 7, 2020
  3. sailro

    sailro

    Microsoft

    Joined:
    Jul 30, 2014
    Posts:
    121
    cc @jbevain @miniwolf_unity

    "WORKING Assembly-CSharp" is indeed generated by our package:

    Code (CSharp):
    1.   <PropertyGroup>
    2.     <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    3.     <UnityProjectGenerator>Package</UnityProjectGenerator>
    4.     <UnityProjectType>Game:1</UnityProjectType>
    5.     <UnityBuildTarget>StandaloneWindows64:19</UnityBuildTarget>
    6.     <UnityVersion>2020.1.0b8</UnityVersion>
    7.   </PropertyGroup>
    but "Assembly-CSharp.csproj" is not:
    - Bad langversion
    - CscToolPath and CscToolExe are present?
    - Mix between unix/windows separators
    - No ProjectTypeGuids, so VS will not load all dedicated Unity wizards
    - No UnityProjectGenerator tag, so it is not Microsoft code

    My guess is that something is rewriting csproj/sln files. (legacy code generation left in Unity ?)

    @schaefsky could you double check which pakage/package versions you are using?

    Perhaps you can try to remove all unneeded package (rider/vscode) and only keep the vs package 2.0.1 to see.

    Thanks!
    Sebastien Lebreton [MSFT]
     
  4. sailro

    sailro

    Microsoft

    Joined:
    Jul 30, 2014
    Posts:
    121
  5. Ziflin

    Ziflin

    Joined:
    Mar 12, 2013
    Posts:
    127
    @sailro Does this also fix Unity "detaching" from VS at random? I've used the "Asset/Open C# Project" fix, but after some project changes inside the editor (creating new scripts, renaming them, etc.) VS will open the super annoying 'File Modification Detected' dialog for the solution and after that trying to open a script opens in the default .cs editor for the OS.

    Also at random times, VS will not enter debug mode. No errors are given, but I have to restart VS to be able to continue debugging code -- despite the Editor being set to "Debug" mode in Unity.

    And finally, it seems there were changes to the ProjectFileGenerator? This fix for Visual Studio displaying CS0649 from Eric Mellino (click to expand comments) here no longer seems to work / get a callback. And it seems the "ENABLE_VSTU" define is no longer set?

    Is there some new way to disable this? Ideally we could just set a "Disable Warnings:" string on the asmdef.
     
  6. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,231
    Yeah this is really annoying. Everytime a new script is made, when opening the script. Visual Stduio Code launches instead of Visual Studio
     
  7. Symo7

    Symo7

    Joined:
    Mar 2, 2014
    Posts:
    23
    I was having this issue with 2.0.0 and 2.0.1 (v1 versions are fine) but the recent 2.0.2 seems to have sorted it. The csproj no longer gets rebuilt (and broken) when entering Play mode. While it was happening as a workaround it could be (temporarily) fixed by going to Preferences -> External Tools -> Regenerate project files which restores the csproj back its original working state.
     
    thong341998 likes this.
  8. radzradz

    radzradz

    Joined:
    Jan 5, 2021
    Posts:
    6
  9. acmoles

    acmoles

    Joined:
    Jun 28, 2016
    Posts:
    4
    This also fixed the issue for me, thanks!

    Unity 2020.2.7f1, VS 2019 Version 16.9.0