Question

I am using the NDepend 4.1 API to count the lines of code in a Visual Studio 2010 solution that has a few projects, which are all targeting the .NET Framework 4. Here's my code:

var servicesProvider = new NDependServicesProvider();
var projectManager = servicesProvider.ProjectManager;
var project = projectManager.CreateTemporaryProject( ... );
var analysisResult = project.RunAnalysis();

var methods = analysisResult.CodeBase.Application.Methods;
var totalLoc = methods.Where( i => i.NbLinesOfCode.HasValue ).Sum( i => i.NbLinesOfCode.Value );

If I repeatedly run this analysis on my solution, I get different results for totalLoc even though I have not changed the solution or its compiled assemblies in any way. For example, if I run the analysis three times, I might get totalLoc values of 12100, 12220, and 12075.

Digging a little deeper, I've started iterating through analysisResult.CodeBase.Application.Methods and writing each method's name and NbLinesOfCode to a text file. When I diff the files from any two analysis runs, I notice that most methods show a consistent LOC count but there are a handful that don't. I can't see any pattern in this and it doesn't seem to be the same set of methods each time.

What could be causing this strange behavior?

Update 1: If I run the analysis on any single assembly, I get a consistent LOC count. The problem only happens when I pass multiple assemblies to CreateTemporaryProject.

Était-ce utile?

La solution

This was a bug now fixed in NDepend version 5!

Interestingly enough the bug came from a subtil concurrent access issue, that manifested with different analysis results when 2 large assemblies were analyzed on two different threads!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top