Skip to content

Commit bc604c9

Browse files
Address code review: evict only the faulted cache entry, clarify test path
Co-authored-by: jessehouwing <4173387+jessehouwing@users.noreply.github.com>
1 parent 39b5356 commit bc604c9

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎Engine/CommandInfoCache.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Concurrent;
6+
using System.Collections.Generic;
67
using System.Management.Automation;
78
using System.Linq;
89
using System.Management.Automation.Runspaces;
@@ -86,8 +87,10 @@ public CommandInfo GetCommandInfo(string commandName, CommandTypes? commandTypes
8687
{
8788
// Lazy<T> caches exceptions forever, which would make every subsequent lookup of this
8889
// command fail for the lifetime of the process. Evict the entry so that the next lookup
89-
// can try again.
90-
_commandInfoCache.TryRemove(key, out _);
90+
// can try again. Only remove the faulted instance so that a replacement that another
91+
// thread may already have added is left alone.
92+
((ICollection<KeyValuePair<CommandLookupKey, Lazy<CommandInfo>>>)_commandInfoCache)
93+
.Remove(new KeyValuePair<CommandLookupKey, Lazy<CommandInfo>>(key, lazyCommandInfo));
9194
throw;
9295
}
9396
}

‎Tests/Rules/Issue2205.tests.ps1‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
Describe 'Issue 2205' {
55
It "does not fail the analysis when a command lookup hits the runspace affinity problem" -Skip:(-not $IsLinux) {
66
$settingsPath = Join-Path $PSScriptRoot 'Issue2205/PSScriptAnalyzerSettings.psd1'
7-
$repositoryRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
7+
# $PSScriptRoot is <repo>/Tests/Rules, so two levels up is the repository root.
8+
$repositoryRoot = (Resolve-Path (Join-Path $PSScriptRoot '..' '..')).Path
89

910
Invoke-ScriptAnalyzer -Path $repositoryRoot -Recurse -Settings $settingsPath -ErrorAction Stop | Out-Null
1011
}

0 commit comments

Comments
 (0)