vNext Build editor filePath control always returns a path even if you did not set a value

You can use the filePath type in a vNext VSTS/TFS task as shown below 

1{  
2     "name": "settingsFile",  
3     "type": "filePath",  
4     "label": "Settings File",  
5     "defaultValue": "",  
6     "required": false,  
7     "helpMarkDown": "Path to single settings files to use (as opposed to files in project folders)",  
8     "groupName":"advanced"  
9   }  

to present a file picker dialog in the build editor that allows the build editor to pick a file or folder in the build’s source repository

image

While doing some task development recently I found that this control did not behave as I had expected

  • If a value is explicitally set then the full local path to selected file or folder (on the build agent) is returned e.g. c:agent_work3syourfolderyourfile.txt – just as expected
  • If you do not set a value, or set a value then remove your setting when you edit a build, then you don’t get an empty string, as I had expected. You get the path to the BUILD_SOURCESDIRECTORY e.g. c:agent_work3s – makes sense when you think about it.

So, if as in my case, you wanted to have specific behaviour only when this values was set to something other than the repo root you need to add some guard code

1  
2if ($settingsFile -eq $Env:BUILD\_SOURCESDIRECTORY )  
3{  
4    $settingsFile = ""  
5}  

Once I did this my task behaved as a needed, only running the code when the user had set an explicit value for the settings file.