Dave Chestnutt: SparklingCode and CodeGaffes

Writing better code; for fun and profit

  Home  |   Contact  |   Syndication    |   Login
  18 Posts | 1 Stories | 74 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 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!

# 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 11/8/2009 8:42 PM petty
Henry finally scored a goal! ugg bootsAfter a nearly six-month goal drought, the Henry finally Barcelona home match against Real Mallorca in the open and. ugg bootsHenry's goal with their feet after the fiercely kicked each other's goal column, the inner frustrations and unhappiness all vented out ... .11.09C



# re: Gotcha! Visual Studio Pre/Post-Build Events 11/12/2009 7:15 AM logo design
quite informative for a new user liek me!

# re: Gotcha! Visual Studio Pre/Post-Build Events 11/14/2009 6:00 PM remapping
thanks for this

# re: Gotcha! Visual Studio Pre/Post-Build Events 11/14/2009 6:01 PM remaps
good job thank you

# ddd 11/24/2009 7:52 PM adee
But there you have it. . wow power leveling wow power leveling

# add link directory 11/29/2009 9:52 AM add link
thanks for the useful post.

# free link directory 11/29/2009 9:54 AM free links
loved the blog keep it up.

# Mr 12/3/2009 2:50 AM Nicole Thompsen
Some of the highest selling replica Rolex watches are the Daytona replica Rolex watch, the Rolex replica Datejust, pandora charm replica Day Date, the Rolex replica Submariner and the Explorer series. If you want to find a good deal on a Rolex watch, you might think about buying one from somebody at an online auction. So who would not prefer to buy a replica Rolex watch when it has got all the qualities of a high quality watch? Only the manufacturers based on Switzerland could make a laser engraving which looks as good as the real Rolex crown. The Italian replicas.Replica Rolex watches allow this demand to be met. The Rolex replicas are very similar to genuine Rolex wristwatches. Some people who even have money to buy the original Rolex watches ,wouldn�t prefer if they had a choice of getting the same looks and status with the replica of that watch. Another reason which supports this kind of preference is that if they buy the replica , they can save the same amount of money for some other things like going on a vacation or buying a new car or any other luxurious stuff.You don�t know too much about replica watches, but this timepiece is really good, being a replica: it is very sharp, the second hand sweeps and the engraving are really good. The scam.There are quite a few high end wrist watches available in the market. They are considered as the ultimate status symbol of being a rich and famous personality.You would find many similarities between these two watches.

# re: Gotcha! Visual Studio Pre/Post-Build Events 12/6/2009 3:00 AM cialis canadian
useful pos

# re: Gotcha! Visual Studio Pre/Post-Build Events 12/6/2009 4:09 AM qerew




Great deal, ugg ultra short!
The ugg ultra tall was a excellent

condition. Thank you.


# re: Gotcha! Visual Studio Pre/Post-Build Events 12/14/2009 11:00 PM Fitness Trainer
Wonderful deal thanks for sharing indeed
New handbags


# re: Gotcha! Visual Studio Pre/Post-Build Events 12/15/2009 9:49 PM air max
Wonderful deal thanks for sharing indeed
AIR MAX TN SPECIAL<br>

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/5/2010 12:53 AM JessicaCW
From time to time different persons try to detect just some information referring to buying essays. Hence, I will offer to utilize the help of essay writing service. In fact, that’s ok to take some blocks from the this topic issue.

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/16/2010 11:13 PM Model Stardom
The new site will have some of these things you make good mention of. The site I am referring to is the new models site we plan to introduce soon.

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/21/2010 1:12 AM linksjuy
I think this is the best OS!!! Thank you guys!!!

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/22/2010 6:04 AM Custom Essays
Hi,
It must've taken you a bit of time, so thanks for taking the time to do so, I appreciate it, and this post is just great.

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/25/2010 1:44 AM discount links of london
Faithful links of london ladies always looking for discount links of london collections among UK, and we can get access to the discount collections like links of london bracelets and cheap links of london charms on the internet.

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/26/2010 1:58 AM hp battery
Very useful command, exactly solved the problem buzzered me for a long time.

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/26/2010 10:09 AM Oliviadu18
Are you willing to get resume company, that suit the range of research you wish?. You can count on our resume writers, as you trust yourself. Thanks because this is the good stuff

# re: Gotcha! Visual Studio Pre/Post-Build Events 1/30/2010 6:50 AM Video Game Reviews
found this software very useful indeed.

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