Home > other >  Adding new class to .NET 6 project results in build error NETSDK1022 (VS2022)
Adding new class to .NET 6 project results in build error NETSDK1022 (VS2022)

Time:05-19

I recently added a new project to my solution. All projects in the solution are.NET 6 projects written in C#. (Using Visual Studio 2022 version 17.2.0)

Every time I add a new class to this new project. Visual Studio inexplicably adds it to the .csproj file (as a <Compile/> tag inside an item group). This leads to a build error.

To illustrate, just now I added a test class (in file "Class.cs") to the project and then built. This is the error:

4>C:\ProgramFiles\dotnet\sdk\6.0.300\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: 'ViewModels\Class1.cs'

Sure enough, here is the line added to the .csproj file

  <Compile Include="ViewModels\Class1.cs" />

If I manually then remove that line from the project file, it builds fine.

The weird thing is it's only for this project. If I do the same thing for any other project in the solution VS does not add the file to the project and I get no build error.

Is there some setting I can use to fix this?

FYI: Here is the initial property group from the project. I have another app project with the same header that does not exhibit this problem

<PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <PlatformTarget>x64</PlatformTarget>
    <AssemblyName>GelSight.VerifyDevice</AssemblyName>  
    <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
    <BaseOutputPath>..\..</BaseOutputPath>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <Platforms>x64</Platforms>
    <Nullable>enable</Nullable>
</PropertyGroup>

CodePudding user response:

Set the EnableDefaultCompileItems property to false.

If you explicitly define any of these items in your project file, you're likely to get a "NETSDK1022" build error similar to the following:

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.

Duplicate 'EmbeddedResource' items were included. The .NET SDK includes 'EmbeddedResource' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultEmbeddedResourceItems' property to 'false' if you want to explicitly include them in your project file.

To resolve the errors, do one of the following:

  • Remove the explicit Compile, EmbeddedResource, or None items that match the implicit ones listed on the previous table.

  • Set the EnableDefaultItems property to false to disable all implicit file inclusion:

<PropertyGroup>
  <EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
  • Selectively disable only Compile, EmbeddedResource, or None globs by setting the EnableDefaultCompileItems, EnableDefaultEmbeddedResourceItems, or EnableDefaultNoneItems property to false:
<PropertyGroup>
  <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
  <EnableDefaultEmbeddedResourceItems>false</EnableDefaultEmbeddedResourceItems>
  <EnableDefaultNoneItems>false</EnableDefaultNoneItems>
</PropertyGroup>

If you only disable Compile globs, Solution Explorer in Visual Studio still shows *.cs items as part of the project, included as None items. To disable the implicit None glob, set EnableDefaultNoneItems to false too.

  • Related