Skip to content

Commit 583c1ae

Browse files
michaelnebelCopilot
andcommitted
C#: Detect SDK-style project imports
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent c3ba5cc commit 583c1ae

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,11 @@ private void Parse(string file)
271271
}
272272
break;
273273
case "import":
274-
isLegacyProjectStructureUsed = isLegacyProjectStructureUsed || reader.GetAttribute("Project")?.EndsWith("Microsoft.CSharp.targets", StringComparison.OrdinalIgnoreCase) == true;
274+
var project = reader.GetAttribute("Project");
275+
isSdkProjectStructureUsed = isSdkProjectStructureUsed || project is not null && reader.GetAttribute("Sdk") is not null;
276+
isLegacyProjectStructureUsed = isLegacyProjectStructureUsed || project?.EndsWith("Microsoft.CSharp.targets", StringComparison.OrdinalIgnoreCase) == true;
275277
break;
278+
// TODO: we could also check `<Sdk Name="Microsoft.NET.Sdk" />`
276279
}
277280
});
278281
}

‎csharp/extractor/Semmle.Extraction.Tests/FileContent.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ public void TestFileContent_SdkProjectStructure()
229229
Assert.False(isLegacy);
230230
}
231231

232+
/// <summary>
233+
/// Verifies that an SDK attribute on an import indicates SDK project structure.
234+
/// </summary>
235+
[Fact]
236+
public void TestFileContent_SdkProjectImport()
237+
{
238+
// Setup
239+
var fileContent = new TestFileContent("<Project><Import Project='Sdk.props' Sdk='Microsoft.NET.Sdk' /></Project>");
240+
241+
// Execute
242+
var isLegacy = fileContent.IsLegacyProjectStructureUsed;
243+
var isSdk = fileContent.IsSdkProjectStructureUsed;
244+
245+
// Verify
246+
Assert.True(isSdk);
247+
Assert.False(isLegacy);
248+
}
249+
232250
/// <summary>
233251
/// Verifies that `FileContent` correctly handles XML syntax with namespaces and various elements.
234252
/// </summary>

0 commit comments

Comments
 (0)