C
C#4w ago
Cyclomatic

Conditional ItemGroup in .csproj (SDK style)

I'm trying to add this conditional ItemGroup
<ItemGroup Condition="'$(IsPacking)' != 'true' OR '$(MSBuildProjectBuild)' == 'true'">
<PackageReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
<DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(IsPacking)' != 'true' OR '$(MSBuildProjectBuild)' == 'true'">
<PackageReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
<DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="9.0.1" />
</ItemGroup>
Basically, want to use the ItemGroup if I have not run dotnet pack OR I have run dotnet build. Any ideas why this isn't working? Even just using MSBuildProjectBuild check doesn't work for me.
2 Replies
wasabi
wasabi4w ago
So, PackageReferences can't really be conditionalized successfully on a whole lot. Kinda depends what those properties do. But it's important to understand that PackageReferences are processsed during Restore. ie, if you do a Restore, and set one set of variables, but then do either a Build or Publish with a different set, something is going to break. And during this Restore pass, since it's job is to resolve PackageReferences themselves, the autogenerated nuget targets and props files which represents the imports from other PackageReferences won't exist yet.
Cyclomatic
CyclomaticOP4w ago
Thank you

Did you find this page helpful?