microsoft / slow-cheetah

Tooling for XML and JSON file transforms on build from Visual Studio and MSBuild
Other
317 stars 66 forks source link

Transformation Damages Values with ">" in Them #263

Open vppetrov opened 1 year ago

vppetrov commented 1 year ago

I am using transformations for nlog.config and I have some conditions that check the length of the thread name using the ">" symbol. slow-cheetah changes these even with no transformation at all. Here is an example:

nlog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="coloredConsole" xsi:type="ColoredConsole"
                layout="${when:when=length('${threadname}') > 0:inner=/${threadname}}:  ${message}"
        />
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="coloredConsole" />
    </rules>
</nlog>

nlog.Debug.config (no transformations at all):

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xmlns="http://www.nlog-project.org/schemas/NLog.xsd">
</nlog>

Result:

<?xml version="1.0" encoding="utf-8"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
        <target name="coloredConsole" xsi:type="ColoredConsole"
                layout="${when:when=length('${threadname}') &gt; 0:inner=/${threadname}}:   ${message}"
        />
    </targets>
    <rules>
        <logger name="*" minlevel="Trace" writeTo="coloredConsole" />
    </rules>
</nlog>

Notice the \> in the layout property.

The simplest solution would be to not touch any values that have no transformation applied.

CZEMacLeod commented 1 year ago

&gt; is the escaped version of > and should be used anywhere you need a > symbol in xml. You can use the unescaped version in some circumstances - such as in an attribute value, but generally it should be avoided. It is not 'damaged' as an xml parser will correctly unescape the attribute value when it is read.

I'm reasonably certain that the way transforms are done, the whole document is loaded by a parser, then transformed then written back out, so while the specific node won't be touched if no transform is applied, the whole file is written back out with full escaping applied.

vppetrov commented 1 year ago

@CZEMacLeod Thanks for the comment, makes sense. Seems to be working as well. All nlog samples seem to use > without escapting it which lead to this confusion but it's a problem of these samples.