Skip to content

Commit d66a440

Browse files
authored
Fix files kept in use in XslTransformation (#6946)
1 parent b26f1a2 commit d66a440

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

src/Tasks.UnitTests/XslTransformation_Tests.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Text.RegularExpressions;
1414
using System.Xml.Xsl;
1515
using System.Xml;
16+
using Shouldly;
1617
using Xunit;
1718

1819
namespace Microsoft.Build.UnitTests
@@ -386,7 +387,7 @@ public void OutputTest()
386387
/// Setting correct "Parameter" parameters for Xsl.
387388
/// </summary>
388389
[Fact]
389-
public void XsltParamatersCorrect()
390+
public void XsltParametersCorrect()
390391
{
391392
string dir;
392393
TaskItem[] xmlPaths;
@@ -780,6 +781,39 @@ public void OutputFileCannotBeWritten()
780781
CleanUp(dir);
781782
}
782783

784+
/// <summary>
785+
/// The files are not kept locked by the task
786+
/// </summary>
787+
[Fact]
788+
public void InputFilesDontLock()
789+
{
790+
string dir;
791+
TaskItem[] xmlPaths;
792+
TaskItem xslPath;
793+
TaskItem[] outputPaths;
794+
MockEngine engine;
795+
Prepare(out dir, out xmlPaths, out xslPath, out _, out outputPaths, out _, out _, out engine);
796+
797+
// Test with files
798+
{
799+
XslTransformation t = new XslTransformation();
800+
t.BuildEngine = engine;
801+
t.XmlInputPaths = xmlPaths;
802+
t.XslInputPath = xslPath;
803+
t.OutputPaths = outputPaths;
804+
805+
t.Execute().ShouldBeTrue();
806+
string xmlInputPath = xmlPaths[0].ItemSpec;
807+
File.Delete(xmlInputPath); // this should succeed (file not locked by task)
808+
File.Exists(xmlInputPath).ShouldBeFalse();
809+
string xslInputPath = xslPath.ItemSpec;
810+
File.Delete(xslInputPath); // this should succeed (file not locked by task)
811+
File.Exists(xslInputPath).ShouldBeFalse();
812+
}
813+
814+
CleanUp(dir);
815+
}
816+
783817
/// <summary>
784818
/// XslDocument that throws runtime exception.
785819
/// </summary>

src/Tasks/XslTransformation.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ public XmlReader CreateReader(int itemPos)
334334
{
335335
if (XmlMode == XmlModes.XmlFile)
336336
{
337-
return XmlReader.Create(new StreamReader(_data[itemPos]), null, _data[itemPos]);
337+
return XmlReader.Create(new StreamReader(_data[itemPos]), new XmlReaderSettings { CloseInput = true }, _data[itemPos]);
338338
}
339-
else // xmlModes.Xml
339+
else // xmlModes.Xml
340340
{
341341
return XmlReader.Create(new StringReader(_data[itemPos]));
342342
}
@@ -459,7 +459,10 @@ public XslCompiledTransform LoadXslt(bool useTrustedSettings)
459459
_log.LogMessageFromResources(MessageImportance.Low, "XslTransform.UseTrustedSettings", _data);
460460
}
461461

462-
xslct.Load(new XPathDocument(XmlReader.Create(new StreamReader(_data), null, _data)), settings, new XmlUrlResolver());
462+
using (XmlReader reader = XmlReader.Create(new StreamReader(_data), new XmlReaderSettings { CloseInput = true }, _data))
463+
{
464+
xslct.Load(new XPathDocument(reader), settings, new XmlUrlResolver());
465+
}
463466
break;
464467
case XslModes.XsltCompiledDll:
465468
#if FEATURE_COMPILED_XSL

0 commit comments

Comments
 (0)