From 7f7f5c58e96e88c0653e45f058b9590d0ce455d0 Mon Sep 17 00:00:00 2001
From: Phantom <P_hanto_m@outlook.com>
Date: Wed, 31 Jul 2024 18:12:36 +0400
Subject: [PATCH] Call FetchStackFramesAndVariablesAsync in
 Microsoft.PowerShell.EditorServices.Services.DebugService.SetVariableAsync
 and in
 Microsoft.PowerShell.EditorServices.Services.DebugService.EvaluateExpressionAsync
 to invalidate variables cache and use actual variables data.

---
 .../Services/DebugAdapter/DebugService.cs                 | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs b/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs
index 667a26bdd..54ff62633 100644
--- a/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs
+++ b/src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs
@@ -481,6 +481,10 @@ public async Task<string> SetVariableAsync(int variableContainerReferenceId, str
             // This makes the returned string consistent with the strings normally displayed for variables in the debugger.
             VariableDetails tempVariable = new(psVariable);
             _logger.LogTrace($"Set variable '{name}' to: {tempVariable.ValueString ?? "<null>"}");
+            
+            // Fetch stack frames and variables again to have actual data in the variables field.
+            // Without this, GetVariables and other methods that use the variables field will use old data.
+            await FetchStackFramesAndVariablesAsync(null).ConfigureAwait(false);
             return tempVariable.ValueString;
         }
 
@@ -508,6 +512,10 @@ public async Task<VariableDetails> EvaluateExpressionAsync(
                     command,
                     cancellationToken,
                     new PowerShellExecutionOptions { WriteOutputToHost = writeResultAsOutput, ThrowOnError = !writeResultAsOutput }).ConfigureAwait(false);
+                
+                // Fetch stack frames and variables again to have actual data in the variables field.
+                // Without this, GetVariables and other methods that use the variables field will use old data.
+                await FetchStackFramesAndVariablesAsync(null).ConfigureAwait(false);
             }
             catch (Exception e)
             {