Skip to content

fix: Private nested types in NetworkVariables in CoreCLR - #4239

Open
EmandM wants to merge 2 commits into
develop-3.x.xfrom
fix/networkVariable-CoreCLR
Open

EmandM wants to merge 2 commits into
develop-3.x.xfrom
fix/networkVariable-CoreCLR

Conversation

@EmandM

@EmandM EmandM commented Nov 3, 2026

Copy link
Copy Markdown
Member

Purpose of this PR

When we generate the serialization code for NetworkVariables, we add code at the assembly level. Mono, for whatever reason, didn't complain if there were private or protected types in the serialization generation code. CoreCLR throws an error if we try to generate serialization for private types.

This PR detects this case and logs a warning in Mono, and an error in CoreCLR. This gives users a human-readable error message with information on how to fix the issue, and lets the code generation safely continue.

Jira ticket

UUM-156224

Changelog

  • Fixed: NetworkVariable serialization now logs a clear error message when using CoreCLR and generating serialization for private or protected types.

Documentation

  • No documentation changes or additions were necessary.

Testing & QA (How your changes can be verified during release Playtest)

Functional Testing

Manual testing :

  • Manual testing done

Automated tests:

  • Covered by existing automated tests
  • Covered by new automated tests

Does the change require QA team to:

  • Review automated tests?
  • Execute manual tests?
  • Provide feedback about the PR?

If any boxes above are checked the QA team will be automatically added as a PR reviewer.

Up-port

n/a

Backports

2.x doesn't need to support CoreCLR

@EmandM
EmandM requested a review from a team as a code owner November 3, 2026 16:46

@u-pr u-pr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Harness Review

Needs changes

The new diagnostics improve failure reporting, but the accessibility validation does not yet cover all supported serialization-generation contexts.

Reviewed commit 6624e66

🤖 Helpful? 👍/👎


for (var definition = type.Resolve(); definition != null && definition.IsNested; definition = definition.DeclaringType)
{
if ((definition.IsNestedPrivate || definition.IsNestedFamily || definition.IsNestedFamilyAndAssembly) && !hidden.Contains(definition))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1

InitializeSerialization still fails on CoreCLR when NetworkVariable<Payload> uses a referenced base's protected internal nested enum without friend access. A derived NetworkBehaviour can legally declare that field using protected access. GetAllFieldsAndResolveGenerics collects Payload, but this predicate does not check IsNestedFamilyOrAssembly. The generated NetworkVariableSerializationHelper cannot access Payload because the helper is outside the base's assembly and inheritance hierarchy. Pass the consuming assembly into CollectTypesHiddenFromAssembly. Reject IsNestedFamilyOrAssembly types when that assembly lacks internal or friend access.

🤖 Helpful? 👍/👎

type.CollectTypesHiddenFromAssembly(hiddenTypes);
if (hiddenTypes.Count > 0)
{
#if ENABLE_CORECLR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2

The warning/error decision can use the wrong backend when the editor and player runtimes differ. This #if uses the Editor-only ILPP assembly's compile-time defines, while assemblyDefines describes the assembly receiving the generated calls. The CI matrix includes Mono 6000.7 editors building CoreCLR players. If their ENABLE_CORECLR values differ, code generation only warns and retains calls that CoreCLR rejects. Select the branch using assemblyDefines.Contains("ENABLE_CORECLR").

🤖 Helpful? 👍/👎

@EmandM
EmandM requested a review from jabbacakes as a code owner November 3, 2026 16:53
@codecov-github-com

codecov-github-com Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

@@              Coverage Diff               @@
##           develop-3.x.x    #4239   +/-   ##
==============================================
  Coverage          76.90%   76.90%           
==============================================
  Files                159      159           
  Lines              27663    27663           
==============================================
  Hits               21274    21274           
  Misses              6389     6389           
Flag Coverage Δ
NGOv2_project_testproject_ubuntu 75.95% <ø> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

see 5 files with indirect coverage changes

Components Coverage Δ
com.unity.netcode.gameobjects 76.90% <100.00%> (ø)

ℹ️ Need help interpreting these results?


CoreCLR introduces a new compilation pipeline that changes some custom serialization generation.

## NetworkVariable serialization

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## NetworkVariable serialization
### NetworkVariable serialization

Just to make it clear that this is related to the CoreCLR stuff above.

This branch has not been deployed

No deployments
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.

2 participants