How to Sidestep a Visual Basic Compiler Bug in RTM

A couple of weeks ago I ran into a very surprising bug in the Visual Basic compiler after upgrading a project from 2005 to 2008. To make a long debugging story shorter, I realized a line of code was not included in the IL output. This particular function call was missing regardless of how I rearranged the code in a Try block, etc. After confirming this with Reflector, I realized that there was a significant bug and worked as hard to preserve the problem as to solve it.

While I’m not happy that the VB compiler has such a serious bug, I am happy with the team’s response. I’m pleased in how they worked with my zipped project to confirm the problem and look beyond my immediate fix (a solution clean, worked out over IM with Bill McCarthy). I’m also happy that they quickly planned a resolution, even if it couldn’t be ready for the RTM release.

In case the magnitude of the problem isn’t clear yet, there is a scenario in which you cannot rely on fidelity between your source code and the IL that runs.

At least two things have to happen for your project to be vulnerable to this issue – you have a constrained generic in an assembly and that assembly is referenced by both project and file references. It’s not clear to me why this confuses the Visual Basic compiler – but it sometimes does and the reason is clear to the team.

The VB team plans a fix for SP1. Obviously that doesn’t help us today. You need to check your solutions for this mixed reference approach if you are using constrained generics in your common utility classes (which I’d encourage you to do because of the power of generics). There may be other conditions that have to also be in place for the bug that I don’t know about, but avoiding mixing file and project references is the easy way to break the offending cycle.

The team is also working on communicating this problem and probably including a KB article that explains the problem. I expect they will also supply a tool to identify mixed references and set them all to project references for all the goodness that provides to Visual Basic. I’ll let them share their future plans in the VB team blog. Since RTM is out now, I want information on this out so you can protect your projects.  

I do not believe you should avoid the RTM release of VB 9.0 becuase of this bug. I do think its important that you check your projects to ensure you don’t smack into this. And tell all your friends to do the same.

I’ll have more links here as if there’s any new information and certainly to the KB article when its out.


One thought on “How to Sidestep a Visual Basic Compiler Bug in RTM”

Leave a Reply

Your email address will not be published. Required fields are marked *