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

Powershell Prompt Double Execution #468

Closed
lzybkr opened this issue Nov 9, 2016 · 43 comments
Closed

Powershell Prompt Double Execution #468

lzybkr opened this issue Nov 9, 2016 · 43 comments
Assignees

Comments

@lzybkr
Copy link
Member

lzybkr commented Nov 9, 2016

From @whatevergeek on August 18, 2016 18:44

Version: powershell_6.0.0-alpha.9-1ubuntu1.14.04.1_amd64.deb
Platform: Ubuntu 14.04

Steps to reproduce

  1. Modify $profile file and add the following function inside
    function prompt
    {
    Write-Host ("This>")
    return " "
    }
  2. Exit and re-enter powershell
  3. Check the commandline prompt

Expected behavior

It should only print "This>" once.

Actual behavior

It prints "This>" twice.

Environment data

> $PSVersionTable
Name                           Value                                                                 
----                           -----                                                                 
PSVersion                      6.0.0-alpha                                                           
PSEdition                      Core                                                                  
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                               
BuildVersion                   3.0.0.0                                                               
GitCommitId                    v6.0.0-alpha.9                                                        
CLRVersion                                                                                           
WSManStackVersion              3.0                                                                   
PSRemotingProtocolVersion      2.3                                                                   
SerializationVersion           1.1.0.1       

Copied from original issue: PowerShell/PowerShell#1897

@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @lukemgriffith on August 18, 2016 23:11

I'm also seeing the same on MacOS 10.11.6


PS /Users/luke/Downloads> $IsOSX                                                                                                                                                   
True
PS /Users/luke/Downloads> $PSVersionTable                                                                                                                                          

Name                           Value                                                                                                                                              
----                           -----                                                                                                                                              
PSVersion                      6.0.0-alpha                                                                                                                                        
PSEdition                      Core                                                                                                                                               
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                            
BuildVersion                   3.0.0.0                                                                                                                                            
GitCommitId                    v6.0.0-alpha.9                                                                                                                                     
CLRVersion                                                                                                                                                                        
WSManStackVersion              3.0                                                                                                                                                
PSRemotingProtocolVersion      2.3                                                                                                                                                
SerializationVersion           1.1.0.1  


$VerbosePreference = "Continue"

function prompt {

  Write-Verbose "Test"

}


I see the verbose "Test" twice.

@lzybkr lzybkr self-assigned this Nov 9, 2016
@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @Jaykul on August 18, 2016 23:38

Ditto for Ubuntu 16.04

I had to stop using Write-Host ... now using escape sequences for color ;-)

@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @myyc on August 20, 2016 10:50

@Jaykul can you share details? it seems ANSI escape sequences aren't supported out of the box

@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @whatevergeek on August 20, 2016 11:21

@myyc which details you need?
my hunch is that whatever posted by write-host
gets posted twice

i'm still in the process of getting the source built on my machine... will try to help also once i get the setup done :-)

@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @myyc on August 20, 2016 11:26

@whatevergeek details on how to use escape sequences instead of write-host ...

@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @theaquamarine on August 22, 2016 22:45

@myyc there's a bit of an example here dahlbyk/posh-git#282 (comment)

@lzybkr
Copy link
Member Author

lzybkr commented Nov 9, 2016

From @Jaykul on August 23, 2016 0:5

Oh, I'm sorry, I didn't mean to leave you hanging. Here's what I have so far, it works on Windows 10 and Linux, even in VS Code ... but obviously not anywhere that ANSI escape sequences don't work (e.g. ISE): http://poshcode.org/6485

@rspaulino
Copy link

@lzybkr
I try to follow your demo to get color on my prompt and when I login it work I see my color prompt but the moment I type something the escape character break.

any ideas?


function prompt {
return "$([char]0x1b)[91m >_ $([char]0x1b)[0m"
}

PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

_


PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

[91clear

@rkeithhill
Copy link
Contributor

You probably already know this but avoiding Write-Host doesn't prevent double execution of the prompt function. It just makes it a bit less obvious:
image

In fact, what I notice with the prompt function is that the first two or three characters from the first line of my prompt get redisplayed on the second line as soon as I type any character:
image

@eamodio
Copy link

eamodio commented Jan 27, 2017

Yeah, I see the same thing -- I originally reported that here PowerShell/PowerShell#2770 (comment)

@lzybkr
Copy link
Member Author

lzybkr commented Jan 27, 2017

The double invocation shouldn't be there (I pointed out the problem before it got merged originally) - but it was put there to support making the prompt red on error. When that goes, we will need a new way to indicate errors.

@rkeithhill
Copy link
Contributor

I wonder if you could change the color of the cursor to the error colors?

@rkeithhill
Copy link
Contributor

Could we get an option to disable the error indicator until this issue is fixed? Or if this isn't an option you want long term, perhaps an env var we can use to "backdoor" disable it? Say PSREADLINE_DISABLE_ERROR_INDICATOR or some such env var.

@branneman
Copy link

branneman commented May 19, 2017

Any idea when this will be fixed? The issue still persists in:

PS> $IsOSX
True

PS> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.18
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

@PowerSchill
Copy link

Still in beta-1

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
BuildVersion                   3.0.0.0
CLRVersion
GitCommitId                    v6.0.0-beta.1
OS                             Darwin 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

@daxian-dbw
Copy link
Member

I try to follow your demo to get color on my prompt and when I login it work I see my color prompt but the moment I type something the escape character break.

A fix was made in the powershell repository to address this, though not fully. See PowerShell/PowerShell#3867

@lzybkr
Copy link
Member Author

lzybkr commented Sep 28, 2017

This branch has a fix (and supports escape sequences) but isn't quite ready - I think just one big thing left for Linux - custom key bindings. It's very usable on Windows right now.

@drvink
Copy link

drvink commented Oct 5, 2017

@lzybkr Great! Should this branch "just work" if we compile the module and make sure it will be loaded first in $env:PSModulePath?

@lzybkr
Copy link
Member Author

lzybkr commented Oct 5, 2017

@drvink - Not quite. In Windows PowerShell, yes, but you probably don't care about Windows PowerShell.

PowerShell Core lists PSReadline in it's trust platform assemblies, so you can't actually update it via the module path. There are 2 options:

  1. Replace the module (I think it's just the psd1 and dll) where PowerShell Core is installed
  2. Drop the current source into a build of PowerShell and build that

I've done both, 1. on Windows and 2. on Linux. I've done minimal testing in PowerShell Core, definitely more on Windows, and I think it mostly works on Windows.

On Linux you won't get custom key binding support yet. There is also one serious bug that crashes PowerShell (as in, it exits). I believe it's a .Net bug but haven't thoroughly investigated. The bug happens when you use Shift+arrows to select. It also depends on something else that I haven't tracked down, but if you repeatedly hit Shift+arrow, you might not hit it, but if you don't release the arrow key, you will probably hit it.

@gwojan
Copy link

gwojan commented Oct 5, 2017

@lzybkr I just dropped the updated module into my profile path:

PS C:\Users\wojang> (Get-Module PSReadLine).ModuleBase
C:\Users\wojang\Documents\PowerShell\Modules\PSReadLine\2.0
PS C:\Users\wojang> Get-Module psreadline -ListAvailable


    Directory: C:\Users\wojang\Documents\PowerShell\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.0        PSReadLine                          {Get-PSReadlineKeyHandler, Set-PSReadlineKeyHandler, Remov...


    Directory: C:\program files\powershell\6.0.0-beta.7\Modules


ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.2        PSReadLine                          {Get-PSReadlineKeyHandler,

@lzybkr
Copy link
Member Author

lzybkr commented Oct 5, 2017

But did it load it? You can check with [Microsoft.PowerShell.PSConsoleReadLine].Assembly.Location

@gwojan
Copy link

gwojan commented Oct 5, 2017

@lzybkr Well... Crap... No... ☹️

I guess that explains some differences I've noticed in PSReadline handling between PowerShell 5.1 and PowerShell 6... < le sigh />

No I just have to find that list again...

@rkeithhill
Copy link
Contributor

I managed to get PSReadLine to compile in my WSL shell using the .NET Core 2.0 SDK. I had to generate a new csproj file and ifdef out some code in PSReadLine.cs that was accessing some Runspace debugger properties that don't exist in PowerShellStandard.Library v3. But it compiles and sort of runs:
image
But as you can see, the chars appear not where the cursor is and with extra spaces. Haven't had a chance to debug this yet but was just wondering if this is a futile exercise or not?

It would be nice to be able to build PSReadLine on non-Windows systems.

FWIW here's the CSPROJ file I'm using:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.CSharp" Version="4.4.0" />
    <PackageReference Include="PowerShellStandard.Library" Version="3.0.0-preview-01" />
  </ItemGroup>
  <ItemGroup>
    <Compile Update="PSReadLineResources.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>PSReadLineResources.resx</DependentUpon>
    </Compile>
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Update="PSReadLine.Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>PSReadLineResources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>
</Project>

@lzybkr
Copy link
Member Author

lzybkr commented Oct 15, 2017

I haven't tried in WSL yet, but @SteveL-MSFT said it worked for him. Debugging would be great - the rendering code no longer uses Win32 console apis - it's pure .Net, so it should work anywhere.

I have copied the sources into the PowerShell repo and built from there on Linux.

It would also be nice to get the tests running on Linux.

I'm avoiding the move to netstandard2.0 until the PowerShell team figures out how to deploy the type forwarding dlls because no module should be doing that.

@rkeithhill
Copy link
Contributor

rkeithhill commented Oct 16, 2017

The PSReadLine assembly didn't have the resources defined in the right namespace. I added the RootNamespace directive to the project file and that cleared one of the issues I was seeing with rendering. No extra spaces now and the cursor tracks typing.

I did manage to get the unit tests to compile but only about 5 out of 150 pass. The vast majority fail because of a null ref exception on rs.Open() in the UnitTest static constructor:

            var iss = InitialSessionState.CreateDefault2();
            var rs = RunspaceFactory.CreateRunspace(iss);
            rs.Open();

In fact, iss comes back as null as well. I'm not sure exactly what is going on here. This is the error I see:

Failed   UnitTestPSReadLine.UnitTest.ViTestCharDelete
Error Message:
 Unable to create instance of class UnitTestPSReadLine.UnitTest. Error: System.NullReferenceException: Object reference not set to an instance of an object..
Stack Trace:
    at UnitTestPSReadLine.UnitTest..cctor() in C:\Users\Keith\GitHub\lzybkr\PSReadLine\UnitTestPSReadLine\UnitTestReadLine.cs:line 365

Oh yeah, I had to comment out a fair bit of test code that was testing the clipboard. You can see the changes I've made so far in my repo - rkeithhill@e3beb6f

BTW when I debug this from VSCode, I see a lot of DLLs loaded but I don't see SMA.dll.

@lzybkr
Copy link
Member Author

lzybkr commented Oct 17, 2017

I believe this is fixed in #561.

I'll publish a pre-release version to the PowerShell Gallery as soon as they support pre-releases.

If you'd like to try a fix before that, you can build the lastest yourself from the master branch, or
grab a build from AppVeyor like this one.

Note the version number is now 2.0, so be sure to copy the contents to \PSReadLine\2.0.

@drvink
Copy link

drvink commented Oct 20, 2017

With some small changes I was able to drop the PSReadLine master branch into the PowerShell git repo (using tag v6.0.0-beta.8) and build it on Linux. It's mostly working! Looks fine in a bare xterm, but try running powershell under screen--you should get some junk at the prompt as soon as you start typing.

@lzybkr
Copy link
Member Author

lzybkr commented Oct 20, 2017

The binary built from this repo works in PowerShell Core all platforms too.

Artifacts in screen are new? I wonder if I'm using an escape sequence that isn't supported under screen.

@drvink
Copy link

drvink commented Oct 20, 2017

The junk I'm seeing is [37m[40m[0m, which are parts of SGR sequences: set foreground color white, set background black, set normal. If they're showing up for you (i.e. if you can reproduce), try seeing what's actually getting written to the terminal (with strace or similar).

@lzybkr
Copy link
Member Author

lzybkr commented Oct 20, 2017

I can reproduce, and I'm strace noob, but I can't see anything in the output that looks like what I'm sending, either with v1.2 (which works in screen) or in v2.0 which doesn't.

@drvink
Copy link

drvink commented Oct 20, 2017

Sorry, brainfart--you would want to strace the screen daemon process, so that you can see what powershell is writing to it. Here's what strace -p $PIDGOESHERE -e read=all -e write=all shows after I press a key to make the junk appear at the prompt (this asks to produce a hexdump of the full contents of reads and writes to any of that PID's file descriptors):

arch-vm[1022]:~% sudo strace -p 5249 -e read=all -e write=all
strace: Process 5249 attached
select(1024, [3 4 6 7 8 10], [], NULL, NULL) = 1 (in [10])
read(10, "a", 4096)                     = 1
 | 00000  61                                                a                |
select(1024, [3 4 6 7 8 10], [8], NULL, NULL) = 1 (out [8])
write(8, "a", 1)                        = 1
 | 00000  61                                                a                |
select(1024, [3 4 6 7 8 10], [], NULL, NULL) = 1 (in [8])
read(8, " ", 4096)                      = 1
 | 00000  20                                                                 |
ioctl(10, SNDCTL_TMR_START or TCSETS, {B38400 opost -isig -icanon -echo ...}) = 0
select(1024, [3 4 6 7 8 10], [], NULL, NULL) = 1 (in [8])
read(8, "\0\33[?25l", 4096)             = 7
 | 00000  00 1b 5b 3f 32 35 6c                              ..[?25l          |
select(1024, [3 4 6 7 8 10], [10], NULL, NULL) = 1 (out [10])
write(10, "\33[?25l", 6)                = 6
 | 00000  1b 5b 3f 32 35 6c                                 .[?25l           |
select(1024, [3 4 6 7 8 10], [], NULL, NULL) = 1 (in [8])
read(8, "\20", 4096)                    = 1
 | 00000  10                                                .                |
ioctl(10, TCXONC, TCOON)                = 0
ioctl(10, SNDCTL_TMR_START or TCSETS, {B38400 opost -isig -icanon -echo ...}) = 0
select(1024, [3 4 6 7 8 10], [], NULL, NULL) = 1 (in [8])
read(8, "\0\33[16;49H\33\0[\0009\0003\0m\0a\0\33\0[\0003\0007\0m\0\33"..., 4096) = 68
 | 00000  00 1b 5b 31 36 3b 34 39  48 1b 00 5b 00 39 00 33  ..[16;49H..[.9.3 |
 | 00010  00 6d 00 61 00 1b 00 5b  00 33 00 37 00 6d 00 1b  .m.a...[.3.7.m.. |
 | 00020  00 5b 00 34 00 30 00 6d  00 1b 00 5b 00 30 00 6d  .[.4.0.m...[.0.m |
 | 00030  00 1b 5b 31 36 3b 35 30  48 1b 5b 33 34 68 1b 5b  ..[16;50H.[34h.[ |
 | 00040  3f 32 35 68                                       ?25h             |
select(1024, [3 4 6 7 8 10], [10], NULL, NULL) = 1 (out [10])
write(10, "[93ma[37m[40m[0m\33[15D\33[?12l\33[?25"..., 33) = 33
 | 00000  5b 39 33 6d 61 5b 33 37  6d 5b 34 30 6d 5b 30 6d  [93ma[37m[40m[0m |
 | 00010  1b 5b 31 35 44 1b 5b 3f  31 32 6c 1b 5b 3f 32 35  .[15D.[?12l.[?25 |
 | 00020  68                                                h                |

The last read is what powershell is writing to screen's pty, and the write is what the screen daemon is writing to the attached client (which shows up on our terminal). Looks like some of the sequences are being output with each character as a 16-bit/.NET-style char rather than what was actually intended.

@lzybkr
Copy link
Member Author

lzybkr commented Oct 20, 2017

Thanks, that's super helpful.

I had to set Console.OutputEncoding = Encoding.Unicode for CJK characters, and that definitely fixed an issue on Windows and it seems like it also fixed CJK on Linux, see #542.

So the quick fix is to just remove where I do that, but I'm not sure that's the right fix.

@lzybkr
Copy link
Member Author

lzybkr commented Nov 2, 2017

@drvink - I just pushed what should fix the screen problem - I should have used UTF8 instead of Unicode for the output encoding - the CJK issue still seems fixed, so hopefully screen will be fine too.

@drvink
Copy link

drvink commented Nov 3, 2017

@lzybkr Thanks! I don't have time to test it at the moment, but that sounds like the right fix to me as well. I'll try to comment when I get a chance to rebuild with the new changes, but if you aren't getting the incorrectly-emitted escape sequences when you strace, it's most likely fixed now.

@lzybkr
Copy link
Member Author

lzybkr commented Nov 4, 2017

I'll have an official prerelease build out next week if you want to verify this is fixed, otherwise you can grab a build out of appveyor here.

@lzybkr lzybkr closed this as completed Nov 4, 2017
@pcgeek86
Copy link

pcgeek86 commented Nov 9, 2017

When I extract the ZIP artifact, it has backslashes in the file names, instead of forward slashes, which breaks the module file paths.

screen shot 2017-11-09 at 2 40 51 pm

@lzybkr
Copy link
Member Author

lzybkr commented Nov 10, 2017

Compress-Archive is creating the archive - but I assume that will be a non-issue when installing from the PowerShell Gallery.

@drvink
Copy link

drvink commented Nov 16, 2017

@lzybkr Just wanted to report that things work fine under screen now. Thanks!

@SteveL-MSFT
Copy link
Member

@pcgeek86 the backslashes is an issue with compress-archive which I opened here PowerShell/Microsoft.PowerShell.Archive#48

@pcgeek86
Copy link

@SteveL-MSFT Cool, thank you for calling that out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests