Dave Chestnutt: SparklingCode and CodeGaffes

Writing better code; for fun and profit

  Home  |   Contact  |   Syndication    |   Login
  18 Posts | 1 Stories | 47 Comments | 21 Trackbacks

News

Tag Cloud


Article Categories

Archives

Post Categories

Visual Studio has a nifty feature called Pre-Build and Post-Build events. These are used to include extra DOS commands before or after the build.

But there's a gotcha! And it will bite you when you least expect it. In Visual Studio, there is NO ERROR CHECKING except at the end of an event. Any errors that happen prior to the final step are lost. Keep reading to see a workaround.

Setting Build Events

Build Events are accessed by right-clicking on a Project in the solution explorer and choosing "Build Events":

It looks like these were originally intended as one-line DOS commands to do tweaks in your build. But they obviously grew - and you can put any number of DOS commands in there to copy files, clean up directories, even run unit tests!

Before describing the gotcha, there are a few things you need to know if you've never used these before. One is that you can put any DOS commands in here. It's really nothing more than a place to save a batch file that's run before (or after) the project build. It also offers "macros". The build process actually creates a batch file from your commands by replacing the macro text.

In this example, MYDEPLOY is just a regular DOS environment variable. The advantage to macros is they take into account the various differences between developers (and their file directory structure) as well as handling Debug vs. Release builds.

For example, this command:

Copy $(OutDir)*.* %MYDEPLOY%

Gets converted to this, and placed in a batch file:

Copy bin\Debug\*.* %MYDEPLOY%

The Gotcha

So, what's the problem?

Well, VS puts these commands in a batch file and runs it. VS checks for build failures so it can tell you about them. However - it only checks for a failure of the batch file that it created - not for any failures within the batch file.

What does this mean? If you only place one command in the build event, VS will correctly report any errors that happen. But if you place multiple commands in here, the net effect is that VS will only detect an error in the last statement.

So, for example, if you put three copy commands in here, VS will only report an error if the final command fails.

Ouch.

The Workaround

So how can we fix this? Since VS is just creating a batch file, we use normal batch file commands to detect errors and report them. Here's what I do:

  • I add a check for error after every meaningful step:
if errorlevel 1 goto BuildEventFailed
  • I add code at the end to handle the error so VS will report it:
REM Exit properly because the build will not fail
REM unless the final step exits with an error code
goto BuildEventOK
:BuildEventFailed
echo POSTBUILDSTEP for $(ProjectName) FAILED
exit 1
:BuildEventOK
echo POSTBUILDSTEP for $(ProjectName) COMPLETED OK


Here's an example of one of my actual post build events:

echo POSTBUILDSTEP for $(ProjectName)
 
xcopy "$(TargetPath)" "$(SolutionDir)$(OutDir)" /i /d /y
if errorlevel 1 goto BuildEventFailed
xcopy "$(TargetDir)$(TargetName).pdb" "$(SolutionDir)$(OutDir)" /i /d /y
if errorlevel 1 goto BuildEventFailed
 
echo GAC of: "$(SolutionDir)$(OutDir)$(TargetFileName)"
gacutil.exe /nologo /i "$(SolutionDir)$(OutDir)$(TargetFileName)" /f
if errorlevel 1 goto BuildEventFailed
 
REM Exit properly because the build will not fail 
REM unless the final step exits with an error code
goto BuildEventOK
:BuildEventFailed
echo POSTBUILDSTEP for $(ProjectName) FAILED
exit 1
:BuildEventOK
echo POSTBUILDSTEP for $(ProjectName) COMPLETED OK

You don't have to be as fancy, with the extra "echo" statements, but do the error checking you need so errors don't slip through the cracks.

One final note: don’t put too much in these events. They’re pretty hard to debug when things go wrong. If you have something complex to do, put the commands in a batch file and reference the batch file yourself in the event.

But now at least, you can get notified if (make that when) something fails.

Technorati tags: , ,

posted on Tuesday, May 30, 2006 11:23 AM

Feedback

# VSTS Links - 05/31/2006 5/31/2006 6:03 AM Team System News
The State and Local Government Developers blog talks about the Requirements Authoring Starter Kit (RASK)....

# re: Gotcha! Visual Studio Pre/Post-Build Events 6/15/2006 7:47 PM Mike
Dude, That's great! Just what I needed.

Thanks!

# re: Gotcha! Visual Studio Pre/Post-Build Events 6/28/2006 10:47 PM JS
Thank you. I haven't used more than one command so far, but it is great to know that multiple commands can be used with a full build report.

# Visual Studio Pre/Post-Build Events 7/26/2006 8:20 AM Jeff Stong
Recently, I was working with a Visual Studio 2003 project that used a post-build event to perform a number...

# re: Gotcha! Visual Studio Pre/Post-Build Events 5/12/2007 6:28 AM Converse
Thanks. It is what I was looking for.

# re: Gotcha! Visual Studio Pre/Post-Build Events 2/14/2008 2:37 PM Dal Hit
Hi Dude, i am coming accross similar problem.

I have VS 2005 with .NET 2,3,3.5. on Win 2K3 Entp Edtn

In post build event, i am running below command
copy "$(ProjectDir)Tests\Microsoft.Practices.EnterpriseLibrary.Configuration.dll.config" "$(TargetDir)"

But it exits with following error:

Error 71 The command "copy 'C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\Tests\Microsoft.Practices.EnterpriseLibrary.Configuration.dll.config' 'C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\bin\ReleaseFinal\'" exited with code 1. Configuration.prod


Even i replace above Macro-Directories with actual path, as below, but still i am getting error.

copy "C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\Tests\Microsoft.Practices.EnterpriseLibrary.Configuration.dll.config" "C:\_Src\LPO_ECM\HERMESDEV01_EcmBuild\Sources\Source\EnterpriseLibrary\Configuration\bin\ReleaseFinal\"


Also i cheked that Source folder has necessary files & destination folder is writtable.

I am not able to understand what wrong here..!!!

In case anyone can throw some light on this issue,
please reply me : dal_hit@yahoo.com

Thanks
Dal Hit

# re: Gotcha! Visual Studio Pre/Post-Build Events 4/14/2008 8:41 AM vvill
a little tidbit. if you have gacutil in your post build events and the gacutil.exe is not in your system's environment path variable, and you add it, you'll have to restart VS (2005) to get it to work.

# re: Gotcha! Visual Studio Pre/Post-Build Events 4/29/2008 7:12 PM amol
I am getting error when try to compile code using VS 2k5.

I have written post script event in this project but it is not working properly as I change the location of project.

The script has function to copy current project's dll to central folder where other project's can refer this dll.



# re: Gotcha! Visual Studio Pre/Post-Build Events 6/11/2008 11:39 PM sandburg
In which file is it stored ?
It don't pass with the sln or vcproj, we use a subversion repository.

# re: Gotcha! Visual Studio Pre/Post-Build Events 8/29/2008 1:15 AM Steeve Gauvreau
I tried it with environment variable like %MYDEPLOY%. And he did not convert my variable. I'm I missing something?

# re: Gotcha! Visual Studio Pre/Post-Build Events 8/29/2008 3:35 AM Dave Chestnutt
add this line as the first line:
ECHO MYDEPLOY=%MYDEPLOY%

That way you can tell if your environment variable is making it all the way thru to the process that runs this. If it's not, you'll have to figure out how to set it so it makes it to that process.

# re: Gotcha! Visual Studio Pre/Post-Build Events 8/29/2008 11:58 PM rüya tabiri
Thank You

# re: Gotcha! Visual Studio Pre/Post-Build Events 8/29/2008 11:59 PM parke
Thank You :)

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/5/2008 12:40 AM Michael
Does anyone know anyway to keep the post build event out of a VSS checkin?

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/23/2008 9:25 AM Stevel
This project is not working properly as I change the location of project. The script has function to copy current project's dll to central folder

# re: Gotcha! Visual Studio Pre/Post-Build Events 10/6/2008 9:23 PM asp.net
good review

# re: Gotcha! Visual Studio Pre/Post-Build Events 10/24/2008 6:05 AM Canon
Easy and useful, thx.

# re: Gotcha! Visual Studio Pre/Post-Build Events 12/6/2008 12:50 AM dasa
One of the best file searchers and download centers is here http://newfileengine.com/
Find al the necessary information there!



# re: Gotcha! Visual Studio Pre/Post-Build Events 2/26/2009 3:30 AM Adina Schisler
THANK YOU! I never noticed this stuff and it so gave me a headache, especially on old projects that come down the pipe from VSS setup like that.

# re: Gotcha! Visual Studio Pre/Post-Build Events 3/14/2009 2:45 AM dll
This tutorial should be put in a place of honor.
The best!!!

# How to debug visual studio pre / post build events 5/18/2009 8:02 AM Ravinder
Hi,

This is Ravinder, I have two solutions. second solution is using the first solution(console application) in Build Events. Means when i will build the second solution first solution exe is get excuted.
my question is here out to debug the first solution(i.e exe) when i build the second solution.

Regards,
Ravinder

# re: Gotcha! Visual Studio Pre/Post-Build Events 5/18/2009 1:08 PM Dave Chestnutt
Good luck. I would suggest re-thinking your process. Some ideas:

Create 3 solutions: the two you have now, and a third one that simply includes all the projects. Then, as a developer, you would load which ever solution was appropriate.

Create a batch file that builds both solutions.

I don't think you'll be able to reliably detect and display errors from the inner build.

# re: Gotcha! Visual Studio Pre/Post-Build Events 8/5/2009 12:38 AM links of london
I have two solutions. second solution is using the first solution(console application) in Build Events. Means when i will build the second solution first solution exe is get excuted.


# linsk of london 8/26/2009 8:11 PM links of london
Thank you!

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/1/2009 9:43 PM links of london
Thanks for your information!

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/9/2009 11:45 PM Sudarsan Srinivasan
Thanks, was really helpful

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/14/2009 4:15 PM rüya tabirleri
Thanks commands

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/23/2009 4:22 PM Muralidhar
I want to do some transformation (xslt). Like, read the resources files and display in an html file or something siliar..How do I do that

# re: Gotcha! Visual Studio Pre/Post-Build Events 9/24/2009 5:16 AM Sell Gold
Great tutorial thank you. I love using Visual Studios.

# re: Gotcha! Visual Studio Pre/Post-Build Events 10/10/2009 8:44 AM medical tourism
Great info. Visual Studio hasn't failed me yet!

# tiffany 10/12/2009 3:11 AM Tiffany charms
It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

# ed hardy 10/19/2009 8:24 PM ed hardy Shoes
Great info. Visual Studio hasn't failed me yet!


# re: Gotcha! Visual Studio Pre/Post-Build Events 10/19/2009 10:28 PM links
Introducing gorgeous new links of london jewellerycolours to our ever expanding collection of Friendshiplinks of london bracelet Bracelets. Hand-woven with purple and pink thread,


# re: Gotcha! Visual Studio Pre/Post-Build Events 10/21/2009 10:00 PM links
"Encircle your wrist<a href=""http://www.linksoflondonstore.com/sweetie-bracelets"">links of london sweetie bracelets
with this charming Sweetie Bracelet from Links of London. Sterling silver rings are strung in glossy perfection and interspersed"


# re: Gotcha! Visual Studio Pre/Post-Build Events 10/27/2009 7:28 PM links of london Necklace
It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.

# re: Gotcha! Visual Studio Pre/Post-Build Events 10/29/2009 10:31 PM tiffany jewellery
perfect.Just what I needed.

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: