Skip to content

C#: Attempt to parse as XML instead of reading everything as text in the FileContent processing. - #22915

Draft
michaelnebel wants to merge 7 commits into
github:mainfrom
michaelnebel:csharp/filecontentparseasxml
Draft

michaelnebel wants to merge 7 commits into
github:mainfrom
michaelnebel:csharp/filecontentparseasxml

Conversation

@michaelnebel

@michaelnebel michaelnebel commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Instead of searching through all files (including source code files) we attempt to read the files as XML instead for detecting framework and package dependencies (and other project related settings). This will

  • Implicitly enable "fail-fast" (as an exception will be thrown as non-XML file content is found).
  • Make it easier to extend the logic for detecting certain properties.

Note, that this change impacts the semantics of the heuristic and we expect that this generally improves the precision of the heuristic detection of project properties.

  • Only XML file content is taken into consideration.
  • XML split across multiple lines will now be parsed and detected correctly.
  • Comments across multiple lines will be handled correctly.
  • <Sdk Name="..."> is now taken into consideration.
  • We don't do any handling of encoding (pointed out by CoPilot) and I don't think that is worth incorporating unless we see a concrete use-case.

Furthermore, this is expected to improve performance: We do less parsing of text as the XmlReader gives up and throws an exception, as soon as non-XML is encountered (before we read all text from all files and did regular expression matching).

DCA looks good

  • Drastic reduction in the number of calls with missing call target for dotnet/efcore.
  • Performance appears unaffected.

@github-actions github-actions Bot added the C# label Oct 28, 2026
@michaelnebel
michaelnebel force-pushed the csharp/filecontentparseasxml branch from 9b8ebe2 to 291ac2e Compare October 28, 2026 13:56
@michaelnebel michaelnebel changed the title C#: Use an XML parser instead of reading everything as text in the FileContent processing. C#: Attempt to parse as XML instead of reading everything as text in the FileContent processing. Oct 28, 2026
@michaelnebel
michaelnebel force-pushed the csharp/filecontentparseasxml branch 2 times, most recently from 781f085 to 4270ed1 Compare October 30, 2026 12:27
@michaelnebel
michaelnebel requested an auto review from Copilot October 30, 2026 12:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Case-sensitive MSBuild element matching ignores valid settings, and failed XML parsing leaves partial dependencies and settings committed, allowing invalid files to affect restoration.

2 open issues
What changed in this PR

Replaces text-based dependency detection in the C# extractor with streaming XML parsing, improving handling of project settings, multiline elements, and XML comments.

Changes:

  • Replaces regular expressions with XmlReader for detecting packages and project settings.
  • Changes file reading to streams and logs XML parsing failures.
  • Expands tests for XML syntax, namespaces, comments, packages.config, and invalid input.
File Description
csharp/​extractor/​Semmle.Extraction.Tests/​FileContent.cs Updates test fixtures and adds XML parsing coverage.
csharp/​extractor/​Semmle.Extraction.CSharp.DependencyFetching/​FileContent.cs Implements streaming XML-based dependency and settings detection.

Review effort: Balanced (auto)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Unresolved regressions misclassify SDK import-form projects, skipping Razor/.resx generation, and skip valid Windows-1252-declared projects, losing package and framework references.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Register code-page provider for Windows-1252 XML

csharp/​extractor/​Semmle.Extraction.CSharp.DependencyFetching/​FileContent.cs:155

An otherwise valid, ASCII-only project declaring encoding="windows-1252" is now skipped at its declaration unless a code-page provider is registered. The extractor targets .NET 10 and does not register one, so this loses package and framework references that the old StreamReader path found. Register CodePagesEncodingProvider before creating XML readers, while keeping stream-based decoding for correctly declared encodings.

Medium severity Recognize SDK attribute in project imports

csharp/​extractor/​Semmle.Extraction.CSharp.DependencyFetching/​FileContent.cs:275

Projects that declare their SDK with <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" /> now leave IsSdkProjectStructureUsed false; the removed regex recognized this form. In repositories using only these imports, that skips Razor/.resx source generation and falls back to the executing runtime. Check the Sdk attribute in this branch as well, and add an import-form regression test.

2 resolved since last review

Review effort: Max

@michaelnebel
michaelnebel force-pushed the csharp/filecontentparseasxml branch 3 times, most recently from dfcb16e to 583c1ae Compare November 2, 2026 10:24
@michaelnebel
michaelnebel force-pushed the csharp/filecontentparseasxml branch from e886c57 to fd2ad1f Compare November 2, 2026 14:14

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants