1

I am working on a customization of my build proces and I've come across one interesting behavior. When using string Property functions and altering some strings, the build starts to break.

For example, I'm trying to do this thing:

<PropertyGroup>
    <BuildDependsOn>$(BuildDependsOn.Replace(";MyTarget", ""))</BuildDependsOn>
</PropertyGroup>

The MyTarget is my custom target that I want to remove from the BuildDependsOn property. As I try to do this, the string seems to be removed correctly, but I am getting error that the target does not exist (this happens both in Visual Studio 2010 and via command-line MSBuild).

The error is following:

The target "EntityDeploy;

            BeforeBuild;
            CoreBuild;
            AfterBuild" does not exist in the project.

I think that once I modify this property, the MSBuild stops to parse its value and is trying to look for one target (including white-spaces, carriage returns and new line feeds) called:

"EntityDeploy;

        BeforeBuild;
        CoreBuild;
        AfterBuild"

and throws an error that such target does not exist.

I have also tried to perform something more trivial to find out more about this problem and I've tried to perform this:

<PropertyGroup>
    <BuildDependsOn>$(BuildDependsOn.Trim())</BuildDependsOn>
</PropertyGroup>

The error appeared again also with the Trim() function. Any idea?

EDIT: I have tried to debug and find out the problem, and I have come across this: When checking property BuildDependsOn before the execution of the Trim() method, its value is like this:

Value   "BuildDependsOn"="\r\n      EntityDeploy;\r\n      \r\n            BeforeBuild;\r\n            CoreBuild;\r\n            AfterBuild\r\n        \r\n    "
EvaluatedValue  "\r\n      EntityDeploy;\r\n      \r\n            BeforeBuild;\r\n            CoreBuild;\r\n            AfterBuild\r\n        \r\n    " string
escapedValue    "\r\n      EntityDeploy;\r\n      \r\n            BeforeBuild;\r\n            CoreBuild;\r\n            AfterBuild\r\n        \r\n    " string

After Trim() method, the property values are like this:

Value   "BuildDependsOn"="EntityDeploy%3b\r\n      \r\n            BeforeBuild%3b\r\n            CoreBuild%3b\r\n            AfterBuild"
EvaluatedValue  "EntityDeploy;\r\n      \r\n            BeforeBuild;\r\n            CoreBuild;\r\n            AfterBuild"   string
escapedValue    "EntityDeploy%3b\r\n      \r\n            BeforeBuild%3b\r\n            CoreBuild%3b\r\n            AfterBuild" string

Might it be that semicolons (; <=> %3b) are breaking the build? How can i fix this?

Matus Nemcik
  • 240
  • 2
  • 13
  • Maybe these links might help you:http://stackoverflow.com/questions/5103026/in-msbuild-can-i-use-the-string-replace-function-on-a-metadata-item and this one too http://msdn.microsoft.com/en-us/library/ms171458(v=vs.100).aspx – lnu Apr 05 '12 at 06:59
  • I know this OOB, but can't you do the idiomatic msbuild thing and define one property group with the target and one with out and then use the configuration to select which one is enabled? – Ritch Melton Apr 05 '12 at 09:49
  • @RitchMelton In normal cases I could ... In fact, I've posted a simplified version of the problem that I have. The thing is that when I include a 3rd party .Targets file, the 3rd party target is automatically added to the BuildDependsOn property. I want to remove it from this property (so that it is not run evertytime a project is built) and decide in another condition whether the target should be executed. – Matus Nemcik Apr 05 '12 at 11:45
  • @Matus- That target file doesn't have a flag (bool condition) you can reset before the include? – Ritch Melton Apr 05 '12 at 20:50

1 Answers1

4

I tried what you did(adding a trim to BuildDependsOn) and it's working.

 <BuildDependsOn>
  ConfigBeforeBuild;
  $(BuildDependsOn.Trim());
  ConfigAfterBuild
</BuildDependsOn>

Did you enabled debugging for msbuild? if not everything is explained here.

Ok, here's the solution(mine was not working either, my test was not correct):

<BuildDependsOn>
  $([MSBuild]::Unescape($(BuildDependsOn.Replace(";MyTarget", ""))))
</BuildDependsOn>   

You have to unescape your string in order to remove the %3b.

lnu
  • 1,404
  • 1
  • 9
  • 25
  • Now that is really strange, in my environment that definitely is not working ... I have updated the question with some results I found out during debugging ... it seems that semicolons are somehow breaking the build, at least that's what I have found out as a only difference ... – Matus Nemcik Apr 05 '12 at 08:14
  • Thank you - I've been fighting this exact problem for hours! – Fergus Bown Dec 06 '12 at 10:12