diff --git a/src/chocolatey/infrastructure/services/XmlService.cs b/src/chocolatey/infrastructure/services/XmlService.cs index 19355c9c5d..32c2d7fc06 100644 --- a/src/chocolatey/infrastructure/services/XmlService.cs +++ b/src/chocolatey/infrastructure/services/XmlService.cs @@ -102,6 +102,8 @@ public void serialize(XmlType xmlType, string xmlFilePath, bool isSilen () => { var xmlSerializer = new XmlSerializer(typeof(XmlType)); + + // Write the updated file to memory using(var memoryStream = new MemoryStream()) using(var textWriter = new StreamWriter(memoryStream, encoding: new UTF8Encoding(encoderShouldEmitUTF8Identifier: true))) { @@ -109,8 +111,23 @@ public void serialize(XmlType xmlType, string xmlFilePath, bool isSilen textWriter.Flush(); memoryStream.Position = 0; - if (!_hashProvider.hash_file(xmlFilePath).is_equal_to(_hashProvider.hash_stream(memoryStream))) + + // Grab the hash of both files and compare them. + var originalFileHash = _hashProvider.hash_file(xmlFilePath); + if (!originalFileHash.is_equal_to(_hashProvider.hash_stream(memoryStream))) { + // If there wasn't a file there in the first place, just write the new one out directly. + if(string.IsNullOrEmpty(originalFileHash)) + { + using(var updateFileStream = _fileSystem.create_file(xmlFilePath)) + { + memoryStream.Position = 0; + memoryStream.CopyTo(updateFileStream); + return; + } + } + + // Otherwise, create an update file, and resiliently move it into place. var tempUpdateFile = xmlFilePath + ".update"; using(var updateFileStream = _fileSystem.create_file(tempUpdateFile)) {