Skip to content

Commit fdb0703

Browse files
committed
fix: inter-project reference for SDK-style solutions
1 parent 7e13495 commit fdb0703

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

ICSharpCode.Decompiler/Solution/SolutionCreator.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace ICSharpCode.Decompiler.Solution
2929
/// </summary>
3030
public static class SolutionCreator
3131
{
32-
static readonly XNamespace ProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
32+
static readonly XNamespace NonSDKProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
3333

3434
/// <summary>
3535
/// Writes a solution file to the specified <paramref name="targetFile"/>.
@@ -63,7 +63,7 @@ public static void WriteSolutionFile(string targetFile, List<ProjectItem> projec
6363
WriteSolutionFile(writer, projects, targetFile);
6464
}
6565

66-
FixProjectReferences(projects);
66+
FixAllProjectReferences(projects);
6767
}
6868

6969
static void WriteSolutionFile(TextWriter writer, List<ProjectItem> projects, string solutionFilePath)
@@ -153,7 +153,7 @@ static void WriteProjectConfigurations(
153153
writer.WriteLine("\tEndGlobalSection");
154154
}
155155

156-
static void FixProjectReferences(List<ProjectItem> projects)
156+
static void FixAllProjectReferences(List<ProjectItem> projects)
157157
{
158158
var projectsMap = projects.ToDictionary(
159159
p => p.ProjectName,
@@ -170,9 +170,11 @@ static void FixProjectReferences(List<ProjectItem> projects)
170170
$"no <Project> at the root; could not fix project references.");
171171
}
172172

173+
// sdk style projects don't use a namespace for the elements,
174+
// but we still need to use the namespace for non-sdk style projects.
173175
var sdkStyle = projectDoc.Root.Attribute("Sdk") != null;
174-
var itemGroupTagName = sdkStyle ? "ItemGroup" : ProjectFileNamespace + "ItemGroup";
175-
var referenceTagName = sdkStyle ? "Reference" : ProjectFileNamespace + "Reference";
176+
var itemGroupTagName = sdkStyle ? "ItemGroup" : NonSDKProjectFileNamespace + "ItemGroup";
177+
var referenceTagName = sdkStyle ? "Reference" : NonSDKProjectFileNamespace + "Reference";
176178

177179
var referencesItemGroups = projectDoc.Root
178180
.Elements(itemGroupTagName)
@@ -191,12 +193,11 @@ static void FixProjectReferences(List<ProjectItem> projects)
191193
static void FixProjectReferences(string projectFilePath, XElement itemGroup,
192194
Dictionary<string, ProjectItem> projects, bool sdkStyle)
193195
{
194-
XName GetElementName(string localName) => sdkStyle ? localName : ProjectFileNamespace + localName;
196+
197+
XName GetElementName(string name) => sdkStyle ? name : NonSDKProjectFileNamespace + name;
195198

196199
var referenceTagName = GetElementName("Reference");
197200
var projectReferenceTagName = GetElementName("ProjectReference");
198-
var projectTagName = GetElementName("Project");
199-
var nameTagName = GetElementName("Name");
200201

201202
foreach (var item in itemGroup.Elements(referenceTagName).ToList())
202203
{
@@ -205,10 +206,20 @@ static void FixProjectReferences(string projectFilePath, XElement itemGroup,
205206
{
206207
item.Remove();
207208

208-
var projectReference = new XElement(projectReferenceTagName,
209-
new XElement(projectTagName, referencedProject.Guid.ToString("B").ToLowerInvariant()),
210-
new XElement(nameTagName, referencedProject.ProjectName));
211-
projectReference.SetAttributeValue("Include", GetRelativePath(projectFilePath, referencedProject.FilePath));
209+
var projectReference = new XElement(
210+
projectReferenceTagName,
211+
new XAttribute("Include", GetRelativePath(projectFilePath, referencedProject.FilePath)));
212+
213+
// SDK-style projects do not use the <Project> and <Name> elements for project references.
214+
// (Instead, those get read from the .csproj file in "Include".)
215+
if (!sdkStyle)
216+
{
217+
projectReference.Add(
218+
// no ToUpper() for uuids, most Microsoft tools seem to emit them in lowercase
219+
// (no .ToLower() as .ToString("B") already outputs lowercase)
220+
new XElement(NonSDKProjectFileNamespace + "Project", referencedProject.Guid.ToString("B")),
221+
new XElement(NonSDKProjectFileNamespace + "Name", referencedProject.ProjectName));
222+
}
212223

213224
itemGroup.Add(projectReference);
214225
}

0 commit comments

Comments
 (0)