Skip to content

NormalizeWhitespace improperly inserts a space before format specifiers in interpolated strings #24454

@tumtumtum

Description

@tumtumtum

Version Used:

2.6.1

Steps to Reproduce:

  1. Compile the following code
using System;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
		{
			var code = 
			@"
				using System;

				public class Foo
				{
					public static void Main()
					{
						Console.WriteLine($""{Guid.NewGuid():N}"");
					}
				}
			";

			var node = SyntaxFactory.ParseSyntaxTree(code, new CSharpParseOptions(LanguageVersion.CSharp7_2))
				.GetCompilationUnitRoot()
				.NormalizeWhitespace("\t");

			Console.WriteLine(node);
		}
    }
}

Expected Behavior:

Should output:

using System;

public class Foo
{
	public static void Main()
	{
		Console.WriteLine($"{Guid.NewGuid():N}");
	}
}

Actual Behavior:

The output includes a space after the : and before the N. This causes the resulting program to fail to run because ": N" is not a valid format specifier.

using System;

public class Foo
{
	public static void Main()
	{
		Console.WriteLine($"{Guid.NewGuid(): N}");
	}
}

Metadata

Metadata

Assignees

Labels

Area-CompilersBugResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions