ArchiMetrics
ArchiMetrics copied to clipboard
Missing root element.
Hey team.
This is a great piece of work, and i am finding some real value in it thank you!
I am trying to calculate both the Halstead Metrics as well as Normal code metrics, but everytime i try to to calculate codemetrics on a SyntaxTree i get a "Missing root element" error from an xml parser.
var metricsCalculator = new ArchiMetrics.Analysis.Metrics.SyntaxMetricsCalculator();
var codeCalculator = new CodeMetricsCalculator();
var calculateTasks = projects.SelectMany(a => a.Documents.Where(z => z.SupportsSyntaxTree)).Select(z => z.GetSyntaxRootAsync());
var metrics = (await Task.WhenAll(calculateTasks).ContinueWith(a =>
a.Result.Select(q => new
{
Halstead = metricsCalculator.Calculate(q),
SyntaxTree = q.SyntaxTree,
Project = projects.FirstOrDefault(p =>
q.SyntaxTree.FilePath.Contains(p.AssemblyName)),
})));
foreach (var metric in metrics)
{
try
{
writer.WriteField(metric.SyntaxTree.FilePath.Replace(",", "_"));
var codemetrics = await codeCalculator.Calculate(new SyntaxTree[] { metric.SyntaxTree }); //This throws an error
//var codemetrics = await codeCalculator.Calculate(metric.Project,solution); //this also throws an error
if i only use the halstead metrics then there is no problem and everythings works as expected.
Can you please submit a failing test? I suspect the issue is related to the code you are analyzing.
Hi, http://1drv.ms/1UGPpc4
i have submited a small MVC web solution, as well as the archimetrics program code. The project just needs to get built as well as nuget packages needs to get updated.
Would this help? are do you need something more specific?
Regards,
Could you not submit a test like this https://github.com/jjrdk/ArchiMetrics/blob/master/ArchiMetrics.Analysis.Tests/CodeMetricsCalculatorTests.cs
I'm seeing this too, with a malformed XML comment like this being the cause of one of the problems:
// <summary>
/// note only two forward slashes before the start of this comment block
/// </summary>
Another cause would be an ampersand not properly encoded to &
in the XML:
/// <summary> hello & world </summary>
I tried to write failing tests for both these scenarios following the template shown above, but they both pass. Maybe a fix is already in compared with the version on NuGet?
No activity and not able to reproduce.
I'm still seeing this issue. I've narrowed it down to MemberDocumentationFactory.Create
the call to XDocument.Parse
will throw an XmlException
if the XML documentation is malformed. I still can't work out how to reproduce this in a simple unit test, but on a call to CodeMetricsCalculator.Calculate(project, solution)
the exception will be thrown.
Here's a failing unit test that demonstrates the issue by analyzing itself:
/// <summary>
/// A bad & comment
/// </summary>
[Fact]
public async Task MalformedMetricsTest()
{
var solutionPath = @"C:\Code\github\ArchiMetrics\Archimetrics.sln";
var solutionProvider = new SolutionProvider();
var solution = await solutionProvider.Get(solutionPath);
var metricsCalculator = new CodeMetricsCalculator();
foreach (var p in solution.Projects)
{
var metrics = await metricsCalculator.Calculate(p, solution);
}
}
Thanks for the analysis. From what I gather the '&' causes an invalid documentation xml which causes the exception.
What is your suggested behaviour in this situation?
Yes, basically anything wrong with the XML, including having lines starting with just two forward slashes //
somewhere in the comment, My suggested behaviour is simply to catch the XmlException
and return null from MemberDocumentationFactory.Create
. I would have created a pull request but I ended up having to change a bunch of project.json files as well due to nuget problems (issue #22). I can create one if that would be helpful.
Anyway, thanks for this awesome library. I've been using it to automate some code metrics gathering, and it is super easy to use.
PRs are always good, also for having something concrete to discuss.
Not sure that returning null is a good fallback. There is actually some documentation content, would it not be better returning the raw string?
OK, will try to get something done. If returning the raw string is possible that would be even better.
MemberDocumentationFactory.Create
returns an IMemberDocumentation
, so I could return one with the malformed comment just placed into the Summary
property perhaps. Would that be acceptable?
That could work. Since it returns an interface, why not return a FaultMemberDocumentation instance where all the properties just return the raw documentation string?
yes can do that. I'll try to put together a PR next week.