dotnet/templating

Condition on csproj ItemGroup item removes entire line from output

krjone01 opened this issue · 2 comments

Product

dotnet CLI (dotnet new)

Describe The Bug

When using a condition on a msbuild ItemGroup item in a template csproj file, the entire line is removed from the output csproj file. This is a "Microsoft.NET.Sdk.Web" project file.

The fragment from the template csproj file

  <!-- main app -->
  <ItemGroup>
    <NgAppInput Include="$(SpaRoot)\src\**" />
    <NgAppOutput Include="$(SpaRoot)\dist\main\**" Exclude="$(SpaRoot)\dist\main\favicon.ico" />
         <!-- Item group can't be empty, but the file does not have to exist. Use fake file for no output files -->
         <!--
      For some unknown reason the line below is being removed from the template output
      You may have to copy and paste it back into the project file.
      <NgAppOutput Include="$(SpaRoot)\fake.file" Condition="@(NgAppOuput->Count()) == 0" />
     -->
    <NgAppOutput Include="$(SpaRoot)\fake.file" Condition="@(NgAppOuput->Count()) == 0" />
  </ItemGroup>

This is the fragment from the csproj file after dot net new

 <!-- main app -->
  <ItemGroup>
    <NgAppInput Include="$(SpaRoot)\src\**" />
    <NgAppOutput Include="$(SpaRoot)\dist\main\**" Exclude="$(SpaRoot)\dist\main\favicon.ico" />
         <!-- Item group can't be empty, but the file does not have to exist. Use fake file for no output files -->
         <!--
      For some unknown reason the line below is being removed from the template output
      You may have to copy and paste it back into the project file.
     -->
  </ItemGroup>

Note that not only is the line removed from the project file, its also removed from the XML comment so it seems like some sort of parsing issue.

I have messed around with the condition and the only reliable way to get the line to come out is to remove any kind of comparison from the condition. Of course, this does not produce the desired result.

  <!-- main app -->
  <ItemGroup>
    <NgAppInput Include="$(SpaRoot)\src\**" />
    <NgAppOutput Include="$(SpaRoot)\dist\main\**" Exclude="$(SpaRoot)\dist\main\favicon.ico" />
                  <!-- Item group can't be empty, but the file does not have to exist. Use fake file for no output files -->
                  <!--
      For some unknown reason the line below is being removed from the template output
      You may have to copy and paste it back into the project file.
      <NgAppOutput Include="$(SpaRoot)\fake.file" Condition="@(NgAppOuput->Count())" />
     -->
    <NgAppOutput Include="$(SpaRoot)\fake.file" Condition="@(NgAppOuput->Count())" />
  </ItemGroup>

To Reproduce

Steps:

  1. Create an ItemGroup in the csproj template file
  2. Add a ItemGroup item with a valid condition attribute
  3. Process the template through dot net new

dotnet Info

output NET SDK:

Version: 7.0.400

Commit: 73bf45718d

Runtime Environment:

OS Name: Windows

OS Version: 10.0.19045

OS Platform: Windows

RID: win10-x64

Base Path: C:\Program Files\dotnet\sdk\7.0.400\

Host:

Version: 7.0.10

Architecture: x64

Commit: a6dbb800a4

.NET SDKs installed:

3.1.426 [C:\Program Files\dotnet\sdk]

6.0.413 [C:\Program Files\dotnet\sdk]

7.0.400 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:

Microsoft.AspNetCore.App 3.1.32 C:\Program Files\dotnet\shared[Microsoft.AspNetCore.App]

Microsoft.AspNetCore.App 5.0.17 C:\Program Files\dotnet\shared[Microsoft.AspNetCore.App]

Microsoft.AspNetCore.App 6.0.21 C:\Program Files\dotnet\shared[Microsoft.AspNetCore.App]

Microsoft.AspNetCore.App 7.0.10 C:\Program Files\dotnet\shared[Microsoft.AspNetCore.App]

Microsoft.NETCore.App 3.1.32 C:\Program Files\dotnet\shared[Microsoft.NETCore.App]

Microsoft.NETCore.App 5.0.17 C:\Program Files\dotnet\shared[Microsoft.NETCore.App]

Microsoft.NETCore.App 6.0.15 C:\Program Files\dotnet\shared[Microsoft.NETCore.App]

Microsoft.NETCore.App 6.0.21 C:\Program Files\dotnet\shared[Microsoft.NETCore.App]

Microsoft.NETCore.App 7.0.10 C:\Program Files\dotnet\shared[Microsoft.NETCore.App]

Microsoft.WindowsDesktop.App 3.1.32 C:\Program Files\dotnet\shared[Microsoft.WindowsDesktop.App]

Microsoft.WindowsDesktop.App 5.0.17 C:\Program Files\dotnet\shared[Microsoft.WindowsDesktop.App]

Microsoft.WindowsDesktop.App 6.0.15 C:\Program Files\dotnet\shared[Microsoft.WindowsDesktop.App]

Microsoft.WindowsDesktop.App 6.0.21 C:\Program Files\dotnet\shared[Microsoft.WindowsDesktop.App]

Microsoft.WindowsDesktop.App 7.0.10 C:\Program Files\dotnet\shared[Microsoft.WindowsDesktop.App]

Other architectures found:

x86 [C:\Program Files (x86)\dotnet]

registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:

Not set

global.json file:

Not found

Learn more:

https://aka.ms/dotnet/info

Download .NET:

https://aka.ms/dotnet/download

Visual Studio Version

VS 2022 Pro

Additional context

No response

Reposted to dotnet/sdk repo. New Bug Report

Closing here.

Perhaps this documentation helps: Ignore conditions expressions in MSBuild files
I have not tried it.