Skip to content

Commit 53b3952

Browse files
Fix bug with ExecuteScriptWithArgsAsync when script is a command (#1569)
1 parent 356efa6 commit 53b3952

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,9 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
10471047

10481048
if (arguments != null)
10491049
{
1050-
// Add CWD from PowerShell if not an absolute path
1051-
if (!Path.IsPathRooted(script))
1050+
// Add CWD from PowerShell if the script is a file (not a command/inline script) and
1051+
// it's not an absolute path.
1052+
if (File.Exists(script) && !Path.IsPathRooted(script))
10521053
{
10531054
try
10541055
{

test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs

+30
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,36 @@ public void Dispose()
9595
this.powerShellContext.Close();
9696
}
9797

98+
[Trait("Category", "DebugService")]
99+
[Fact]
100+
// This regression test asserts that `ExecuteScriptWithArgsAsync` works for both script
101+
// files and, in this case, in-line scripts (commands). The bug was that the cwd was
102+
// erroneously prepended when the script argument was a command.
103+
public async Task DebuggerAcceptsInlineScript()
104+
{
105+
await this.debugService.SetCommandBreakpointsAsync(
106+
new[] { CommandBreakpointDetails.Create("Get-Random") }).ConfigureAwait(false);
107+
108+
Task executeTask =
109+
this.powerShellContext.ExecuteScriptWithArgsAsync(
110+
"Get-Random", string.Join(" ", "-Maximum", "100"));
111+
112+
await this.AssertDebuggerStopped("", 1).ConfigureAwait(false);
113+
this.debugService.Continue();
114+
await executeTask.ConfigureAwait(false);
115+
116+
StackFrameDetails[] stackFrames = debugService.GetStackFrames();
117+
Assert.Equal(StackFrameDetails.NoFileScriptPath, stackFrames [0].ScriptPath);
118+
119+
VariableDetailsBase[] variables =
120+
debugService.GetVariables(stackFrames[0].LocalVariables.Id);
121+
122+
var var = variables.FirstOrDefault(v => v.Name == "$Error");
123+
Assert.NotNull(var);
124+
Assert.True(var.IsExpandable);
125+
Assert.Equal("[ArrayList: 0]", var.ValueString);
126+
}
127+
98128
public static IEnumerable<object[]> DebuggerAcceptsScriptArgsTestData
99129
{
100130
get

0 commit comments

Comments
 (0)