Skip to content

Commit b8dbaa6

Browse files
committed
Refactor runspace creation in TypeGetter.cs
Updated `GetFormatViewDefinitionForType` to create a runspace with the default initial session state, ensuring proper loading of format data. Removed redundant fallback logic for `Get-FormatData` invocation. Added `Debug.Assert` to validate non-null results. Updated comments to reflect the new behavior. Included `System.Diagnostics` for debugging utilities.
1 parent b0cdb95 commit b8dbaa6

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/Microsoft.PowerShell.ConsoleGuiTools/TypeGetter.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Globalization;
67
using System.Linq;
78
using System.Management.Automation;
@@ -29,9 +30,9 @@ public class TypeGetter
2930
{
3031
if (_formatCache.TryGetValue(typeName, out var cached)) return cached;
3132

32-
// Always create a new runspace to avoid pipeline concurrency issues
33-
// when called from within an active PowerShell cmdlet/pipeline
34-
using var runspace = RunspaceFactory.CreateRunspace();
33+
// Create a runspace with the default initial session state to load format data
34+
var iss = InitialSessionState.CreateDefault();
35+
using var runspace = RunspaceFactory.CreateRunspace(iss);
3536
runspace.Open();
3637

3738
try
@@ -42,21 +43,14 @@ public class TypeGetter
4243

4344
var results = ps.Invoke();
4445

45-
if (results.Count == 0)
46-
{
47-
ps.Commands.Clear();
48-
ps.AddCommand("Get-FormatData")
49-
.AddParameter("TypeName", typeName);
50-
results = ps.Invoke();
51-
}
52-
5346
FormatViewDefinition? result = null;
5447
if (results.Count > 0)
5548
{
5649
var extendedTypeDefinition = results[0].BaseObject as ExtendedTypeDefinition;
5750
result = extendedTypeDefinition?.FormatViewDefinition[0];
5851
}
5952

53+
Debug.Assert(result is not null);
6054
_formatCache[typeName] = result;
6155
return result;
6256
}

0 commit comments

Comments
 (0)