Avoid decorating ErrorRecord during CliXml serialization#27528
Open
KirtiRamchandani wants to merge 1 commit into
Open
Avoid decorating ErrorRecord during CliXml serialization#27528KirtiRamchandani wants to merge 1 commit into
KirtiRamchandani wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR prevents Export-Clixml / ConvertTo-CliXml remoting serialization from mutating the input ErrorRecord (and InformationalRecord) by adding remoting-only members to the original object, and adds regression coverage for ErrorRecord.
Changes:
- Add Pester tests asserting
ErrorRecorddoes not gain new extended members after CliXml serialization. - Update serialization logic to serialize a remoting-only wrapper
PSObjectinstead of mutating the originalPSObjectforErrorRecord/InformationalRecord.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| test/powershell/Modules/Microsoft.PowerShell.Utility/clixml.tests.ps1 | Adds regression tests and a helper to compare extended member sets pre/post serialization. |
| src/System.Management.Automation/engine/serialization.cs | Introduces a wrapper-copy strategy for remoting serialization to avoid persisting remoting-only members on the source object. |
Comment on lines
+195
to
+198
| $filePath = Join-Path $subFilePath 'error.xml' | ||
| Write-Error foo 2>$null | ||
| $errorRecord = $Error[0] | ||
| $before = Get-ExtendedMemberName $errorRecord |
Comment on lines
+255
to
+257
| Write-Error foo 2>$null | ||
| $errorRecord = $Error[0] | ||
| $before = Get-ExtendedMemberName $errorRecord |
Comment on lines
+39
to
+43
| function Get-ExtendedMemberName { | ||
| param([object] $InputObject) | ||
|
|
||
| @($InputObject | Get-Member -View Extended | Select-Object -ExpandProperty Name) | ||
| } |
Comment on lines
+1492
to
+1498
| foreach (PSMemberInfo member in source.InstanceMembers) | ||
| { | ||
| if (!member.IsHidden) | ||
| { | ||
| target.Members.Add(member); | ||
| } | ||
| } |
Comment on lines
1541
to
1547
| InformationalRecord informationalRecord = mshSource.ImmediateBaseObject as InformationalRecord; | ||
| if (informationalRecord != null) | ||
| { | ||
| mshSource = GetRemotingSerializationTarget(mshSource); | ||
| informationalRecord.ToPSObjectForRemoting(mshSource); | ||
| isInformationalRecord = true; | ||
| break; |
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.
PR Summary
Fixes #25854.
ConvertTo-CliXmlandExport-CliXmlserializeErrorRecordinstances by adding remoting-specific note properties such asErrorCategory_ActivityandInvocationInfo. The serializer was adding those properties to the caller'sPSObjectwrapper, which caused the originalErrorRecordto gain serialization-only extended members after serialization completed.This change serializes remoting records through a wrapper with local instance members, preserving the generated CliXml fields without decorating the caller's input object.
PR Context
PSObject.Copy()alone still shares the base object's resurrection-table key, so note properties added to the copy are visible on wrappers around the same base object. The new helper resets the serialization wrapper's instance-member collection before adding remoting-only properties, while preserving existing visible instance members for serialization.The same path is used for informational records, so the isolation is applied there too.
PR Checklist
Tests
ErrorRecordincreased extended members from 1 to 13 and addedErrorCategory_*,InvocationInfo,SerializeExtendedInfo, and related serialization notes.Export-ClixmlandConvertTo-CliXml.Start-PSBuild -Configuration Debug -SMAOnlyConvertTo-CliXmlandExport-Clixmladded no extended members to the originalErrorRecord.ErrorCategory_CategoryandConvertFrom-CliXmlround-trips the error information.Start-PSPester -Path .\test\powershell\Modules\Microsoft.PowerShell.Utility\clixml.tests.ps1 -Terse -ThrowOnFailure -SkipTestToolBuild -BinDir <publish>(19 passed)git diff --check(line-ending warning only)