Search code examples
msbuildmsbuild-task

msbuild empty property value is threated as null value


I have a task configuration property RelativePathOverride defined like this:

  <PropertyGroup>
    <RelativePathOverride>..\..\</RelativePathOverride>
  </PropertyGroup>

and then used as

  <Target Name="CustomTaskTarget" AfterTargets="PostBuildEvent">
    <SimpleTask RelativePathOverride="$(RelativePathOverride)"/> 
  </Target>

The task code is following:

public class SimpleTask : Task
{
    public string RelativePathOverride { get; set; }
    

    public override bool Execute()
    {
        Log.LogMessage(MessageImportance.High, $"RelativePathOverride: {RelativePathOverride ?? "NULL"}");

        if (RelativePathOverride == null)
        {
            // default value
            RelativePathOverride = "..\\";
        }

    }
}

This works fine. However, the problem is that when I provide the empty value for the RelativePathOverride property then it's defaulted to NULL!

  <PropertyGroup>
    <RelativePathOverride></RelativePathOverride>
  </PropertyGroup>

In my logic - I want the empty value to be an empty value! This is very important because we are talking about a relative path. NULL value means that there is NO override provided, so the default will be hardcoded to ..\. But since empty property value is also threated as null then this corrupts my logic..

Is there is native approach to allow passing empty values into property?

p.s. on a side note (might be related) - 1 space is also threated as null..


Solution

  • In most instances, comparisons in msbuilds usually interpret "empty" and null as the same thing. I suggest using .\ or . in your case.