Substring under PropertyGroup in msbuild props not working when property is passed as argument to msbuild

998 views Asked by At

In my props file, i pass the BUILD_VERSION to dotnet msbuild using properties as /p:BUILD_VERSION=1.2.3.4. However, i get errors on when performing Substring operation on property BuildVersion which gets its value from : BUILD_VERSION.

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <IntermediateOutputPath>./obj/</IntermediateOutputPath>
    <BuildVersion>$(BUILD_VERSION)</BuildVersion>
    <InformationalVersion>$(BuildVersion)</InformationalVersion>
    <BuildMajorVersion>$([System.String]::Concat($(BuildVersion.Substring(0, $(BuildVersion.IndexOf('.')))), ".0.0.0"))</BuildMajorVersion>
    <AssemblyVersion>$(BuildMajorVersion)</AssemblyVersion>
    <AssemblyFileVersion>$(BuildMajorVersion)</AssemblyFileVersion>
  </PropertyGroup>

The error that i get is : """.Substring(0,-1)" length should be inside the string

I checked that BUILD_VERSION is present by printing in it in Message.

  <Target Name="SetBuildVersions" BeforeTargets="BeforeBuild">
    <Message Importance="High" Text="BUILD_VERSION=$(BUILD_VERSION)"/>
  </Target>

It prints BUILD_VERSION=1.2.3.4.

If i hardcode <BuildVersion>1.2.3.4</BuildVersion> instead of <BuildVersion>$(BUILD_VERSION)</BuildVersion> below, it works.

If i use the Substring code inside a Target it works :

  <Target Name="SetBuildVersions" BeforeTargets="BeforeBuild">
    <PropertyGroup>
      <InformationalVersion>$(BuildVersion)</InformationalVersion>
      <BuildMajorVersionOnly>$([System.String]::Concat($(BuildVersion.Substring(0, $(BuildVersion.IndexOf('.')))), ".0.0.0"))</BuildMajorVersionOnly>
      <AssemblyVersion>$(BuildMajorVersionOnly)</AssemblyVersion>
      <AssemblyFileVersion>$(BuildMajorVersionOnly)</AssemblyFileVersion>
    </PropertyGroup>
  </Target>

Why can't i use the Substring inside normal PropertyGroup when passing BUILD_VERSION as property argument to msbuild ?

0

There are 0 answers