Fix stableStringify treating shared references as circular#327398
Open
dsavy4 wants to merge 3 commits into
Open
Fix stableStringify treating shared references as circular#327398dsavy4 wants to merge 3 commits into
dsavy4 wants to merge 3 commits into
Conversation
_stableStringify uses a WeakSet to guard against circular references, but
it never removed a value after serializing its subtree. As a result a
value that is shared across sibling branches (a DAG, not a cycle) was
reported as "[Circular]" on every occurrence after the first.
For example stableStringify({ x: shared, y: shared }) returned
{"x":{"a":1},"y":"[Circular]"} instead of
{"x":{"a":1},"y":{"a":1}}.
Track only the current ancestor path by removing the value from the set
once its subtree is done. True cycles are still detected. Added tests for
shared references in both object and array positions.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes stableStringify to distinguish shared references from circular references.
Changes:
- Tracks only the active ancestor path during serialization.
- Adds object and array shared-reference regression tests.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/vs/base/common/objects.ts |
Removes serialized values from cycle tracking. |
src/vs/base/test/common/objects.test.ts |
Tests repeated shared references. |
Author
|
@microsoft-github-policy-service agree |
Co-authored-by: Copilot Autofix powered by AI <175728472+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.
_stableStringifyguards against circular references with aWeakSet, but it never removes a value from the set after serializing its subtree. So a value that is shared across sibling branches, which is a DAG and not a cycle, gets reported as"[Circular]"on every occurrence after the first.Repro:
This matters because
stableStringifyis used to compare objects for change detection (for example in the chat voice client,stableStringify(current[key]) !== stableStringify(prev[key])), where a shared reference can make two structurally equal objects compare as different.The fix tracks only the current ancestor path by removing the value from the set once its subtree is done, so true cycles are still detected while shared references serialize in full. Added tests for shared references in both object and array positions, and the existing circular-reference test still passes.