ArchiMetrics icon indicating copy to clipboard operation
ArchiMetrics copied to clipboard

Missing root element.

Open Spookenator opened this issue 9 years ago • 13 comments

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.

Spookenator avatar Sep 10 '15 21:09 Spookenator

Can you please submit a failing test? I suspect the issue is related to the code you are analyzing.

jjrdk avatar Sep 11 '15 09:09 jjrdk

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,

Spookenator avatar Sep 11 '15 12:09 Spookenator

Could you not submit a test like this https://github.com/jjrdk/ArchiMetrics/blob/master/ArchiMetrics.Analysis.Tests/CodeMetricsCalculatorTests.cs

jjrdk avatar Sep 14 '15 02:09 jjrdk

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 &amp; 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?

markheath avatar Oct 05 '15 16:10 markheath

No activity and not able to reproduce.

jjrdk avatar Apr 27 '16 10:04 jjrdk

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);
            }
        }

markheath avatar Sep 06 '16 11:09 markheath

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?

jjrdk avatar Sep 09 '16 18:09 jjrdk

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.

markheath avatar Sep 09 '16 20:09 markheath

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?

jjrdk avatar Sep 10 '16 09:09 jjrdk

OK, will try to get something done. If returning the raw string is possible that would be even better.

markheath avatar Sep 10 '16 18:09 markheath

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?

markheath avatar Sep 16 '16 11:09 markheath

That could work. Since it returns an interface, why not return a FaultMemberDocumentation instance where all the properties just return the raw documentation string?

jjrdk avatar Sep 17 '16 11:09 jjrdk

yes can do that. I'll try to put together a PR next week.

markheath avatar Sep 17 '16 11:09 markheath