Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition on "PropertyGroup" in Directory.build.props not working

Tags:

msbuild

I've created a Directory.build.props file so I can set the C# language version in there. But I also have Visual Basic Projects, so i wanted to limit the setting to C# projects.

<Project>
    <PropertyGroup Condition="'$(ProjectExt)'=='.csproj'">
        <LangVersion>7.2</LangVersion>   
    </PropertyGroup>
</Project>

But my project is not loading it / the UI is not displaying the language version 7.2. I've tried to apply the same condition inside the csproj file, also not working.

<PropertyGroup>
    <LangVersion Condition="'$(ProjectExt)'=='.csproj'">7.2</LangVersion>
</PropertyGroup>

However, this will work:

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Message Text="Condition working" Importance="high" Condition="'$(ProjectExt)'=='.csproj'"/>
</Target>

The build will output my message

Why is the condition not working on my LanguageVersion? Any Clues?

like image 737
HankTheTank Avatar asked Feb 05 '19 13:02

HankTheTank


People also ask

What is directory build props?

Directory. Build. props is a user-defined file that provides customizations to projects under a directory.

How do I set properties in MSBuild?

MSBuild lets you set properties on the command line by using the -property (or -p) switch. These global property values override property values that are set in the project file. This includes environment properties, but does not include reserved properties, which cannot be changed.


1 Answers

You will need to use a property to condition on that is available very early in the build. In your case, you should condition on MSBuildProjectExtension:

<PropertyGroup>
  <LangVersion Condition="'$(MSBuildProjectExtension)'=='.csproj'">7.2</LangVersion>
</PropertyGroup>

See MSBuild reserved and well-known properties for the complete set of available properties.

ProjectExt is only defined late in the build definition and is therefore not available in Directory.Build.props, which is imported very early into the project.

like image 79
Martin Ullrich Avatar answered Nov 13 '22 15:11

Martin Ullrich