Monday, May 19, 2008

MSBuild v3.5 ToolsVersion Attribute Gotchas

I just spent an hour debugging a project trying to use the new Properties and AdditionalProperties metadata in MSBuild v3.5.  (MSDN)

This special metadata is used to pass properties to MSBuild projects that are invoked using the MSBuild task.



<Project DefaultTargets="Test" xmlns="" ToolsVersion="3.5">
    <Project Include="b.proj">

  <Target Name="Test">
    <MSBuild Projects="@(Project)" />


<Project DefaultTargets="Test" xmlns="" ToolsVersion="3.5">
  <Target Name="Test">
    <Message Importance="High" Text="Foo=$(Foo)" />

Then when you run %SystemRoot%\Microsoft.NET\Framework\v3.5\MSBuild.exe a.proj, it should print out: Foo=123.  Woot!

We have successfully passed some properties to a recursive invocation of MSBuild.

ToolsVersion is Essential

Now what happens if you omit ToolsVersion="3.5".

Well, it you omit it from b.proj then the script will still work.

But it you omit it from a.proj MSBuild will run in a v2.0 compatibility mode.  So the value of the Foo property will NOT be passed into b.proj.  Not too surprising but it took me a little while to track it down.

This just goes to show how important the ToolsVersion attribute is.  Be careful!

No comments: