Prevent VS from adding explicit compile tags to csproj

Viewed 1436

I am aware of .NET Core's change to the .csproj file, and how the GLOB automatically detects *.cs files, as as documented here. However, the issue is I am still running into a certain error upon compile.

As documented in the above link, I keep getting the 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.

My project is picking up all *.cs files perfectly fine within the project directory. However, whenever I ** ADD A NEW** class (right click >> add new item), the following is also added into the .csproj:

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

I know that simply removing this will prevent the error from occurring, since this explicit compile conflicts with the GLOB's implicit include. How can I stop this from happening?

EDIT

Here is my .csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <LangVersion>8</LangVersion>
        <NeutralLanguage>en</NeutralLanguage>
        <Authors>Phillip Smith</Authors>
        <Company>Phillip Smith</Company>
        <Description>.Net Standard library to read and write .m64 files for Mupen64</Description>
        <PackageIcon>nugetIcon.png</PackageIcon>
        <PackageTags>mupen, n64, m64</PackageTags>
        <EnableNETAnalyzers>true</EnableNETAnalyzers>
        <PackageProjectUrl>https://github.com/TimeTravelPenguin/MupenSharp</PackageProjectUrl>
        <RepositoryUrl>https://github.com/TimeTravelPenguin/MupenSharp</RepositoryUrl>
        <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        <Version>1.0.3.5</Version>
        <Copyright>Phillip Smith</Copyright>
        <PackageLicenseExpression></PackageLicenseExpression>
        <ApplicationIcon>icon.ico</ApplicationIcon>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
        <DocumentationFile>D:\C#_Offline\MupenMovieEditor\MupenSharp\MupenSharp.xml</DocumentationFile>
    </PropertyGroup>

    <ItemGroup>
        <Content Include="icon.ico" />
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="JetBrains.Annotations" Version="2020.3.0" />
    </ItemGroup>

    <ItemGroup>
        <Reference Include="System.ComponentModel.DataAnnotations">
            <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.ComponentModel.DataAnnotations.dll</HintPath>
        </Reference>
    </ItemGroup>

    <ItemGroup>
        <None Include="..\..\Images\nugetIcon.png">
            <Pack>True</Pack>
            <PackagePath></PackagePath>
        </None>
    </ItemGroup>

    <ItemGroup>
        <Compile Update="Resources\ExceptionsResource.Designer.cs">
            <DesignTime>True</DesignTime>
            <AutoGen>True</AutoGen>
            <DependentUpon>ExceptionsResource.resx</DependentUpon>
        </Compile>
    </ItemGroup>

    <ItemGroup>
        <EmbeddedResource Update="Resources\ExceptionsResource.resx">
            <Generator>ResXFileCodeGenerator</Generator>
            <LastGenOutput>ExceptionsResource.Designer.cs</LastGenOutput>
        </EmbeddedResource>
    </ItemGroup>
</Project>

EDIT 2

Video demonstration:

https://youtu.be/3ZU-Ue-goaE

1 Answers

new-sdk project will automatically identify the type of file and then include it as an item in your project. So you don't have to define any items that are related to the file.

It is a new feature of new-sdk project.

Since you have defined your files through items under csproj file and want to use them without deleting these items, you should disable the automatic associated feature.

Add these under csproj file to disable it:

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

Besides, there is also a similar issue you can check it.

Update 1

Actually, EnableDefaultCompileItems to false just disables the global lookup feature(disable implicit association) and avoid duplicate recognition of items----Use the old feature under Net framework projects.

You have to manually uses the items from your csproj to include any items. If you do not write any items to include files, VS will turn out errors. And the feature is to improve the function. The new-sdk projects enable such automatic feature to simplify the trouble for developers.

Also, if you use EnableDefaultCompileItems to false, some compile items which you have defined before as implicit files will miss because the automatic association function is turned off and does not leave any mark in csproj.

However, these files still exists under your project. In this situation, you have to click show all files on the solution explorer and then include them in your project.

enter image description here

=================================================

Besides, the new sdk projects with the global lookup feature cannot work with compile include node. This is the double repeat import file.

In fact, it still has an alternative solution.

You can use update node.

Directly Use this without EnableDefaultCompileItems:

<ItemGroup>
    <Compile Update="Base\Class1.cs" />
</ItemGroup>

Note: update is a feature of new-sdk projects and only works for those which are already detected globally and found by the implicit association of the new-SDK project. If the files are not implicitly associated, then update will never work.

In other words, if the files you want to associate are outside the project (get rid of the implicit association of the project), then you can only use include to include these files from outside into inside projects, update only works for subsequent MSBuild operations as the implicitly associated file and this is equivalent to a supplement to the automatically associated files rather than re-importing, more likely an override operation.

Update acts on the inside project's files while Include acts on the outside project's files.

That is different from include node.

My project is picking up all .cs files perfectly fine within the project directory. However, whenever I ** ADD A NEW* class (right click >> add new item), the following is also added into the .csproj:

That means you already have set EnableDefaultCompileItems to false. With this, your csproj will have this include node.

If you still have this issue, please share your csproj file with us to help us troubleshoot your issue.

Related