From 2bc0c039490fef1df66f046f5a91cb34b5713b2a Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Tue, 31 Aug 2021 19:04:14 -0700 Subject: [PATCH] Fix bug with `ExecuteScriptWithArgsAsync` when `script` is a command --- .../PowerShellContextService.cs | 5 ++-- .../Debugging/DebugServiceTests.cs | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index ec3923db0..ad35c519e 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -1047,8 +1047,9 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n if (arguments != null) { - // Add CWD from PowerShell if not an absolute path - if (!Path.IsPathRooted(script)) + // Add CWD from PowerShell if the script is a file (not a command/inline script) and + // it's not an absolute path. + if (File.Exists(script) && !Path.IsPathRooted(script)) { try { diff --git a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs index 8dd322d0a..f743ab255 100644 --- a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs @@ -95,6 +95,36 @@ public void Dispose() this.powerShellContext.Close(); } + [Trait("Category", "DebugService")] + [Fact] + // This regression test asserts that `ExecuteScriptWithArgsAsync` works for both script + // files and, in this case, in-line scripts (commands). The bug was that the cwd was + // erroneously prepended when the script argument was a command. + public async Task DebuggerAcceptsInlineScript() + { + await this.debugService.SetCommandBreakpointsAsync( + new[] { CommandBreakpointDetails.Create("Get-Random") }).ConfigureAwait(false); + + Task executeTask = + this.powerShellContext.ExecuteScriptWithArgsAsync( + "Get-Random", string.Join(" ", "-Maximum", "100")); + + await this.AssertDebuggerStopped("", 1).ConfigureAwait(false); + this.debugService.Continue(); + await executeTask.ConfigureAwait(false); + + StackFrameDetails[] stackFrames = debugService.GetStackFrames(); + Assert.Equal(StackFrameDetails.NoFileScriptPath, stackFrames [0].ScriptPath); + + VariableDetailsBase[] variables = + debugService.GetVariables(stackFrames[0].LocalVariables.Id); + + var var = variables.FirstOrDefault(v => v.Name == "$Error"); + Assert.NotNull(var); + Assert.True(var.IsExpandable); + Assert.Equal("[ArrayList: 0]", var.ValueString); + } + public static IEnumerable DebuggerAcceptsScriptArgsTestData { get