Skip to content

Powershell Prompt Double Execution #468

Closed

Description

@lzybkr

From Paul (@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

Activity

  1. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    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.

  2. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    From Joel Bennett (@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 ;-)

  3. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    From myyc on August 20, 2016 10:50

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

  4. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    From Paul (@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 :-)

  5. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    From myyc on August 20, 2016 11:26

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

  6. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    From theaquamarine on August 22, 2016 22:45

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

  7. lzybkr commented on Nov 9, 2016

    @lzybkr
    ContributorAuthor

    From Joel Bennett (@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

  8. rspaulino commented on Dec 29, 2016

    @rspaulino

    Jason Shirk (@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

  9. rkeithhill commented on Jan 27, 2017

    @rkeithhill
    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

  10. 21 remaining items

  11. drvink commented on Oct 20, 2017

    @drvink

    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.

  12. lzybkr commented on Oct 20, 2017

    @lzybkr
    ContributorAuthor

    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.

  13. drvink commented on Oct 20, 2017

    @drvink

    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).

  14. lzybkr commented on Oct 20, 2017

    @lzybkr
    ContributorAuthor

    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.

  15. drvink commented on Oct 20, 2017

    @drvink

    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.

  16. lzybkr commented on Oct 20, 2017

    @lzybkr
    ContributorAuthor

    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.

  17. lzybkr commented on Nov 2, 2017

    @lzybkr
    ContributorAuthor

    Mark Laws (@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.

  18. drvink commented on Nov 3, 2017

    @drvink

    Jason Shirk (@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.

  19. lzybkr commented on Nov 4, 2017

    @lzybkr
    ContributorAuthor

    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.

  20. pcgeek86 commented on Nov 9, 2017

    @pcgeek86

    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

  21. lzybkr commented on Nov 10, 2017

    @lzybkr
    ContributorAuthor

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

  22. drvink commented on Nov 16, 2017

    @drvink

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

  23. SteveL-MSFT commented on Nov 16, 2017

    @SteveL-MSFT
    Member

    Trevor Sullivan (@pcgeek86) the backslashes is an issue with compress-archive which I opened here https://github.com/PowerShell/Microsoft.PowerShell.Archive/issues/48

  24. pcgeek86 commented on Nov 17, 2017

    @pcgeek86

    Steve Lee (@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

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions