Unity Projects Analysis: The solution file has two projects named “UnityEngine.UI”

Unicorn Developer
PVS-Studio
Published in
2 min readJun 15, 2021

--

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.

Reasons

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).

Possible Solutions

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.

Conclusion

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.

--

--

Unicorn Developer
PVS-Studio

The developer, the debugger, the unicorn. I know all about static analysis and how to find bugs and errors in C, C++, C#, and Java source code.