Skip to content

Write a source file's global attributes after its usings [patch] - #148

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/issue-143-file-attributes-after-usings
Oct 30, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/issue-143-file-attributes-after-usings

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #143

What changed

  • SourceFileTemplateExtensions.AddSourceFile used to write the template's attributes first, through AddTemplate, and then the namespace and the usings. A file-level attribute can only be global (assembly: / module:), so any file that set both Attributes and Usings failed to compile: CS1529 with no namespace, and CS0246 with one, because the attribute sat outside the namespace the usings were declared in.
  • When the file has attributes, it now writes the usings, then the attributes followed by a blank line, then the namespace. This is the only layout in which the attribute resolves through the file's own usings.
  • A file with no attributes renders exactly as before, so the golden output and the existing ASourceFileWritesItsPreambleThenItsTypes test are unchanged.
  • Documentation and comments still come first in both layouts. I added a <remarks> explaining the ordering.
  • I did not add the optional rejection of Keywords on a file template. That would be a behaviour change beyond this bug.

Tests

  • Two new tests in TemplateTests: ASourceFileWritesItsGlobalAttributesAfterItsUsingsAndBeforeItsNamespace and ASourceFileWithNoNamespaceWritesItsGlobalAttributesAfterItsUsings.
  • With the fix reverted, both tests fail.
  • I compiled both rendered layouts in a scratch net10.0 class library. Both build cleanly.
  • The full suite passes (225/225, net10.0).

Generated by Claude Code

AddSourceFile wrote the template's attributes first, so a file with both
attributes and usings put [assembly: X] above the usings (CS1529) or
outside the file-scoped namespace the usings were in (CS0246). When the
file has attributes it now writes the usings, then the attributes, then
the namespace. A file without attributes renders as before.

Fixes #143

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 79a5e99 into main Oct 30, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the claude/issue-143-file-attributes-after-usings branch October 30, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants