Skip to content

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

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

ghostflyby wants to merge 8 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

@ForNeVeR
ForNeVeR self-requested a review October 21, 2026 09:20
@ghostflyby
ghostflyby marked this pull request as ready for review October 21, 2026 10:12
@ForNeVeR ForNeVeR self-assigned this Oct 24, 2026
…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.
- 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.
…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.
A handwritten member only covers the BCL overload it forwards to when its own
parameter list is the leading part of the overload's generated parameter list,
i.e. when it is that overload with its trailing arguments pinned. Overload
identity is the parameter list alone, so a member that forwards to the same BCL
method with a different parameter type is an addition next to the generated one
rather than a replacement for it.

GetFiles(AbsolutePath, LocalPathPattern) is exactly that case: it forwards to
Directory.GetFiles(string, string) but replaces the second parameter type, so
the old blanket "forwards to it" rule suppressed GetFiles(AbsolutePath, string)
and silently dropped it from the public API.

Signature identity also ignores nullability now, because C# overloads do not
differ by it and because a reference assembly compiled before nullable reference
types existed reports every reference type as oblivious.

AppendAllText and WriteAllText keep handwritten `string? contents` overloads for
the same reason: netstandard2.0's reference assembly has no NRT metadata, so the
generator would render a non-nullable parameter and break the released contract.
The BCL accepts null there by design (it writes an empty file or nothing), which
the released baseline records as `string?`.

The public API baselines gain the newly generated members: 25 for net8.0 and 16
for netstandard2.0.
@ghostflyby
ghostflyby force-pushed the pathio-source-generator branch from d64286b to b154907 Compare November 9, 2026 06:00
Directory.GetParent returns null for a filesystem root, and the net8.0 baseline
already records GetParent as `DirectoryInfo?`. The netstandard2.0 reference
assembly carries no NRT metadata, so the generator renders the same member as
non-nullable and the baseline recorded a `DirectoryInfo!` contract that the
implementation does not honour. Declare the member by hand for the same reason
AppendAllText and WriteAllText are handwritten.

Also compare generated overload keys against handwritten ones with the
nullability-insensitive renderer on both sides. Previously the generated key kept
its annotations while the handwritten key dropped them, so the two only matched
for oblivious reference assemblies.
ghostflyby added a commit to ghostflyby/TruePath that referenced this pull request Nov 9, 2026
Add a net10.0 target (the current LTS) to TruePath.SystemIo. With the source
generator from ForNeVeR#249 in place this is nearly a one-line change: the generator
projects the per-target-framework BCL automatically, exposing 123 extension
methods on this target, including the new span- and Memory-based overloads:

- AppendAllText/WriteAllText with ReadOnlySpan<char> contents,
- AppendAllBytes/WriteAllBytes with ReadOnlySpan<byte>,
- the ReadOnlyMemory-based async counterparts of all of the above.

The existing netstandard2.0 and net8.0 targets are unchanged.

The public API baselines gain the net10.0 surface. The four handwritten
`string?` overloads and GetParent are kept as handwritten members here as well,
so the nullable contracts match the other targets and only the ReadOnlySpan
overloads are generated alongside them.

TruePath.Tests also gains a net10.0 target so the reflection completeness test
validates the generated net10.0 surface against the matching runtime.

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.

2 participants