EnableDefaultCompileItems 问题。编译包括在构建时添加的项目

EnableDefaultCompileItems issue. Compile Include items being added at build time

我目前正在尝试将旧的 csproj 文件迁移到 dotnet 核心样式文件。

我有一个基本的 csproj 文件,例如

<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 对此很满意,但排除了我的所有代码。我已尝试将 EnableDefaultCompileItems 显式设置为 true(如上所述),但即使在项目卸载/加载后也没有任何反应。

如果我尝试通过 'Show All Files' 'Include in Project' 然后我会在构建时添加一堆 'Compile Include' 元素:

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

产生以下错误

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   
    

这似乎表明 VS 已经知道该文件存在但未在解决方案资源管理器中显示它或将其包含在编译中。以前有人遇到过这个问题吗?

VS 版本信息:

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

EnableDefaultCompileItems issue. Compile Include items being added at build time

新sdk格式项目的新特​​性,新sdk的csproj文件与旧sdk格式不同。非常简洁,不需要像compilenoneMicrosoft.NET.Sdk这样的item include,默认会自动关联所有源文件,不用手动写,这是优点这种新的 SDK 格式。

请参考this document about how to migrate old net framework csproj file into the new sdk format

Which produces the following error

注意:虽然将EnableDefaultCompileItems设置为false可以避免这个错误,但它会隐藏解决方案资源管理器中的文件。

所以最好设置为true,然后删除任何项目节点,如compile includenone include等。

,使用

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

在解决方案资源管理器中显示所有排除文件。

然后,像这样删除任何编译 xml 节点:

 <Compile Include="Class1.cs" />

整个csproj文件,参考这个:

<?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>