PhilH
PhilH

Reputation: 80

EnableDefaultCompileItems issue. Compile Include items being added at build time

I'm currently trying to migrate old csproj files to the dotnet core styles ones.

I have a bare bones csproj file like

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="Current">
  <PropertyGroup>
    <PackageTags>build-$(BUILD_BUILDID)</PackageTags>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <AssemblyName>Something</AssemblyName>
    <AssemblyTitle>Something</AssemblyTitle>
    <AssemblyProduct>Something</AssemblyProduct>
    <Description>Something</Description>
    <ProjectGuid>SomeGuid</ProjectGuid>
    <EnableDefaultCompileItems>true</EnableDefaultCompileItems>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="SomeReference" Version="6.0.2" />
    <PackageReference Include="SomeReference" Version="3.4.0" />
    <PackageReference Include="SomeReference" Version="4.7.0" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="System.Configuration" />
    <Reference Include="System.Configuration.Install" />
    <Reference Include="System.ServiceProcess" />
  </ItemGroup>
</Project>

Visual Studio is happy with this but excludes all of my code. I've tried to explicitly set EnableDefaultCompileItems to true ( as above ) but nothing happens even after a project unload / load.

If I try to 'Include in Project' via 'Show All Files' then I get a bunch of 'Compile Include' elements being added at build time:

 <ItemGroup>
    <Compile Include="Class1.cs" />
  </ItemGroup>

Which produces the following error

Severity    Code    Description Project File    Line    Suppression State   Suppression State
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: 'Class1.cs'   Some.Project.Name   
    

This seems to suggest VS already knows the file exists but isn't displaying it in solution explorer or including it in a compile. Has anyone come across this issue before?

VS version info:

Microsoft Visual Studio Enterprise 2019
Version 16.3.4
VisualStudio.16.Release/16.3.4+29409.12
Microsoft .NET Framework
Version 4.8.03752

Upvotes: 3

Views: 8982

Answers (1)

Mr Qian
Mr Qian

Reputation: 23760

EnableDefaultCompileItems issue. Compile Include items being added at build time

It is the new feature of new sdk format project and the new sdk's csproj file is different from the old sdk format. It is very concise, you do not need to add item include like compile, none, Microsoft.NET.Sdk will automatically associate all source files by default, no need to write manually, which is the advantage of this new sdk format.

Please refer to this document about how to migrate old net framework csproj file into the new sdk format.

Which produces the following error

Note: although that set EnableDefaultCompileItems to false will avoid this error, it will hidden the files on the solution explorer.

So it would better set it to true and then delete any item nodes like compile include, none include and so on.

Or, use

<PropertyGroup>
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>

to show all exlcude files in the solution explorer.

Then, delete any compile xml node like this:

 <Compile Include="Class1.cs" />

For the whole csproj file, refer to this:

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        
        <OutputType>exe</OutputType>
        
        <!--use this in the new sdk format, net472 means net framework 4.7.2-->
        <TargetFramework>net472</TargetFramework>

        <FileAlignment>512</FileAlignment>
        <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
        <Deterministic>true</Deterministic>
    </PropertyGroup>

    <PropertyGroup>
        <EnableDefaultItems>true</EnableDefaultItems>   
    </PropertyGroup>
    

    <ItemGroup>
        <PackageReference Include="SomeReference" Version="6.0.2" />
        <PackageReference Include="SomeReference" Version="3.4.0" />
        <PackageReference Include="SomeReference" Version="4.7.0" />
    </ItemGroup>
    <ItemGroup>
        <Reference Include="System.Configuration" />
        <Reference Include="System.Configuration.Install" />
        <Reference Include="System.ServiceProcess" />
    </ItemGroup>
    
    
    <!--//remove  itemgroup like this-->
    
    <!--<ItemGroup>
        <Compile Include="Program.cs" />
        
        <None Include="xxxxxxx" />
        
        .......
        
        any item 

    </ItemGroup>-->
        
</Project>

Upvotes: 5

Related Questions