Search code examples
c#msbuildneovimomnisharp

How to find and fix duplicate project entries in dotnet solution file?


I use Neovim with OmniSharp and it doesn't load any of 34 projects that are showing in VisualStudio 2022

OmniSharp logs showing this error

[fail]: OmniSharp.WorkspaceInitializer
        The project system 'OmniSharp.MSBuild.ProjectSystem' threw exception during initialization.
System.ArgumentException: An item with the same key has already been added.
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at OmniSharp.MSBuild.ProjectSystem.GetProjectPathsAndIdsFromSolutionOrFilter(String solutionOrFilterFilePath) in D:\a\1\s\src\OmniSharp.MSBuild\ProjectSystem.cs:line 197
   at OmniSharp.MSBuild.ProjectSystem.Initalize(IConfiguration configuration) in D:\a\1\s\src\OmniSharp.MSBuild\ProjectSystem.cs:line 117
   at OmniSharp.WorkspaceInitializer.Initialize(IServiceProvider serviceProvider, CompositionHost compositionHost) in D:\a\1\s\src\OmniSharp.Host\WorkspaceInitializer.cs:line 54

So I assume there should be duplicates in the sln file. I sorted file with :%sort u and found a couple of duplicates, deleted them, but it didn't work. What else can I do?


Solution

  • Although the OP has resolved their issue, I will provide some general guidelines regarding the .sln file.

    When creating a new project, it will be referenced twice in the .sln file. The first reference appears as follows:

    Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTest\UnitTests.csproj", "{A1EEBF59-8295-4354-927F-CE696137BF5C}"
    EndProject
    

    The second reference appears in the Global section, where you need to define the build configurations:

    Global
        GlobalSection(SolutionConfigurationPlatforms) = preSolution
            Debug|x64 = Debug|x64
            Release|x64 = Release|x64
        EndGlobalSection
        GlobalSection(ProjectConfigurationPlatforms) = postSolution
            {A1EEBF59-8295-4354-927F-CE696137BF5C}.Debug|x64.ActiveCfg = Debug|x64
            {A1EEBF59-8295-4354-927F-CE696137BF5C}.Debug|x64.Build.0 = Debug|x64
            {A1EEBF59-8295-4354-927F-CE696137BF5C}.Release|x64.ActiveCfg = Release|x64
            {A1EEBF59-8295-4354-927F-CE696137BF5C}.Release|x64.Build.0 = Release|x64
    

    As the OP mentioned, he manually deleted the first reference to the project but forgot about the second one. The easiest way to find the second reference is by searching the .sln file by project ID or by identifying the ID that no longer exists if the first reference has already been deleted.

    Usually, I use Notepad++ because it has a highlight option. enter image description here