Skip to content

Carry the source file's using directives into the generated file - #153

Merged
virzak merged 2 commits into
masterfrom
fix/documentation-in-generated-files
Jul 28, 2026
Merged

Carry the source file's using directives into the generated file#153
virzak merged 2 commits into
masterfrom
fix/documentation-in-generated-files

Conversation

@virzak

@virzak virzak commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Problem

Documentation comments are copied into the generated file verbatim, but a cref in one is resolved against the file it lands in, not the file it was written in. The generated file carries none of the source file's using directives, so every cref which relied on one goes unresolved:

warning CS1574: XML comment has cref attribute 'Task' that could not be resolved
warning CS1574: XML comment has cref attribute 'ArgumentException' that could not be resolved

For a project which treats warnings as errors - which is most of the ones worth adopting this - that is a build failure, not a nit. Found while converting SSH.NET, where a single converted method produced seven of them.

Fix

Emit the directives the source file declared, on the side of the namespace they were declared on:

// <auto-generated/>
#nullable enable

using System.Text;

namespace Test
{
    using System.Net.Sockets;

    public partial class Class
    {

The distinction matters: a directive written inside a namespace may name another namespace relatively, and would not resolve if hoisted above it. The generated code itself is fully qualified and needs none of them, so nothing here changes what the code means - only what the documentation can refer to.

Also in this change: a <returns> element is dropped when the synchronized method returns nothing, because the task it describes is no longer returned. A <returns> describing a value which survives is kept - it is imprecise about the task it mentions, but dropping it would say less than saying it imprecisely. Rewriting the prose is not something the generator can do.

Verification

First commit records both behaviours as they are today, so the second commit's diff is exactly what changed. 19 existing snapshots pick up the directives their sources declared; no generated code changed.

Full suite passes on both target frameworks (338 and 297) and all three Roslyn variants build.

🤖 Generated with Claude Code

virzak added 2 commits July 28, 2026 00:51
Two cases, both recorded here as the generator currently handles them so that
the fix shows what changes:

A cref is resolved against the file it lands in, not the file it was written
in. The generated file carries neither the using directives from the top of
the source file nor those inside its namespace, so a cref which relied on one
goes unresolved and the compiler reports CS1574 - a build failure for the many
projects which treat warnings as errors.

A method which returned a task returns nothing once synchronized, so a
<returns> element describing that task describes something which no longer
exists. The element is copied across regardless.

Generated with Claude Code
Documentation is copied into the generated file verbatim, and a cref in it is
resolved against the file it lands in. Whatever the crefs relied on being in
scope has to land there too, or the compiler reports CS1574 for each one and a
project which treats warnings as errors fails to build.

The directives are emitted on the side of the namespace they were declared on:
one written inside a namespace may name another only relatively, and would not
resolve from above it. The generated code itself is fully qualified and does
not need any of them, so nothing here changes what the code means.

Also drop a <returns> element when the synchronized method returns nothing,
since the task it describes is no longer returned. A <returns> describing a
value which survives is kept: it is imprecise about the task it mentions, but
dropping it would say less than saying it imprecisely.

Generated with Claude Code
@codecov

codecov Bot commented Jul 28, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.67442% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.92%. Comparing base (9205073) to head (fbda0c4).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...p.SyncMethodGenerator/SyncMethodSourceGenerator.cs 90.90% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #153      +/-   ##
==========================================
+ Coverage   93.82%   93.92%   +0.09%     
==========================================
  Files           9        9              
  Lines        1522     1563      +41     
  Branches      352      360       +8     
==========================================
+ Hits         1428     1468      +40     
  Misses         25       25              
- Partials       69       70       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@virzak virzak added the bug Something isn't working label Jul 28, 2026
@virzak
virzak merged commit c949773 into master Jul 28, 2026
10 checks passed
@virzak
virzak deleted the fix/documentation-in-generated-files branch July 28, 2026 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant