From 67366fe77ab17dd68d43c031a74b39f5b8a0cd27 Mon Sep 17 00:00:00 2001 From: Peter Crabtree Date: Tue, 18 Mar 2025 22:23:58 -0400 Subject: [PATCH] fix: Handle SDK-style project references in WriteSolutionFile() --- .../Solution/SolutionCreator.cs | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/ICSharpCode.Decompiler/Solution/SolutionCreator.cs b/ICSharpCode.Decompiler/Solution/SolutionCreator.cs index 7cb07c9d5..a4d9fb04c 100644 --- a/ICSharpCode.Decompiler/Solution/SolutionCreator.cs +++ b/ICSharpCode.Decompiler/Solution/SolutionCreator.cs @@ -33,6 +33,7 @@ namespace ICSharpCode.Decompiler.Solution /// /// Writes a solution file to the specified . + /// Also fixes intra-solution project references in the project files. /// /// The full path of the file to write. /// The projects contained in this solution. @@ -164,22 +165,37 @@ namespace ICSharpCode.Decompiler.Solution { XDocument projectDoc = XDocument.Load(project.FilePath); + if (projectDoc.Root?.Name.LocalName != "Project") + { + throw new InvalidOperationException( + $"The file {project.FilePath} is not a valid project file, " + + $"no at the root; could not fix project references."); + } + + var sdkStyle = projectDoc.Root.Attribute("Sdk") != null; + var itemGroupTagName = sdkStyle ? "ItemGroup" : ProjectFileNamespace + "ItemGroup"; + var referenceTagName = sdkStyle ? "Reference" : ProjectFileNamespace + "Reference"; + var referencesItemGroups = projectDoc.Root - .Elements(ProjectFileNamespace + "ItemGroup") - .Where(e => e.Elements(ProjectFileNamespace + "Reference").Any()); + .Elements(itemGroupTagName) + .Where(e => e.Elements(referenceTagName).Any()) + .ToList(); foreach (var itemGroup in referencesItemGroups) { - FixProjectReferences(project.FilePath, itemGroup, projectsMap); + FixProjectReferences(project.FilePath, itemGroup, projectsMap, sdkStyle); } projectDoc.Save(project.FilePath); } } - private static void FixProjectReferences(string projectFilePath, XElement itemGroup, IDictionary projects) + static void FixProjectReferences(string projectFilePath, XElement itemGroup, + Dictionary projects, bool sdkStyle) { - foreach (var item in itemGroup.Elements(ProjectFileNamespace + "Reference").ToList()) + var referenceTagName = sdkStyle ? "Reference" : ProjectFileNamespace + "Reference"; + + foreach (var item in itemGroup.Elements(referenceTagName).ToList()) { var assemblyName = item.Attribute("Include")?.Value; if (assemblyName != null && projects.TryGetValue(assemblyName, out var referencedProject)) @@ -187,7 +203,7 @@ namespace ICSharpCode.Decompiler.Solution item.Remove(); var projectReference = new XElement(ProjectFileNamespace + "ProjectReference", - new XElement(ProjectFileNamespace + "Project", referencedProject.Guid.ToString("B").ToUpperInvariant()), + new XElement(ProjectFileNamespace + "Project", referencedProject.Guid.ToString("B").ToLowerInvariant()), new XElement(ProjectFileNamespace + "Name", referencedProject.ProjectName)); projectReference.SetAttributeValue("Include", GetRelativePath(projectFilePath, referencedProject.FilePath));