error NETSDK1022: Duplicate 'Compile' items were included.
An SDK-style project already compiles every .cs file under its folder, and
the .csproj lists one of those files again with a
<Compile Include="..." /> line. Delete that line.
The message ends with the names of the duplicated files. Remove their
Compile Include items from the project file, or, if you really want to list
every file by hand, set EnableDefaultCompileItems to false.
The error
C:\Program Files\dotnet\sdk\10.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.Shared.targets(190,5): error NETSDK1022: 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' [...\DupLab\DupLab.csproj]
The project path is shortened; nothing else in the output was changed.
Why it happens
Projects that start with <Project Sdk="Microsoft.NET.Sdk"> get their
source files implicitly: the SDK adds a Compile item for each
.cs file in the project folder and below. An explicit
<Compile Include="Program.cs" /> adds the same file a second time, and
the SDK refuses a file that would be passed to the compiler twice. The error text
itself names both ways out, and the last part of the message, after "The duplicate
items were", lists every file it found twice. In this test it was only
Program.cs, and the project file to edit is the one named in square
brackets at the end of the line.
Nothing in the code is wrong. The project file and the SDK's defaults disagree about who lists the source files, and the build stops before the compiler even starts, so no C# error appears alongside this one.
The fix
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
dotnet build
dotnet run
With the Compile item removed the build reported 0 errors and the app ran.
The file is still compiled, because the SDK's default items include it; the
Hello, World! from dotnet run came from that same
Program.cs.
The other route the message offers worked too:
dotnet build -p:EnableDefaultCompileItems=false built the unchanged project
with 0 errors, because the explicit line was then the only one. Choose it only if you
intend to maintain the full file list in the project file yourself.
How it was reproduced
A dotnet new console project on .NET SDK 10.0.403 (net10.0)
with one line added to the .csproj:
<ItemGroup><Compile Include="Program.cs" /></ItemGroup>.
dotnet build failed as shown.
Frequently asked
- What does NETSDK1022 duplicate Compile items were included mean?
- The SDK already includes every .cs file in the project folder, and the csproj adds one of them again with a Compile Include item. The message lists the duplicated files.
- How do I fix NETSDK1022?
- Delete the Compile Include lines for the files named in the message and build again. The SDK's default items still compile those files.
- What does EnableDefaultCompileItems false do?
- It turns off the SDK's automatic Compile items, so the explicit Compile Include lines in the project file no longer duplicate them. The error message names it as the second way out.
More decoded errors in the Fixes category.