dotnet / msbuild

The Microsoft Build Engine (MSBuild) is the build platform for .NET and Visual Studio.
https://docs.microsoft.com/visualstudio/msbuild/msbuild
MIT License
5.22k stars 1.35k forks source link

Conditons on ItemGroups Don't Work on Non-SDK Projects #10880

Open mruiz2112 opened 3 days ago

mruiz2112 commented 3 days ago

Issue Description

Adding a condition on an ItemGroup, i.e. <ItemGroup Condition="false">, does not work for non-SDK projects. The condition seems to be ignored and whatever is inside the ItemGroup will be added/executed. I've noticed this to be true within a csproj file as well as in a Directory.Packages.props file.

My company has a few large non-SDK web apps that we can't upgrade to SDK style so it would be nice to have this fixed.

Steps to Reproduce

WebApplication_NonSDK.zip

I uploaded a small sample solution with an ItemGroup, <ItemGroup Condition="false">, that adds a PackageReference. Despite the condition clearly evaluating to false, the PackageReference is added anyway. Changing the syntax to this fixes the issue:

<Choose>
    <When Condition="false">
      <ItemGroup>
        <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
      </ItemGroup>
    </When>
  </Choose>

However, it is confusing and inconvenient that the conditional on the ItemGroup does not work, especially since it only seems to affect non-SDK projects.

Expected Behavior

The PackageReference is NOT added to the project

Actual Behavior

The PackageReference is added to the project

Analysis

No response

Versions & Configurations

No response

KalleOlaviNiemitalo commented 3 days ago

Are you seeing this in Visual Studio, MSBuild.exe, or dotnet msbuild? I remember VS not supporting Condition on ItemGroup, but AFAIK it works fine in command-line builds.

mruiz2112 commented 3 days ago

Visual Studio. I haven't tried on MSBuild.exe on dotnet msbuild yet but I can try that and report back with my findings

mruiz2112 commented 3 days ago

Seems like the problem is isolated to Visual Studio. I also tested with Jetbrains Rider and didn't run into this issue

KalleOlaviNiemitalo commented 3 days ago

https://github.com/dotnet/project-system/issues/1542 looks similar, but it was closed in lieu of https://github.com/dotnet/project-system/issues/2129, which does not mention Condition.