Skip to content

Commit

Permalink
Fix files kept in use in XslTransformation
Browse files Browse the repository at this point in the history
Following #6863, the created XmlReader is no longer responsible for its
underlying stream. This can cause the build process to hold on to the
processed file, preventing its removal. This can especially be a problem
when the transformation is in fact aimed at the input file itself, where
we want to create the transformed file, then move it to the original.
  • Loading branch information
lanfeust69 authored Oct 13, 2021
1 parent bbcce1d commit 7e23cbc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/Tasks.UnitTests/XslTransformation_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public void XmlXslParameters()

Assert.True(t.Execute()); // "The test should have passed at the both params correct test"
}

// we should now be able to delete the xml input file (not kept open)
if (xmlInputs[xmi].Key == XslTransformation.XmlInput.XmlModes.XmlFile)
{
string xmlInputPath = ((TaskItem[])xmlInputs[xmi].Value)[0].ItemSpec;
File.Delete(xmlInputPath);
}
}

CleanUp(dir);
Expand Down Expand Up @@ -386,7 +393,7 @@ public void OutputTest()
/// Setting correct "Parameter" parameters for Xsl.
/// </summary>
[Fact]
public void XsltParamatersCorrect()
public void XsltParametersCorrect()
{
string dir;
TaskItem[] xmlPaths;
Expand Down
6 changes: 3 additions & 3 deletions src/Tasks/XslTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ public XmlReader CreateReader(int itemPos)
{
if (XmlMode == XmlModes.XmlFile)
{
return XmlReader.Create(new StreamReader(_data[itemPos]), null, _data[itemPos]);
return XmlReader.Create(new StreamReader(_data[itemPos]), new XmlReaderSettings { CloseInput = true }, _data[itemPos]);
}
else // xmlModes.Xml
else // xmlModes.Xml
{
return XmlReader.Create(new StringReader(_data[itemPos]));
}
Expand Down Expand Up @@ -459,7 +459,7 @@ public XslCompiledTransform LoadXslt(bool useTrustedSettings)
_log.LogMessageFromResources(MessageImportance.Low, "XslTransform.UseTrustedSettings", _data);
}

xslct.Load(new XPathDocument(XmlReader.Create(new StreamReader(_data), null, _data)), settings, new XmlUrlResolver());
xslct.Load(new XPathDocument(XmlReader.Create(new StreamReader(_data), new XmlReaderSettings { CloseInput = true }, _data)), settings, new XmlUrlResolver());
break;
case XslModes.XsltCompiledDll:
#if FEATURE_COMPILED_XSL
Expand Down

0 comments on commit 7e23cbc

Please sign in to comment.