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

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

Closed
tumtumtum opened this issue Jan 25, 2018 · 5 comments

Comments

@tumtumtum
Copy link

commented Jan 25, 2018

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}");
	}
}
@sharwell

This comment has been minimized.

Copy link
Member

commented Jan 25, 2018

Also observed here: DotNetAnalyzers/StyleCopAnalyzers#2289 (comment)

The space after : is not incorrect, but it changes the meaning of the format string. In other words, the formatter must not be allowed to change the spacing; it would be equally incorrect to remove a space if it was already present.

@tumtumtum

This comment has been minimized.

Copy link
Author

commented Jan 25, 2018

Can you give me a pointer as to what it's changing in the syntax tree? I can't see anything different within the FormatClauseSyntax that would causing the space to be inserted.

@tumtumtum

This comment has been minimized.

Copy link
Author

commented Jan 25, 2018

Oh typical, just after I write the message I find it. There's trailing whitespace trivia on the ColonToken

tumtumtum added a commit to tumtumtum/Shaolinq that referenced this issue Jan 25, 2018
Convert AsyncRewriterTask to use out-of-process generation to avoid a…
…ssembly conflicts with msbuild.

Put in fix for roslyn bug related to format specifier usage in interpolated strings (dotnet/roslyn#24454)

@jcouv jcouv added the 4 - In Review label Jan 27, 2018

@jcouv

This comment has been minimized.

Copy link
Member

commented Jan 27, 2018

Fix pending review

@jcouv jcouv added this to Active/Investigating in Compiler: Julien's umbrellas Jan 27, 2018

@jinujoseph jinujoseph removed the help wanted label Jan 28, 2018

@jcouv jcouv added this to the 15.7 milestone Jan 28, 2018

@jcouv jcouv removed this from Active/Investigating in Compiler: Julien's umbrellas Jan 28, 2018

@jcouv

This comment has been minimized.

Copy link
Member

commented Jan 29, 2018

Fix merged for 15.7

@jcouv jcouv closed this Jan 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.