9
votes

I am migrating an old .NET Framework csproj to dotnet core. What is the dotnet core equivalent of this:

<Compile Include="ServiceHost.Designer.cs">
  <DependentUpon>ServiceHost.cs</DependentUpon>
</Compile>

I tried:

<ItemGroup>
    <Compile Include="ServiceHost.Designer.cs">
        <DependentUpon>ServiceHost.cs</DependentUpon>
    </Compile>
</ItemGroup>

But I got this error:

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: 'ProjectInstaller.Designer.cs'; 'ServiceHost.Designer.cs' TestWindowsService C:\Program Files\dotnet\sdk\2.1.4\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets

2
Maybe this post will be helpful. - Justinas Marozas

2 Answers

17
votes

Since the items are included by default, you need to use Update instead of Include if you only want to modify the items that need this update and not list every cs file individually:

<ItemGroup>
    <Compile Update="ServiceHost.Designer.cs">
        <DependentUpon>ServiceHost.cs</DependentUpon>
    </Compile>
</ItemGroup>
-1
votes

New format SDK projects have several globbing patterns set by default to "True". One of them is to include all *.cs files in project directory and it's subdirectories. The error you are getting is caused by double inclusion of *.cs files and there is an easy way to prevent it indicated in error message. You should include into your project the following property:

<PropertyGroup>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

With that setting you have to include all files in the project explicitly by using:

<ItemGroup>
    <Compile Include="ServiceHost.Designer.cs">
        <DependentUpon>ServiceHost.cs</DependentUpon>
    </Compile>
    <Compile Include="MyOtherFile.cs"/>
    .......
</ItemGroup>

If you decide to not use EnableDefaultCompileItems setting all *.cs files will be included automatically, however, their grouping could be confusing as it may be flattened without any subgroupings. In this case you should not include any *.cs files eplicitly in the .csproj. The globbing pattern used by project will include files in the project automatically for you.