Skip to content

NormalizeWhitespace breaks XML attributes in comments #47363

@svick

Description

@svick

Version Used: 3.7.0

Steps to Reproduce:

Run the following code:

string code = @"
/// <example>
///   <code lang=""cs"" source=""foo.cs"" />
/// </example>
class Foo {}";

var tree = SyntaxFactory.ParseSyntaxTree(code);
tree = tree.WithRootAndOptions(tree.GetRoot().NormalizeWhitespace(), tree.Options.WithDocumentationMode(DocumentationMode.Diagnose));
var compilation = CSharpCompilation.Create(null)
	.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
	.AddSyntaxTrees(tree)
	.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location));
var diagnostics = compilation.GetDiagnostics();

Expected Behavior:

No warnings.

Actual Behavior:

warning CS1570: XML comment has badly formed XML -- ''source' is an unexpected token. Expecting whitespace. Line 2, position 32.'

This seems to happen because NormalizeWhitespace() changes the code to:

/// <example>
///   <code lang = "cs"source = "foo.cs"/>
/// </example>
class Foo
{
}

Notice how there is no space before the source attribute, which is invalid XML.

Also, when running this code under a Debug build of the compiler on .Net Core, the following assertion is triggered:

Debug.Assert(false, "If we hit this, then we might need to think about a different workaround " +
"for stripping the location out the message.");

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions