From c6c68312009e8b904705758e7ef8524ec7144c6d Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 4 Sep 2026 13:19:53 +0200 Subject: [PATCH] Write item metadata the way a non-SDK project keeps it Metadata as attributes on an item element is MSBuild 15 syntax. The non-SDK project format is what an export falls back to for toolchains that predate the SDK, and those reject an unknown attribute on an item element, so a Page item carrying Generator and SubType as attributes undoes the reason to write that format at all. Every non-SDK project written by anything else keeps metadata in child elements. The SDK-style writer keeps attributes: there the syntax is a given and it is what the format's own tooling produces. Assisted-by: Claude:claude-opus-5:Claude Code --- .../ProjectFileWriterDefaultTests.cs | 86 +++++++++++++++++++ .../ProjectFileWriterDefault.cs | 5 +- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/ProjectDecompiler/ProjectFileWriterDefaultTests.cs diff --git a/ICSharpCode.Decompiler.Tests/ProjectDecompiler/ProjectFileWriterDefaultTests.cs b/ICSharpCode.Decompiler.Tests/ProjectDecompiler/ProjectFileWriterDefaultTests.cs new file mode 100644 index 000000000..8a21d4257 --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/ProjectDecompiler/ProjectFileWriterDefaultTests.cs @@ -0,0 +1,86 @@ +// Copyright (c) 2026 Siegfried Pammer +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; +using System.IO; + +using ICSharpCode.Decompiler.CSharp; +using ICSharpCode.Decompiler.CSharp.ProjectDecompiler; +using ICSharpCode.Decompiler.Metadata; + +using NUnit.Framework; + +namespace ICSharpCode.Decompiler.Tests.ProjectDecompiler; + +[TestFixture] +public sealed class ProjectFileWriterDefaultTests +{ + /// + /// Item metadata as attributes is MSBuild 15 syntax. The non-SDK format exists for the + /// toolchains that came before it, and those reject an unknown attribute on an item element, + /// so metadata has to be written the way every non-SDK project writes it: as child elements. + /// + [Test] + public void ItemMetadataIsWrittenAsChildElements() + { + ProjectItemInfo[] files = [ + new ProjectItemInfo("Page", "Themes/Generic.xaml") + .With("Generator", "MSBuild:Compile") + .With("SubType", "Designer"), + ]; + + string project = WriteProjectFile(files); + + using (Assert.EnterMultipleScope()) + { + Assert.That(project, Does.Contain(@""), project); + Assert.That(project, Does.Contain(@"MSBuild:Compile"), project); + Assert.That(project, Does.Contain(@"Designer"), project); + Assert.That(project, Does.Not.Contain(@"Generator="""), "metadata does not belong in an attribute"); + } + } + + [Test] + public void AnItemWithoutMetadataStaysOnOneLine() + { + ProjectItemInfo[] files = [new ProjectItemInfo("Compile", "Program.cs")]; + + string project = WriteProjectFile(files); + + Assert.That(project, Does.Contain(@""), project); + } + + static string WriteProjectFile(ProjectItemInfo[] files) + { + StringWriter output = new(); + ProjectFileWriterDefault.Instance.Write(output, new TestProjectInfoProvider(), files, + new PEFile("ICSharpCode.Decompiler.dll")); + return output.ToString(); + } + + sealed class TestProjectInfoProvider : IProjectInfoProvider + { + public IAssemblyResolver AssemblyResolver { get; } = new UniversalAssemblyResolver(null, false, null); + public IAssemblyReferenceClassifier AssemblyReferenceClassifier { get; } = new AssemblyReferenceClassifier(); + public LanguageVersion LanguageVersion => LanguageVersion.Latest; + public bool CheckForOverflowUnderflow => false; + public Guid ProjectGuid { get; } = Guid.NewGuid(); + public string TargetDirectory { get; } = Environment.CurrentDirectory; + public string StrongNameKeyFile => null; + } +} diff --git a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs index 78f815feb..f767fc76a 100644 --- a/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs +++ b/ICSharpCode.Decompiler/CSharp/ProjectDecompiler/ProjectFileWriterDefault.cs @@ -173,8 +173,11 @@ namespace ICSharpCode.Decompiler.CSharp.ProjectDecompiler w.WriteAttributeString("Include", item.FileName); if (item.AdditionalProperties != null) { + // Item metadata as attributes is MSBuild 15 syntax. This format is what an + // export targets when the toolchain predates that, so the metadata goes where + // every non-SDK project keeps it: in child elements. foreach (var (key, value) in item.AdditionalProperties) - w.WriteAttributeString(key, value); + w.WriteElementString(key, value); } w.WriteEndElement(); }