Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing root element. #13

Open
Spookenator opened this issue Sep 10, 2015 · 13 comments
Open

Missing root element. #13

Spookenator opened this issue Sep 10, 2015 · 13 comments

Comments

@Spookenator
Copy link

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.

@jjrdk
Copy link
Owner

jjrdk commented Sep 11, 2015

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

@Spookenator
Copy link
Author

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,

@jjrdk
Copy link
Owner

jjrdk commented Sep 14, 2015

@markheath
Copy link
Contributor

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?

@jjrdk
Copy link
Owner

jjrdk commented Apr 27, 2016

No activity and not able to reproduce.

@jjrdk jjrdk closed this as completed Apr 27, 2016
@markheath
Copy link
Contributor

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

@jjrdk
Copy link
Owner

jjrdk commented Sep 9, 2016

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 jjrdk reopened this Sep 9, 2016
@markheath
Copy link
Contributor

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.

@jjrdk
Copy link
Owner

jjrdk commented Sep 10, 2016

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?

@markheath
Copy link
Contributor

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

@markheath
Copy link
Contributor

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?

@jjrdk
Copy link
Owner

jjrdk commented Sep 17, 2016

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

@markheath
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants