Tutorial :TFS MSBuild: $(ProjectDir) blank or random


I have a vcproj file that includes a simple pre-build event along the lines of:

Helpertask.exe $(ProjectDir)  

This works fine on developer PCs, but when the solution is built on our TFS 2008 build server under MSBuild, $(ProjectDir) is either blank or points to an unrelated folder on the server!

So far the best workaround I have managed is to hard code the developer and server paths instead:

if exist C:\DeveloperCode\MyProject   HelperTask.exe C:\DeveloperCode\MyProject  if exist D:\BuildServerCode\MyProject HelperTask.exe D:\BuildServerCode\MyProject  

This hack works in post-build steps but it doesn't work for a pre-build step (the Pre-build task now does nothing at all under MSBuild!)

Do you have any ideas for a fix or workaround? I have very little hair left!


$(MSBuildProjectDirectory) worked for me


I think your problem may be related to how items are initalized. An items include attribute is evaluated at the begining of a build. So if you depend on files that are created in the build process you must declare these as dynamic items. Dynamic items are those defined inside of a target, or by using the CreateItem task. I've detailed this on my blog MSBuild: Item and Property Evaluation.

Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build


I think the problem is that build server's workspace probably isn't initialized properly.


I just kept getting problems with this - I tried many different approaches but they all failed in mysterious ways.

Once $(ProjectDir) started behaving properly again, the pre-build step stopped executing the command (I added echo commands above and below it - they were both executed, but the program in between them was not. No errors or output of any kind were generated to indicate why it failed).

I don't know if this is a dodgy server of if MSBuild is having a laugh.

I've given up now. I gave the build server a big kick and have changed tack: We now run this tool offline (manually) and check in the results for the build server to use. So much for an automated build :-( If only MSBuild would run solutions in the same way as Visual Studio does - it's maddening that it sets up the environment completely differently (different paths coming out of the solution variables, ouptus redirected into different folders so you can't find them where they're supposed to be, etc)


I branched an existing project and $(ProjectDir) kept the old directory in the newly branched code. But that's because I had some compiling errors. Once every project in the solution compiled without errors, $(ProjectDir) changed to the correct path.

Carlos A Merighe

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »