From 4bb11f5d0bd9cf10ca8f80a61f87ede6b9697c8a Mon Sep 17 00:00:00 2001 From: Dmitry Savy Date: Fri, 24 Jul 2026 16:43:09 -0500 Subject: [PATCH 1/2] Fix BidirectionalMap leaving a stale reverse entry on key update BidirectionalMap.set updated the forward map but never removed the reverse mapping of the key's previous value. After map.set('a', 1) followed by map.set('a', 2), a reverse lookup getKey(1) still returned 'a' even though 'a' now maps to 2. Delete the previous value's reverse entry when an existing key is reassigned. Added a regression test covering key updates. --- src/vs/base/common/map.ts | 5 +++++ src/vs/base/test/common/map.test.ts | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/vs/base/common/map.ts b/src/vs/base/common/map.ts index e9b7a4c065ff2f..3c5fbd5d74ccce 100644 --- a/src/vs/base/common/map.ts +++ b/src/vs/base/common/map.ts @@ -770,6 +770,11 @@ export class BidirectionalMap { } set(key: K, value: V): void { + // Drop the reverse mapping of the previous value when updating an existing key, + // otherwise a lookup by the old value would still resolve to this key. + if (this._m1.has(key)) { + this._m2.delete(this._m1.get(key)!); + } this._m1.set(key, value); this._m2.set(value, key); } diff --git a/src/vs/base/test/common/map.test.ts b/src/vs/base/test/common/map.test.ts index dd7c4a1841a049..e5af65b3e982a2 100644 --- a/src/vs/base/test/common/map.test.ts +++ b/src/vs/base/test/common/map.test.ts @@ -609,6 +609,16 @@ suite('BidirectionalMap', () => { assert.strictEqual(map.delete('four'), false); }); + test('should not leave a stale reverse entry when a key value is updated', () => { + const map = new BidirectionalMap(); + map.set('one', 1); + map.set('one', 2); + + assert.strictEqual(map.get('one'), 2); + assert.strictEqual(map.getKey(2), 'one'); + assert.strictEqual(map.getKey(1), undefined); + }); + test('should handle forEach correctly', () => { const map = new BidirectionalMap(); map.set('one', 1); From 645d7427ab0092b2cfdf4f0f64ebc1fcc2329fb3 Mon Sep 17 00:00:00 2001 From: Dmitry Savy Date: Fri, 24 Jul 2026 16:49:14 -0500 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- src/vs/base/common/map.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/vs/base/common/map.ts b/src/vs/base/common/map.ts index 3c5fbd5d74ccce..6f79e1e81402b0 100644 --- a/src/vs/base/common/map.ts +++ b/src/vs/base/common/map.ts @@ -770,8 +770,6 @@ export class BidirectionalMap { } set(key: K, value: V): void { - // Drop the reverse mapping of the previous value when updating an existing key, - // otherwise a lookup by the old value would still resolve to this key. if (this._m1.has(key)) { this._m2.delete(this._m1.get(key)!); }