Skip to content

Conversation

@teo-tsirpanis
Copy link
Contributor

Calling ToString() clears the builder, which can cause surprises when debugging.

Calling `ToString()` clears the builder, which can cause surprises when debugging.
Copilot AI review requested due to automatic review settings December 6, 2025 20:25
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Dec 6, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 6, 2025
@teo-tsirpanis teo-tsirpanis added area-System.Runtime and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Dec 6, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a side-effect free debugger display to both versions of ValueStringBuilder to address the problematic behavior where calling ToString() clears the builder, which causes unexpected issues during debugging.

Key changes:

  • Added [DebuggerDisplay] attribute to both ValueStringBuilder and ValueStringBuilder<TChar> structs
  • Created side-effect free DebuggerDisplay properties that display the builder's content without clearing it
  • Refactored ValueStringBuilder<TChar>.ToString() to extract string generation logic into a separate GetString() method

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/Common/src/System/Text/ValueStringBuilder.cs Added [DebuggerDisplay] attribute and DebuggerDisplay property that uses AsSpan().ToString() for side-effect free debugging
src/libraries/Common/src/System/Text/ValueStringBuilder_1.cs Added [DebuggerDisplay] attribute, extracted GetString() method for reuse, and added DebuggerDisplay property that calls GetString() for side-effect free debugging

}

// ToString() clears the builder, so we need a side-effect free debugger display.
private string DebuggerDisplay => AsSpan().ToString();
Copy link

Copilot AI Dec 6, 2025

Choose a reason for hiding this comment

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

The DebuggerDisplay property should be marked as readonly for consistency with the generic version in ValueStringBuilder_1.cs and to clearly indicate it doesn't mutate state. The property only reads from the builder without modifying it.

private readonly string DebuggerDisplay => AsSpan().ToString();
Suggested change
private string DebuggerDisplay => AsSpan().ToString();
private readonly string DebuggerDisplay => AsSpan().ToString();

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants