问题
This is a very annoying VS error, I'm on VS 2019 16.8.1
I spend a lot of time fixing it:
Duplicate 'Compile' items were included. The .NET SDK includes 'Compile' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultCompileItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'Program.cs' Service.Host Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets 295
I can't understand what causes this error.
I commented out the Include
tags in the *.csproj files, but the problem is that I have more than 30 projects, all of a sudden they all have the same duplicate error, going through all of them to fix such a mysterious error seems unsafe action, some of these projects are legacy projects that haven't been touched for a long time.
Another problem in the solution commenting the Include
tags is that I have some project with this include structure:
<ItemGroup>
<Compile Include="Contracts.cs" />
<Compile Include="IKey.cs">
<DependentUpon>Contracts.cs</DependentUpon>
</Compile>
<Compile Include="ISigner.cs">
<DependentUpon>Contracts.cs</DependentUpon>
</Compile>
</ItemGroup>
commenting this will affect the project structure.
I tried to comment out the CheckForDuplicateItems
tags in the file:
"C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets",
and the errors are gone.
EDIT
I just noticed that the .NET 5 version (..\sdk\5.0.101..) is included in the targets file path, I only have one project with .NET 5, does this cause the problem?
回答1:
.NET5 uses the new style projects which auto-include a number of files into your project. This helps keep the projects files small and doesn't cause them to be checked out and changed every time you add, remove or rename a file. Probably the easiest way to fix this for all projects at once is to add a directory.build.props
in the root folder of your project to set the <EnableDefaultCompileItems>false<EnableDefaultCompileItems>
for all projects at once.
<Project>
<PropertyGroup>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
</Project>
Do not edit any files in the sdk directory, they are the default settings for all projects current and future. Instead you can add these settings to your project files or these directory of solution global files to change them for your projects in a specific folder or solution.
See:
- Directory.Build.props and Directory.Build.targets
- Default compilation includes in .NET Core projects
来源:https://stackoverflow.com/questions/65275700/fixing-the-duplicate-compile-items-were-included-error-in-visual-studio