2

I have an MsBuild (csproj) file in which I do some custom conditional items and targets. Everything runs fine for one configuration but I get an error for others that are cloned from the original. The error comes from the fact a custom variable out of a regex does not get set.

Here are the main excerpts from the MsBuild script:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N1|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N1</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="Site Extractor">
    <MySite>$([System.Text.RegularExpressions.Regex]::Match($(DefineConstants), '(?&lt;=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N2|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N2</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="N1 Specifics" Condition="$(MySite) == 'N1'">
    <SharedResPath>..\..\Resources\N1.1</PathPilotageKX>
  </PropertyGroup>

  <PropertyGroup Label="N2 Specifics" Condition="$(MySite) == 'N2'">
    <SharedResPath>..\..\Resources\N2.0</PathPilotageKX>
  </PropertyGroup>

  <Target Name="My Validations" BeforeTargets="BeforeBuild">
    <Message Text="DefinedConstants: [$(DefineConstants)]" />
    <Message Text="Site: [$(MySite)]" />
    <Message Text="Shared Res Path: [$(SharedResPath)]" />
    <Message Text="Configuration: $(Configuration)" />
    <Error Text="Site code (SITE_$(MySite)) is not supported by csproj (MSBuild)." Condition="$(SharedResPath) == ''" />
  </Target>

Validation task result is:

   My Validations:
     DefinedConstants: [TRACE;SITE_N1]
     Site: []
     Shared Res Path: []
     Configuration: N1
##[error]MyProject.csproj(352,5): Error : Site code (SITE_) is not supported by csproj (MSBuild).

Edit 1:

After some more fiddling, it looks like it is not related to Azure DevOps after all since I was able to reproduce the problem on the dev machine; so I removed every mentions of it. Also, it does not seem to neither be related to the value of DefineConstants since copying the value from one that is working to one that is not and vice-versa doesn't change anything.

Edit 2

Since the order of the elements has importance after all, I edited the excerpts to represent what was really in my project. Sorry for misleading, I didn't realize it had importance since my focus was elsewhere.

1 Answer 1

1

I hope the original question was not too misleading though I'm not sure the culprit could have been identified with it the way it was originally presented. (I modified the question to represent the real situation).

So the thing is, the order in which the PropertyGroup blocks appear is important. The csproj I modified was created using Visual Studio and the new configurations (in that case N2) were added to the end of the file but I had the custom property (MySite) set at the top close to the original property group (here N1).

Concretely, if I have

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N1|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N1</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="Site Extractor">
    <MySite>$([System.Text.RegularExpressions.Regex]::Match($(DefineConstants), '(?&lt;=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N2|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N2</DefineConstants>
  </PropertyGroup>

DefineConstants is not set yet when the regex attemps to extract the value. If instead I define the project with

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N1|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N1</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N2|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N2</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="Site Extractor">
    <MySite>$([System.Text.RegularExpressions.Regex]::Match($(DefineConstants), '(?&lt;=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
  </PropertyGroup>

Everything now works fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.