【问题标题】:EnableDefaultCompileItems issue. Compile Include items being added at build timeEnableDefaultCompileItems 问题。编译包括在构建时添加的项目
【发布时间】:2020-09-09 15:16:27
【问题描述】:

我目前正在尝试将旧的 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(如上),但即使在项目卸载/加载之后也没有任何反应。

如果我尝试通过“显示所有文件”来“包含在项目中”,那么我会在构建时添加一堆“编译包含”元素:

 <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

【问题讨论】:

  • 是Net Core项目的新特​​性,Net Core项目的csproj文件不同于旧的sdk格式。非常简洁,不需要添加任何项目include,Microsoft.NET.Sdk默认会自动关联所有源文件,无需手动编写,这是Net Core相对于NET Framework的优势。
  • 这个问题是从net framework迁移到net core的正常错误。正如微软文档所说,不要添加任何项目包括,你应该删除任何compile,none,...项目。将EnableDefaultCompileItems 设置为false 将隐藏所有编译文件,不建议这样做。使用&lt;PropertyGroup&gt;&lt;EnableDefaultItems&gt;true&lt;/EnableDefaultItems&gt;&lt;/PropertyGroup&gt; 显示所有文件。

标签: visual-studio msbuild


【解决方案1】:

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

这是新sdk格式项目的新特​​性,新sdk的csproj文件与旧sdk格式不同。非常简洁,不需要添加item include,如compile、none、Microsoft.NET.Sdk,默认会自动关联所有源文件,无需手动编写,这是新sdk格式的优点。

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

这会产生以下错误

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

所以最好将其设置为true,然后删除compile include、none include等任何项目节点。

或者,使用

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

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

然后,像这样删除任何 compile 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 2020-06-20
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 2011-09-29
    相关资源
    最近更新 更多