dotnet/msbuild

NewLine normalization on .NET Core

AndyGerlicher opened this issue · 4 comments

Related to #1340

In Full Framework, we do not normalize any line endings in a user's project file. To do this we had to use XmlTextReader. This behavior is legacy in all versions of MSBuild and should be preserved.

In .NET Core, XmlTextReader is not available and there does not seem to be a way to not normalize EOL characters. This means doing any project file manipulation via the .NET Core version of MSBuild will result in all \r\n characters normalized to \n on Save.

In the absence of built-in functionality to do this (it was taken out in .NET Core) the only way I can think of is trying to determine the 'right' EOL character on Load and replacing on Save. This could be:

  1. If any \n characters appear by itself, replace all NewLine characters with \n (do nothing, this is all .NET Core supports).
  2. If all EOL characters are \r\n on the way in, string.Replace() on Save.

Thoughts? We could try to be fancier and count the number of \r\n and if they appear more often than \n use that? And of course if we do this for .NET Core, should we do the same in Full Framework MSBuild? I think not to preserve behavior, and I don't think there's a good solution here unfortunately.

Andy already knows this, but just for the sake of anyone reading this -- XmlTextReader is returning in the next version of .NET Core. It's already in the CoreFX repo. This issue is because MSBuild isn't ready to take a dependency on that version yet.

@AndyGerlicher can this issue be revisited? We've had multiple related issues reported on CLI (dotnet/cli#8147 and dotnet/cli-migrate#37).

@peterhuene Andy's out sick today, but I think it's reasonable to try to move back to XmlTextReader; we already have hard .NET Core 2.1 dependencies.