Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected backslash with OSC 9;9 prompt #3719

Open
5 tasks done
yan12125 opened this issue Jun 18, 2023 · 10 comments
Open
5 tasks done

Unexpected backslash with OSC 9;9 prompt #3719

yan12125 opened this issue Jun 18, 2023 · 10 comments
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation.

Comments

@yan12125
Copy link

Prerequisites

Steps to reproduce

  1. Configure OSC 9;9 for PowerShell and add custom prompt in $PROFILE following https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory#powershell-powershellexe-or-pwshexe:
function prompt {
  $loc = $executionContext.SessionState.Path.CurrentLocation;

  $out = "PS $loc$('>' * ($nestedPromptLevel + 1)) ";
  if ($loc.Provider.Name -eq "FileSystem") {
    $out += "$([char]27)]9;9;`"$($loc.ProviderPath)`"$([char]27)\"
  }
  return $out
}
  1. Type a single quote ' or a dot .

Expected behavior

PS> '

Actual behavior

PS>\'

Error details

No response

Environment data

PS /home/yen/tmp/PowerShell> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0-preview.3
PSEdition                      Core
GitCommitId                    7.4.0-preview.3
OS                             Linux 6.3.8-arch1-1 PowerShell/PowerShell#1 SMP PREEMPT_DYNAMIC Wed, 14 Jun 2023 20:10:31 +0000
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

I can also reproduce the issue with PowerShell 7.3.4 on Windows 10 22H2.

Visuals

圖片

This is captured with Konsole 23.04.2 and tmux 3.3a. I can reproduce the same issue with PowerShell 7.3.4 on Windows 10 22H2.

@yan12125
Copy link
Author

The issue is gone after Remove-module PSReadline. Probably an issue with PSReadline?

Both PSReadline 2.2.6 and 2.3.1-beta1 have this issue.

@237dmitry
Copy link

I had the exactly problem. In my case OSC 9;9 output the last symbol of prompt string, m in prompt below. I do not know why and how but I fixed this by exporting [int] class (no matter what class).

Set-PSReadlineOption -ContinuationPrompt $("`u{20}" * 5)
[console]::WriteLine("`e]12;#0087ff`a")
 
function Global:Prompt
{
    $null = [int]::new()          # No artefacts. I do not know why.
    $prompt = "`e[38;5;31m `u{ff05} `e]9;9;${PWD}`e\`e[0m"
    return $prompt
}

@yan12125
Copy link
Author

You may want this instead of hacking around $null:

$normalPrompt = "`e[38;5;31m `u{ff05} `e[0m"
$errorPrompt = "`e[91m `u{ff05} `e[0m"
Set-PSReadlineOption -PromptText $normalPrompt, $errorPrompt

From a comment in MicrosoftDocs/terminal#597 (comment),

IMPORTANT: Make sure there's a printable charater last in the prompt.
Otherwise, PSReadline is gonna use the terminating \ here and colorize
that if it detects a syntax error

-PromptText explicitly specifies prompts to use for syntax error indication and avoids fragile auto detection.

I have no idea why hacking around $null works, though.

@yan12125
Copy link
Author

yan12125 commented Jun 18, 2023

Apparently PSReadLine simply cuts a substring since the last non-whitespace character in the prompt: https://github.com/PowerShell/PSReadLine/blob/v2.2.6/PSReadLine/ReadLine.cs#L819. It will be great if OSC commands are parsed and ignored, but that may be far from trivial due to the need of a parser of various escape sequences.

If I were a PowerShell developer, I might close this as WONTFIX :)

GitHub
A bash inspired readline implementation for PowerShell - PSReadLine/PSReadLine/ReadLine.cs at v2.2.6 · PowerShell/PSReadLine

@237dmitry
Copy link

237dmitry commented Jun 18, 2023

You may want this

No, I do not want. I showed this example to present a workaround that is inexplicable to me.

@SteveL-MSFT SteveL-MSFT transferred this issue from PowerShell/PowerShell Jun 19, 2023
@ghost ghost added the Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. label Jun 19, 2023
@daxian-dbw
Copy link
Member

daxian-dbw commented Jul 24, 2023

I cannot reproduce the issue with the repro steps described in the issue. The GIF below is from WSL Ubuntu 18.04 with PowerShell 7.4-preview.3 + PSReadLine 2.3.1-beta1. I tried on Windows 11 with both conhost and Windows Terminal, and cannot reproduce too.

repro-ws-2022

@yan12125 and @237dmitry Can either of you please provide a repro that help me reproduce the issue locally?

@daxian-dbw daxian-dbw added the Needs-Repro There is no sufficient information on repro steps. label Jul 24, 2023
@yan12125
Copy link
Author

@daxian-dbw For me, the issue happens only when the prompt function is defined in $PROFILE. It does not happend when the function is defined from the command line.

Also, it seems that error prompt is not enabled for you (the last non-whitespace character should become red when there is a syntax error). Could you check if PromptText or ErrorColor in Get-PSReadLineOption results is overridden or not?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Attention 👋 and removed Needs-Repro There is no sufficient information on repro steps. labels Jul 25, 2023
@daxian-dbw
Copy link
Member

I can reproduce the issue after moving the prompt function to $PROFILE. We will investigate it.

@daxian-dbw daxian-dbw added Issue-Bug It either shouldn't be doing this or needs an investigation. and removed Needs-Triage 🔍 It's a new issue that core contributor team needs to triage. Needs-Attention 👋 labels Jul 25, 2023
@yan12125
Copy link
Author

Thank you for confirmation. As the reproducer is from official Microsoft docs, how do you think about updating docs to work-around this issue first? MicrosoftDocs/terminal#689

@daxian-dbw
Copy link
Member

Unfortunately, we don't own that doc.

nguyen-dows pushed a commit to MicrosoftDocs/terminal that referenced this issue Aug 31, 2023
PSReadLine cuts a substring since the last non-whitespace character in
the prompt as the error prompt [1], which is displayed when there are
syntax errors. As a result, when the prompt ends in `$([char]27)\`, as
in the previous example, an unexpected backslash may appear.

See: PowerShell/PSReadLine#3719

[1]
https://github.com/PowerShell/PSReadLine/blob/v2.2.6/PSReadLine/ReadLine.cs#L819.

---

Closes #597
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation.
Projects
None yet
Development

No branches or pull requests

3 participants