nil4 / dotnet-transform-xdt

Modern .NET tools and library for XDT (Xml Document Transformation)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Task remove new lines which break config files

Dunge opened this issue · comments

commented

Let's say I have a App.config with this section:

      <setting name="InstanceName" serializeAs="String">
        <value>[[InstanceName]]</value>
      </setting>

And a App.Debug.Config with this section:

      <setting name="InstanceName" serializeAs="String" xdt:Transform="Replace" xdt:Locator="Match(name)">
        <value>Debug</value>
      </setting>

After dotnet-transform-xdt does it work, the resulting file will become:

      <setting name="InstanceName" serializeAs="String"><value>Debug</value></setting>

At first glance this shouldn't be a problem because it's still a valid XML file, but somehow on execution the .NET System.Configuration class don't like this, and throw a "System.Configuration.ConfigurationErrorsException" exception:

System.Configuration.ConfigurationErrorsException: Unrecognized element 'setting'. (Project\bin\Debug\net5.0\ProjectName.exe.config line 28)

A quick Google search lead me to this and this which are VERY old posts, but it still seems to be the case. I tried with project targeting net472 AND net5.0 and it throw the same error.

commented

I've managed to work around my particular issue by using this syntax instead:

      <setting name="InstanceName" serializeAs="String" xdt:Locator="Match(name)">
        <value xdt:Transform="Replace">Debug</value>
      </setting>

As explained in the last section of this documentation.

Still I assume in other situations people would want to transform blocks of multiple lines anyway, so the issue remain valid.

I've also read previous closed issues about whitespace not being preserved before version 2.0.0. This seems a very similar problem, but with newlines and still does it in the latest 2.2.0 version.

commented

Hi @Dunge! I appreciate the detailed report and the research around this issue. I am very glad that you found a viable workaround.

The code in this repo is stable but no longer evolving. Contributions (PRs) would be considered only to improve the tool itself, e.g. those necessary to continue shipping it (e.g. running on the latest .NET Core runtimes/SDKs). Other changes are unlikely to be accepted, particularly to the core XDT logic, not even to fix bugs such as this.

A bit of history: this tool appeared in the pre-.NET Core 1.0 (DNX / project.json) era, when there were precious few options for transforms. It was a straight port of the XDT code Microsoft published on CodePlex at the time. This includes existing features, test cases and generally bug-for-bug compatibility with that codebase. It was a conscious decision since that point to not add new features, or change any of the XDT logic, to retain compatibility with the XDT supported by Microsoft tools.

In .NET Core 1.x, shortcomings in the XML libraries meant that whitespace was not correctly preserved in general, and the CodePlex test cases were broken back then. Things improved significantly in .NET Core 2.0, when the changes required to get the existing test cases to run were implemented; nevertheless, no XDT issues were fixed, nor were new features added.

The specific scenario you described is not covered by the original tests, as you see by browsing through dotnet-xdt.tests/Resources. Looking at the references you found, it is apparent that Microsoft was aware of this particular issue for quite a while, but decided to not address it.

Nowadays, the .NET Core SDK added built-in XDT transformation support, documented at https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig. I believe (but did not verify) that the official XDT code now lives at github.com/dotnet/xdt, based on the repo description:

Microsoft Xml Document Transformation (XDT) enables transforming XML files. This is the same technology used to transform web.config files in .NET CLI and Visual Studio.

Nevertheless, looking at the files under https://github.com/dotnet/xdt/tree/master/test/Microsoft.Web.XmlTransform.Test/Resources, it is plain to see that they are the same set of test cases from CodePlex, so I am pretty sure that the dotnet/xdt repo is also a port and has the same issue in your scenario.

Support for XDT is built-in into .NET Core SDK, today e.g. at WebSdk/Publish/Tasks/Tasks/Xdt/TransformXml.cs and WebSdk/Publish/Targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets

Therefore, as you can hopefully understand, a fix for this issue will not be implemented here. I think the best bet for it being addressed is to open an issue under dotnet/xdt.

commented

Thank you @nil4 !

I was on the process of moving my .Net Framework projects to .Net5, and haven't ported everything to the json format yet, so I used the System.Configuration.ConfigurationManager package for compatibility and was looking for a tool to replace Microsoft.Web.Publishing.Tasks.dll that is only available in Visual Studio, but not in the .NET SDK using dotnet.exe to build on my build machine. That previous tool did not wipe the newlines.

Thank you for pointing me toward dotnet/xdt, I wasn't aware of its existence and ended up here instead. I will try to use it, hopefully it can be added as a nuget and solution task build step like the one here.