如何在dotnet解决方案文件中查找和修复重复的项目条目?

huangapple go评论64阅读模式
英文:

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

问题

我使用 Neovim 与 OmniSharp,但它不加载在 VisualStudio 2022 中显示的 34 个项目。

OmniSharp 日志显示以下错误:

        项目系统 'OmniSharp.MSBuild.ProjectSystem' 在初始化期间引发异常。
System.ArgumentException: 已添加具有相同键的项。
   在 System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   在 OmniSharp.MSBuild.ProjectSystem.GetProjectPathsAndIdsFromSolutionOrFilter(String solutionOrFilterFilePath) in D:\a\s\src\OmniSharp.MSBuild\ProjectSystem.cs:line 197
   在 OmniSharp.MSBuild.ProjectSystem.Initalize(IConfiguration configuration) in D:\a\s\src\OmniSharp.MSBuild\ProjectSystem.cs:line 117
   在 OmniSharp.WorkspaceInitializer.Initialize(IServiceProvider serviceProvider, CompositionHost compositionHost) in D:\a\s\src\OmniSharp.Host\WorkspaceInitializer.cs:line 54

所以我认为在 sln 文件中可能存在重复项。我使用 :%sort u 命令对文件进行排序并找到一些重复项,然后删除它们,但问题仍然存在。我还能做什么?

英文:

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\s\src\OmniSharp.MSBuild\ProjectSystem.cs:line 197
   at OmniSharp.MSBuild.ProjectSystem.Initalize(IConfiguration configuration) in D:\a\s\src\OmniSharp.MSBuild\ProjectSystem.cs:line 117
   at OmniSharp.WorkspaceInitializer.Initialize(IServiceProvider serviceProvider, CompositionHost compositionHost) in D:\a\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?

答案1

得分: 1

在重新查看解决方案文件后,我发现重复项实际上位于文件的另一部分。

{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Release|Any CPU.Build.0 = Release|Any CPU

在某个地方有这4行的重复项。
删除它们,现在一切都正常工作。

英文:

After revisiting solution file I found out that duplicates were in fact in the other section of the file

{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F853C9A7-7E99-48A2-AB4F-0D48E150853B}.Release|Any CPU.Build.0 = Release|Any CPU

Somewhere there was a duplicate of this 4 lines
Deleted them and everything works now

答案2

得分: 1

尽管楼主已解决了他们的问题,但我将提供一些关于.sln文件的一般指导方针。

在创建新项目时,.sln文件中会有两次引用。第一个引用如下所示:

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

第二个引用位于全局部分,在那里您需要定义构建配置:

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

正如楼主提到的,他手动删除了项目的第一个引用,但忘记了第二个。找到第二个引用的最简单方法是通过项目ID在.sln文件中进行搜索,或者如果第一个引用已经被删除,通过识别不再存在的ID来找到它。

通常我使用Notepad++,因为它具有高亮显示选项。

英文:

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.
如何在dotnet解决方案文件中查找和修复重复的项目条目?

huangapple
  • 本文由 发表于 2023年3月15日 19:57:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定