Skip to content

Keep a file's assembly attributes and comments below C# 10 [patch] - #79

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/77-block-namespace-attributes
Nov 13, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/77-block-namespace-attributes

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #77

What was wrong

Below C# 10, WriteSourceFile rebuilt the template inside a namespace { } block, but the rebuilt copy held only Usings and Classes. Any assembly:/module: attributes, file Comments and Documentation on the template were dropped with no diagnostic. For example, a C# 7.3 consumer never got an InternalsVisibleTo or CLSCompliant that the generator emitted.

Change

GeneratorBase.WriteSourceFile, block path only:

  • The comments, documentation and global attributes now go out first, through a copy of the template that holds nothing else. Then the namespace block is written around a copy that holds only the classes.
  • When the file has attributes, its usings also go before the namespace. That way an attribute can be named through them (using System; then [assembly: CLSCompliant(true)]), which is the same order CodeBlocker uses for a file-scoped namespace. A file without attributes still keeps its usings inside the block, as before.
  • The C# 10+ path is unchanged.

Output at C# 7.3:

// Generated from things.json
using System;

[assembly: CLSCompliant(true)]

namespace Generated
{
	public class Alpha
	{
	}
	...
}

Tests

  • New fixture AttributedThingsGenerator: one using, an assembly: CLSCompliant(true) attribute named through that using, and a file comment.
  • AFilesAssemblyAttributesAndCommentsAreKeptAtEveryLanguageVersion (C# 7.3, 9, 10, Latest): the output compiles with no errors, the compiled assembly carries CLSCompliantAttribute, and the comment is present.
  • AFilesAssemblyAttributesAndCommentsPrecedeABlockScopedNamespace (C# 7.3, 9): the comment, the using and the attribute all come before namespace Generated {.
  • Both tests failed before the fix: the attribute was missing from the assembly and the comment was missing from the output. Both pass after it. The full suite passes (104/104).

Generated by Claude Code

The block-scoped namespace path copied only the usings and classes into
the template it wrote inside the block, so a file's assembly:/module:
attributes, comments and documentation vanished for C# 7.3-9 consumers
with no diagnostic. Write them before the namespace block, with the
usings too when there are attributes so the attributes can be named
through them, as CodeBlocker does for a file-scoped namespace.

Fixes #77

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5chbrSDfDAQ2U7T5G
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 120deed into main Nov 13, 2026
15 checks passed
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.

Below C# 10, WriteSourceFile silently drops the template's assembly:/module: attributes and file comments

2 participants