Skip to content

Reset keyToScope and lastScope in ScopeInfoBuilder.build() so a reused builder does not leak state#7

Merged
szuend merged 2 commits into
ChromeDevTools:mainfrom
gunjanjaswal:fix/builder-reset-keyToScope-lastScope
Jul 8, 2026
Merged

Reset keyToScope and lastScope in ScopeInfoBuilder.build() so a reused builder does not leak state#7
szuend merged 2 commits into
ChromeDevTools:mainfrom
gunjanjaswal:fix/builder-reset-keyToScope-lastScope

Conversation

@gunjanjaswal

Copy link
Copy Markdown
Contributor

Summary

ScopeInfoBuilder.build() returns the accumulated ScopeInfo and resets the builder so it can be reused. It clears #scopes, #ranges, and #knownScopes, but leaves #keyToScope and #lastScope populated, so state from a previous build leaks into the next one.

If you reuse a builder across two build() calls, a startRange({ scopeKey }) in the second build resolves originalScope from a key registered during the first build, linking the range to a scope that is not part of the current tree. Encoding that result then throws Unknown OriginalScope for definition!. A freshly constructed builder produces no such link. lastScope() has the same leak: it returns a scope from the prior build after a build() that opened none.

Fix

Clear #keyToScope and reset #lastScope in build(), alongside the existing resets.

Test

Added a case in builder.test.ts: register key: 0 and build once, then in a second build resolve scopeKey: 0 and assert the range's originalScope is undefined (and lastScope() is null). It fails before the change and passes after.

@szuend szuend merged commit aaf57ee into ChromeDevTools:main Jul 8, 2026
7 checks passed
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