Skip to content

Fix invalid single-file codegen for generate-helper-types#735

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-fix-helper-types-single-file
Jul 13, 2026
Merged

Fix invalid single-file codegen for generate-helper-types#735
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:tannergooding-fix-helper-types-single-file

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #657.

In single-file mode (no multi-file), generate-helper-types emitted each generated helper type (NativeTypeNameAttribute, NativeAnnotationAttribute, transparent structs, etc.) with its own using directives and its own namespace wrapper, appended inside the already-open main namespace. The main namespace close was deferred to the end, so the file was malformed: nested/duplicate namespaces, using after a namespace (CS1529), and a trailing stray }. Multi-file mode was unaffected since each helper is its own file.

Fix

In single-file mode (!GenerateMultipleFiles):

  • Hoist the helper types' required using directives (System, System.Diagnostics, and conditionally System.Runtime.InteropServices / System.Runtime.CompilerServices / the transparent-struct handle namespace) into the top-of-file using block.
  • Emit [assembly: DisableRuntimeMarshalling] before the namespace (assembly attributes and usings are illegal after a namespace).
  • Emit each helper type body directly into the already-open shared namespace, with no per-helper using or namespace open/close.
  • Handle the file-scoped-namespace single-file case as well.

Multi-file output is byte-for-byte unchanged (verified by diffing pre-fix vs post-fix generated output).

Tests

  • Added HelperTypesTest covering the valid single-file output for both block-scoped and file-scoped namespaces.
  • Updated TransparentStructTest golden output, which previously asserted the invalid (malformed) output, to the corrected valid single-file shape.
  • Full generator suite passes (Failed: 0).

Note

This PR (including this description) was drafted by Copilot.

In single-file mode each generated helper type was emitted with its own using directives and namespace wrapper appended inside the already-open main namespace, producing nested namespaces, usings after a namespace, and a stray trailing brace. Hoist the helper usings to the top of the file, emit the DisableRuntimeMarshalling assembly attribute before the namespace, and emit each helper type body directly into the shared namespace. Multi-file output is unchanged.

Fixes dotnet#657

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding tannergooding merged commit 3a546cd into dotnet:main Jul 13, 2026
14 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.

generate-helper-types generates invalid code when multi-file is not specified

1 participant