Skip to content

Commit dfcb16e

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 dfcb16e

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,12 @@ private void Parse(string file)
246246
break;
247247
case "project":
248248
var sdk = reader.GetAttribute("Sdk");
249-
isSdkProjectStructureUsed |= sdk != null;
249+
isSdkProjectStructureUsed |= sdk is not null;
250250
useAspNetCoreDlls |= string.Equals(sdk, "Microsoft.NET.Sdk.Web", StringComparison.OrdinalIgnoreCase);
251251
break;
252252
case "frameworkreference":
253253
var framework = reader.GetAttribute("Include");
254-
isSdkProjectStructureUsed |= framework != null;
254+
isSdkProjectStructureUsed |= framework is not null;
255255
useAspNetCoreDlls |= string.Equals(framework, "Microsoft.AspNetCore.App", StringComparison.OrdinalIgnoreCase);
256256
break;
257257
case "implicitusings":
@@ -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 |= 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)