Fix invalid single-file codegen for generate-helper-types#735
Merged
tannergooding merged 1 commit intoJul 13, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #657.
In single-file mode (no
multi-file),generate-helper-typesemitted each generated helper type (NativeTypeNameAttribute,NativeAnnotationAttribute, transparent structs, etc.) with its ownusingdirectives and its ownnamespacewrapper, appended inside the already-open main namespace. The main namespace close was deferred to the end, so the file was malformed: nested/duplicate namespaces,usingafter 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):usingdirectives (System,System.Diagnostics, and conditionallySystem.Runtime.InteropServices/System.Runtime.CompilerServices/ the transparent-struct handle namespace) into the top-of-file using block.[assembly: DisableRuntimeMarshalling]before the namespace (assembly attributes and usings are illegal after a namespace).usingornamespaceopen/close.Multi-file output is byte-for-byte unchanged (verified by diffing pre-fix vs post-fix generated output).
Tests
HelperTypesTestcovering the valid single-file output for both block-scoped and file-scoped namespaces.TransparentStructTestgolden output, which previously asserted the invalid (malformed) output, to the corrected valid single-file shape.Failed: 0).Note
This PR (including this description) was drafted by Copilot.