From e3212c55055144e630d25d5014b2c51eca159d50 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 14 Jun 2023 22:03:41 -0700 Subject: [PATCH 1/4] Add additional platform check for Windows before launching native app. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change ensures that we do not get the cursor position in preparation for the BufferCell call since it is only supported on Windows. On non-Windows machines this might also cause problems for applications such as xpect in which pwsh runs in a session connected by a pty. This change should address the misbehavior described in issue #18874. --- .../engine/NativeCommandProcessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index 2168365c747..a0505aa4d16 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1691,7 +1691,7 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect if (_runStandAlone) { - if (s_supportScreenScrape == null) + if (s_supportScreenScrape == null && Platform.IsWindows) { try { From 9fb9768afccb38df47b48668e04a4fde28c83705 Mon Sep 17 00:00:00 2001 From: James Truher Date: Wed, 14 Jun 2023 22:50:12 -0700 Subject: [PATCH 2/4] Add test for not getting cursor position. --- .../NativeCommandProcessor.Tests.ps1 | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index 41e3f3cebcf..5b11e72b78a 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -366,3 +366,28 @@ Describe "Run native command from a mounted FAT-format VHD" -tags @("Feature", " $result | Should -BeExactly $expected } } + +Describe "Native application invocation and getting cursor position" -Tags 'CI' { + It "Invoking a native application should not collect the cursor position" -Skip:($IsWindows) { + $expectCmd = Get-Command expect -Type Application -ErrorAction Ignore + $dateCmd = Get-Command date -Type Application -ErrorAction Ignore + # if date or expect are missing mark the test as pending + # test setup will need to ensure that these programs are present. + $missing = @() + if ($null -eq $expectCmd) { + $missing += "expect" + } + if ($null -eq $dateCmd) { + $missing += "date" + } + if ($missing.count -ne 0) { + $message = "missing command(s) {0}" -f ($missing -join ", ") + Set-ItResult -Pending $message + } + + $powershell = Join-Path -Path $PSHOME -ChildPath "pwsh" + $commandString = "spawn $powershell -nopro -c /bin/date; expect eof" + [string]$result = expect -c $commandString + $result.IndexOf("`e[6n") | Should -Be -1 -Because $result.replace("`e","``e").replace("`u{7}","") + } +} From da5116ac186cc5e0cd31159d4398f6ce83040e9a Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 15 Jun 2023 23:41:29 -0700 Subject: [PATCH 3/4] Use #if rather than runtime check to determine whether we can screen scrape. --- .../engine/NativeCommandProcessor.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/NativeCommandProcessor.cs b/src/System.Management.Automation/engine/NativeCommandProcessor.cs index a0505aa4d16..a5cc7874a6b 100644 --- a/src/System.Management.Automation/engine/NativeCommandProcessor.cs +++ b/src/System.Management.Automation/engine/NativeCommandProcessor.cs @@ -1691,8 +1691,11 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect if (_runStandAlone) { - if (s_supportScreenScrape == null && Platform.IsWindows) + if (s_supportScreenScrape == null) { +#if UNIX + s_supportScreenScrape = false; +#else try { _startPosition = this.Command.Context.EngineHostInterface.UI.RawUI.CursorPosition; @@ -1704,6 +1707,7 @@ private void CalculateIORedirection(bool isWindowsApplication, out bool redirect { s_supportScreenScrape = false; } +#endif } // if screen scraping isn't supported, we enable redirection so that the output is still transcribed From 165cee246665d2089d461012c26581760f7ade26 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 19 Jun 2023 10:30:16 -0700 Subject: [PATCH 4/4] Update invocation of 'Set-ItResult' --- .../Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 index 5b11e72b78a..9542aafe368 100644 --- a/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 +++ b/test/powershell/Language/Scripting/NativeExecution/NativeCommandProcessor.Tests.ps1 @@ -382,7 +382,7 @@ Describe "Native application invocation and getting cursor position" -Tags 'CI' } if ($missing.count -ne 0) { $message = "missing command(s) {0}" -f ($missing -join ", ") - Set-ItResult -Pending $message + Set-ItResult -Pending -Because $message } $powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"