Skip to content

chore: Suppress XmlException thrown internally on AppConfigGenerator::Generate method #2817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions src/BenchmarkDotNet/Toolchains/AppConfigGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using BenchmarkDotNet.Extensions;
using BenchmarkDotNet.Jobs;

#nullable enable

namespace BenchmarkDotNet.Toolchains
{
internal static class AppConfigGenerator
Expand All @@ -26,42 +28,56 @@ internal static class AppConfigGenerator

internal static void Generate(Job job, TextReader source, TextWriter destination, IResolver resolver)
{
using (var xmlReader = XmlReader.Create(source))
{
var xmlDocument = new XmlDocument();
var xmlDocument = new XmlDocument();

XmlNode configurationElement;

var configurationElement = GetOrCreateConfigurationElement(xmlDocument, xmlReader);
if (source == TextReader.Null)
{
// Create a new configuration node.
configurationElement = xmlDocument.CreateNode(XmlNodeType.Element, "configuration", string.Empty);
xmlDocument.AppendChild(configurationElement);
}
else
{
// Try to get configuration node from specified TextReader.
using var xmlReader = XmlReader.Create(source);
configurationElement = GetOrCreateConfigurationElement(xmlDocument, xmlReader);
}

var runtimeElement = GetOrCreateRuntimeElement(xmlDocument, configurationElement);
var runtimeElement = GetOrCreateRuntimeElement(xmlDocument, configurationElement);

ClearStartupSettingsForCustomClr(configurationElement, job.Environment.Runtime);
ClearAllRuntimeSettingsThatCanBeSetOnlyByJobConfiguration(runtimeElement);
ClearStartupSettingsForCustomClr(configurationElement, job.Environment.Runtime);
ClearAllRuntimeSettingsThatCanBeSetOnlyByJobConfiguration(runtimeElement);

GenerateJitSettings(xmlDocument, runtimeElement, job.Environment);
GenerateGCSettings(xmlDocument, runtimeElement, job.Environment.Gc, resolver);
GenerateJitSettings(xmlDocument, runtimeElement, job.Environment);
GenerateGCSettings(xmlDocument, runtimeElement, job.Environment.Gc, resolver);

xmlDocument.Save(destination);
}
xmlDocument.Save(destination);
}

private static XmlNode GetOrCreateConfigurationElement(XmlDocument xmlDocument, XmlReader xmlReader)
{
try
{
xmlDocument.Load(xmlReader);

return xmlDocument.SelectSingleNode("/configuration");
var configurationNode = xmlDocument.SelectSingleNode("/configuration");
if (configurationNode != null)
return configurationNode;
}
catch // empty document
catch (XmlException)
{
return xmlDocument.AppendChild(xmlDocument.CreateNode(XmlNodeType.Element, "configuration", string.Empty));
// Failed to load XML content.
}

// If the XML is invalid or configuration node is not exists. Create a new configuration element
return xmlDocument.AppendChild(xmlDocument.CreateNode(XmlNodeType.Element, "configuration", string.Empty))!;
}

private static XmlNode GetOrCreateRuntimeElement(XmlDocument xmlDocument, XmlNode configurationElement)
{
return configurationElement.SelectSingleNode("runtime")
?? configurationElement.AppendChild(xmlDocument.CreateNode(XmlNodeType.Element, "runtime", string.Empty));
?? configurationElement.AppendChild(xmlDocument.CreateNode(XmlNodeType.Element, "runtime", string.Empty))!;
}

private static void ClearAllRuntimeSettingsThatCanBeSetOnlyByJobConfiguration(XmlNode runtimeElement)
Expand All @@ -75,7 +91,7 @@ private static void ClearAllRuntimeSettingsThatCanBeSetOnlyByJobConfiguration(Xm
}
}

private static void ClearStartupSettingsForCustomClr(XmlNode configurationElement, Runtime runtime)
private static void ClearStartupSettingsForCustomClr(XmlNode configurationElement, Runtime? runtime)
{
if (!(runtime is ClrRuntime clrRuntime) || string.IsNullOrEmpty(clrRuntime.Version))
return;
Expand Down
Loading