dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.91k stars 4.01k forks source link

Roslyn NormalizeWhitespace bug #36027

Open sk78v opened 5 years ago

sk78v commented 5 years ago

Version Used: Please see the packages.config. Microsoft.CodeAnalysis packages 2.6.0 Steps to Reproduce: program.cs file

 static void Main(string[] args)
        {
            var workspace = new Microsoft.CodeAnalysis.AdhocWorkspace();
            var projectId = Microsoft.CodeAnalysis.ProjectId.CreateNewId();
            var versionStamp = Microsoft.CodeAnalysis.VersionStamp.Create();
            var projectInfo = ProjectInfo.Create(projectId, versionStamp, "NewProject", "projName", Microsoft.CodeAnalysis.LanguageNames.CSharp);
            var newProject = workspace.AddProject(projectInfo);

            Console.WriteLine("i");

            var sourceText = SourceText.From(
              @"
                  using System;
                  using System.IO;
                  using System.Linq;
                  using System.Text;

                  namespace HelloWorld
                  {
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              int i = 0;                           

                              /**
                               Roslyn normalize whitespace issue
                              **/
                              if( i> 5)
                              {
                                    Console.WriteLine(i.ToString());
                              }
                              else if(i > 10)
                              {
                                    Console.WriteLine(i.ToString());
                              }            
                          }

                      }
                  }");

            var document = workspace.AddDocument(newProject.Id, "NewFile.cs", sourceText);
            var syntaxRoot = document.GetSyntaxRootAsync().Result;

            var changedSyntaxRoot = syntaxRoot.NormalizeWhitespace();

            Console.Read();
        }

Expected Behavior: After calling NormalizeWhitespace() on the SyntaxNode, expecting the valid code to be written like this but the "If" statement is getting commented.

                              /**
                               Roslyn normalize whitespace issue
                              **/
                              if( i> 5)
                              {

[packages.txt](https://github.com/dotnet/roslyn/files/3234042/packages.txt)

                                    Console.WriteLine(i.ToString());
                              }
                              else if(i > 10)
                              {
                                    Console.WriteLine(i.ToString());
                              }      

Actual Behavior: "If" statement is getting commented.

           ///
            ///Roslyn normalize whitespace issue
            //////if (i > 5)
            {
                Console.WriteLine(i.ToString());
            }
            else if (i > 10)
            {
                Console.WriteLine(i.ToString());
            }
sharwell commented 5 years ago

@jcouv Moved back to Area-Compilers since this code is all in the core compiler

gafter commented 5 years ago

@sharwell Although the code is in the compiler project for convenience, this is not a priority for the compiler team, which primarily maintains the code for consuming (not producing) source code. Moving to our backlog. If this API is important to you we should talk about transferring ownership.

bernd5 commented 4 years ago

The issue is still there. It seems to happen only with MultiLineDocumentTrivia.

I'm not quite sure, but it could be just a missing case in Microsoft.CodeAnalysis.CSharp.Syntax.SyntaxNormalizer.NeedsLineBreakAfter.

CyrusNajmabadi commented 1 year ago

@bernd5 neal is saying this is not a priority. SO we'd likely take a community fix, but we're not going to expend effort on any work here ourselves.

bernd5 commented 1 year ago

@CyrusNajmabadi for me this is not a big issue anymore - because I remove trivia completely now. But I could imagine that for other people it might be. Currently I'm quite busy, not sure if I have time for a contribution here.