Thursday, January 29, 2009

Raising some warnings as errors

Today a program just threw an unexpected exception : dll not found.
First I was amazed, how could this happen? How can a program in .Net complain at run-time about a missing dll. I mean how could the program be compiled without this dll?
Turned out that the programmer used some lazy loading, and the compiler (msbuild) DID warn about this, but it is a warning, not an error (warning MSB3245).

I really wanted to fix this situation, I wanted this specific warning to be threated as an error. It turned out to require some creative work.
At work I'm programming with VB.Net, and the compiler options are a bit different than those of C#. In VB.Net you have an option to threat ALL warnings as errors, but this is not an option (yet). There are some warnings, for which we do not have found a way to remove them, and they do not break the program or so.
Another option was to manually place in the project file (using notepad) each warning-number that should be handled as an error.
Now this is not a real solution : this meant changing 200+ projects, and when there is another warning that should be threated as an error, we must again change all project files.

So I was thinking of passing an extra argument to MSBuild, but there is no such argument. With csc, you have the /warnaserror option, but not in msbuild :-(.

The solution I came up with :
° pass another extra argument to msbuild :
/fl /flp:warningsonly;logfile=warnings.txt
this creates a new file warnings.txt that contains only the warnings
° in my current build script, add an extra task that parses this warnings.txt file and checks for certain warning numbers. If it contains a line that matches a list of predefined warnings, fail the build.

Should the list of warnings ever be changed, this is a very small update. Just change this list in every build script. And since i have a program that creates the build scripts, this is just 1 minute of work ;-)

No comments:

Post a Comment