While PVS-Studio analyses a Unity project, one may stumble upon such an error: Error was encountered while trying to open solution file ‘…’: The solution file has two projects named “UnityEngine.UI”. This note discusses the reasons for this error and how to eliminate it.
PVS-Studio uses some third-party libraries, including Roslyn and MSBuild to check C# projects. We use Roslyn to parse code. MSBuild parses solution (.sln) and project (.csproj) files. Besides, MSBuild is the main .NET build system.
You may encounter the error above when you call the Microsoft.Build.Construction.SoltuionFile.Parse method to receive a SolutionFile instance. The method is in the external library for the analyzer (Microsoft.Build.dll), so we can’t change its behavior directly.
If you look closer at the text representation of the .sln file, you can notice duplicates in it. Example: (IDs of project types are shorter to make the example more compact).
Note that this .sln file duplicates project names: UnityEngine.UI and Assembly-CSharp. Each instance corresponds to a unique project file. For example, these are UnityEngine.UI.csproj and UnityEngine.UI.Player.csproj for UntiyEngine.UI.
If you try to build this solution by MSBuild, you will see an already familiar error: Solution file error MSB5004: The solution file has two projects named “UnityEngine.UI”.
Interestingly, Visual Studio and JetBrains Rider, unlike MSBuild, are able to build such projects. Apparently, they use a different build mechanism (maybe they parse the .sln file themselves).
A possible solution is to change project settings. The Unity editor generates an .sln file with duplicates if the “Player projects” option is enabled. Disable it in the same Unity editor: Edit -> Preferences -> Player projects.
After that, you may need to regenerate project files. The “Regenerate project files” button below is right for this.
Then duplicates from the .sln file must disappear, and the analysis should work fine.
If this method does not help or is not suitable for you, please email us.
We strive to improve the analysis of Unity projects. If you encounter any problems or have any wishes, please feel free to write to us.