One of the first things a developer is asked to do when starting on a new project is to download the code and get the software running. Far too often, that code will not run, or even compile, without specific steps that are known only by the original developer(s). But what if the original developer isn’t available to assist with the necessary computer configurations, build steps, or 3rd party installations to get a new development environment up and running? These debugging tips represent a proactive strategy to independently resolve a code base that is fraught with compile errors.
(Examples represent debugging in C# .NET and Visual Studio 2013)
Build One Project at a Time
One small compile error in the right project can cause numerous errors across the solution. We must isolate our efforts to the projects that are actually causing the problem. To do this, compile each project individually and see which are already working.
Fix Independent Projects First
Because compile errors cascade through to other projects that depend on the problematic code, we should work on those projects that are independent of others first. One way to determine those projects is to look at the project references and make sure it is not pointing to any of the other projects in this solution.
Right-click on References and click Add Reference. This opens the Reference Manager. This image shows other projects are unchecked, so this project is independent of the others.
Note: Visual Studio Ultimate Edition has a feature under the Architecture tab called “Generate Dependency Graph For Solution” that makes this easy. However, this only works if the solution already compiles.
Investigate Reference Paths
One of the two most common reasons why a project cannot compile is because a referenced library cannot be found. Either the file is simply missing from the source code or Visual Studio cannot resolve the path to it.
To troubleshoot this situation (you can see that the missing library has a yellow triangle icon), edit the .csproj file directly. Right-click on the project and choose Unload Project. Right-click the project again and choose Edit.
The xml for the .csproj file will load into the text editor. Search for the project reference that cannot be resolved. It is quite possible that the file exists in the source code, but a relative path has been configured that is different on the original developer’s machine. Take a look at the file path and make sure the file exists where the path points. If the path is inaccurate, either move the library or alter the .csproj file.
Search for 3rd Party Libraries
When a referenced library is not part of the source code, it might be downloadable from the Internet. Try Googling the name of the missing project to see if the library is commonly used and available. The NuGet Package Manager is part of Visual Studio and is another great source of .NET libraries.
Lastly, try to gain read access to the deployment directories of this application on the network. For example, it is possible that missing libraries are in the bin directory of the production source code.
Obtain Missing Source Code
The other most common reason why a project cannot compile is because a source file is missing or out of date. The easiest way to debug this .NET code is to contact the previous developer. The file is likely sitting on his or her computer waiting to be committed to source control. Unfortunately, if this is not an option, desperate (i.e. time-consuming) options are all that remain:
- Try looking through the source control history log. Perhaps the file was once there but has been accidentally deleted or changed.
- Go back to the production server files and see if the file exists there. If not, go a step further and decompile production code. Perhaps the decompiled code can be inserted into the development code to resolve class or logic dependencies.
Follow the above tips to tackle a non-compiling Visual Studio solution. Hopefully, these ideas aid your debugging in C# and revive an otherwise overwhelming code base.