Skip to content

PathIo: generate System.IO forwarding APIs with a Roslyn source generator - #249

Open
ghostflyby wants to merge 6 commits into
ForNeVeR:mainfrom
ghostflyby:pathio-source-generator
Open

ghostflyby wants to merge 6 commits into
ForNeVeR:mainfrom
ghostflyby:pathio-source-generator

Conversation

@ghostflyby

Copy link
Copy Markdown

Summary

Introduce a Roslyn incremental source generator (TruePath.PathIoGenerator) that fills the other partial part of PathIo (marked with [GenerateSystemIoExtensions]) with forwarding extension methods for every public static File/Directory method of the current target framework, copying the BCL XML documentation verbatim. PathIo.cs shrinks to the members with custom behavior or documentation.

Changes

  • New TruePath.PathIoGenerator project referenced by TruePath.SystemIo as an analyzer (PrivateAssets=all, never packed into the NuGet package).
  • Hand-written members win over generated ones (by rewritten signature or by the forwarding call in their body); File wins over Directory on duplicate signatures; path-io-exclusions.txt lists deliberately unexposed APIs.
  • BCL XML docs reach the generator via AdditionalFiles (the command-line compiler does not attach them to references), are extracted line-by-line to survive malformed netstandard.xml, and param/paramref names are normalized to the generated parameter names (keeping the historical sourceFile/destFile spellings).
  • Generated code uses short type names for imported namespaces; new APIs appear automatically per target framework (93 methods on net8.0, 66 on netstandard2.0, including previously missing symlink, OpenHandle, Replace, and enumeration APIs).
  • Tests: generator unit tests plus a reflection completeness test asserting BCL API is a subset of generated, hand-written, or explicitly excluded.

Testing

…rator

Add TruePath.PathIoGenerator, a Roslyn incremental source generator that
fills the other partial part of PathIo (marked with
GenerateSystemIoExtensionsAttribute) with forwarding extension methods for
every public static File/Directory method of the current target framework,
with the BCL XML documentation copied verbatim.

- PathIo.cs keeps only the members with custom behavior or documentation;
  pure forwarders are generated (93 methods on net8.0, 66 on
  netstandard2.0), introducing previously missing APIs (symlinks,
  OpenHandle, Replace, EnumerateDirectories, GetParent, etc.).
- Hand-written members win over generated ones, both by rewritten
  signature and by the forwarding call inside their body; File wins over
  Directory on duplicate signatures; path-io-exclusions.txt lists APIs
  deliberately not exposed.
- BCL XML docs reach the generator via AdditionalFiles (the command-line
  compiler does not attach them to references), are extracted
  line-by-line to survive malformed netstandard.xml, and param/paramref
  names are normalized against the generated parameters; path parameters
  keep the historical sourceFile/destFile names.
- Generated code uses short type names for imported namespaces.
- Tests: generator unit tests plus a reflection completeness test
  asserting BCL API ⊆ generated ∪ hand-written ∪ exclusions.
…ndex

- Drop dead test helpers, redundant nullability and qualifiers, an unused
  RenderFile parameter, and a redundant currentName check in BclDocIndex.
- Store parsed XElement members in the doc index instead of raw strings:
  XElement.Parse is now called exactly once per member with its result
  actually used, instead of a validation-only call with a discarded
  return value followed by a second parse at emission time. The cached
  element is cloned before doc formatting, which renames param elements
  in place.
@ForNeVeR
ForNeVeR self-requested a review October 21, 2026 09:20
- Strip the UTF-8 BOM from TruePath.sln introduced by an editor.
- Add a REUSE license companion for path-io-exclusions.txt.
- Pin the generator test references to the Basic.Reference.Assemblies
  package: discovering the SDK's reference packs from the test host's
  runtime directory is not reliable on CI runners where the runtime and
  the SDK live in different roots. The test helper now also surfaces the
  driver diagnostics when the expected generated tree is missing.
- The completeness test now mirrors the generator's third exclusion rule:
  BCL overloads that hand-written members forward to with path.Value as
  the first argument (previously flagged Directory.Delete(string,bool)).
The SPDX tags emitted into the generated file are string literals in the
generator source; REUSE picked them up as this file's own tags and failed
on the trailing C# syntax inside the expression.
- Reject nested marked classes with the new TPG004 diagnostic instead of
  silently generating a same-named top-level class, and derive the hint
  name from the full type name (sanitized) so same-named types cannot
  collide.
- Hoist the exclusion file texts and the XML documentation index into
  their own cached pipeline nodes: the .xml filter now happens before
  BclDocIndex.Build, so editing path-io-exclusions.txt no longer
  re-scans the ~15 MB documentation files, and the exclusion file is
  matched by exact file name.
- Move the hand-written member analysis into the syntax-anchored
  ForAttributeWithMetadataName transform (equatable MarkedType payload
  with sorted arrays), so it is cached across edits in unrelated files
  instead of re-running on every compilation change.
- Clone the member element inside DocFormatting.ToDocLines rather than
  at the call site, and read the XML documentation files through
  SourceText.Lines instead of ToString/Replace/Split (three fewer full
  copies of the text).

Generated output verified byte-identical on both target frameworks
before and after the change.
@ghostflyby
ghostflyby marked this pull request as ready for review October 21, 2026 10:12
…int names

- Render every special type with its C# keyword inside generic arguments
  too: net10 span-based APIs like ReadOnlySpan<char> previously rendered
  as ReadOnlySpan<Char> because System_Char was missing from the table.
- Emit the generated file under the simple type name again
  (PathIo.Generated.g.cs): the full-name sanitization was only needed
  for nested marked types, which are now rejected with TPG004.

Both verified by temporarily building TruePath.SystemIo against net10.0,
which generates 112 methods including the new span- and Memory-based
overloads without any rule changes.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant