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

process.stdout.isTTY is undefined when git-bash (on Windows) invokes node via a shell script #39673

Closed
daiplusplus opened this issue Aug 5, 2021 · 6 comments

Comments

@daiplusplus
Copy link

daiplusplus commented Aug 5, 2021

Version

14.17.4 (also reproduces on 16.4)

Platform

Microsoft Windows NT 10.0.19042.0 x64

Subsystem

No response

What steps will reproduce the bug?

This was discovered while investigating this issue: angular/angular-cli#21512

  1. Save this script (ttytest.js) somewhere:

    process.stdout.write("typeof stdout.isTTY === '" + typeof process.stdout.isTTY + "'\n");
    if( typeof process.stdout.isTTY === 'boolean' ) {
        process.stdout.write( "process.stdout.isTTY == " + process.stdout.isTTY ? 'true' : 'false' );
    }
    else if( typeof process.stdout.isTTY === 'undefined' ) {
        process.stdout.write( "process.stdout.isTTY is undefined " );
    }
    else if( typeof process.stdout.isTTY === 'object' && process.stdout.isTTY === null ) {
        process.stdout.write( "process.stdout.isTTY is null" );
    }
    else {
        process.stdout.write( "process.stdout.isTTY.toString() == \"" + process.stdout.isTTY.toString() + "\"" );
    }
  2. Open a Windows cmd prompt (with node in the PATH) and run the script:

    C:\>node "C:\Users\Me\Desktop\2021\2021-08\ttytest.js"
    typeof stdout.isTTY === 'boolean'
    process.stdout.isTTY == true
    

    So far, so good.

  3. Open a git-bash prompt and run the script:

    me@cube MINGW64 /c/users/David/Desktop
    $ node /c/users/David/Desktop/2021/2021-08/ttytest.js
    typeof stdout.isTTY === 'boolean'
    process.stdout.isTTY == true
    

    Also good.

  4. Run the script via a cmd batch file:

    Save this to indirect-cmd.cmd

    node ttytest.js
    

    This also works fine:

    C:\Users\me\Desktop\2021\2021-08>indirect-cmd.cmd
    
    C:\Users\me\Desktop\2021\2021-08>node ttytest.js
    typeof stdout.isTTY === 'boolean'
    process.stdout.isTTY == true
    C:\Users\me\Desktop\2021\2021-08>
    
  5. However, do the same for bash and it fails

    Save this to indirect-bash.sh

    node ttytest.js

    And run it:

    me@cube MINGW64 /c/users/me/Desktop/2021/2021-08
    $ /c/users/me/Desktop/2021/2021-08/indirect.sh
    typeof stdout.isTTY === 'undefined'
    process.stdout.isTTY is undefined
    
  6. It is also undefined when node is invoked using exec:

    Save this to indirect-exec-bash.sh

    exec node ttytest.js

    And run it:

    me@cube MINGW64 /c/users/me/Desktop/2021/2021-08
    $ ./indirect-bash.sh
    typeof stdout.isTTY === 'undefined'
    process.stdout.isTTY is undefined
    

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

I expect isTTY to never be undefined.

What do you see instead?

No response

Additional information

No response

@daiplusplus daiplusplus changed the title process.stdout.isTTY is undefined when git-bash (on Windows) is invoked via a shell script process.stdout.isTTY is undefined when git-bash (on Windows) invokes node via a shell script Aug 5, 2021
@devsnek
Copy link
Member

devsnek commented Aug 5, 2021

You'll need to run node using winpty.

@daiplusplus
Copy link
Author

daiplusplus commented Aug 5, 2021

@devsnek Right - I know that works-around the issue, but I feel that isTTY being undefined is a bug, surely?

The documentation for writeStream.isTTY says:

A boolean that is always true.

...yet I've demonstrated that it can sometimes be undefined and not true.

So either:

  1. The documentation is incorrect and should be updated to state that it can return undefined and under what circumstances it will be undefined.
  2. There's a bug in isTTY that causes it to incorrectly return undefined when it should return false - the fact winpty was - or was not used - is irrelevant to the current incongruence.

@devsnek
Copy link
Member

devsnek commented Aug 5, 2021

If stdout is not a tty, it won't be an instance of tty.WriteStream, it will just be a normal socket stream, which has no isTTY property. The documentation also recommends using Boolean(process.stdout.isTTY) because of this. https://nodejs.org/api/process.html#process_a_note_on_process_i_o

@daiplusplus
Copy link
Author

My mistake - I realise that the tty module adds isTTY to WriteStream as a bool literal rather than extending WriteStream with a proper getter/setter property for isTTY.

I was thinking that isTTY was always a defined member of process.stdout.

@daiplusplus
Copy link
Author

daiplusplus commented Aug 5, 2021

For reference, and for anyone else encountering the same issue, it's the same as these issues/problems:

OmgImAlexis referenced this issue Mar 9, 2022
Adds documentation for the following `WriteStream` instance methods:

- `WriteStream.clearLine()`
- `WriteStream.clearScreenDown()`
- `WriteStream.cursorTo()`
- `WriteStream.getWindowSize()`
- `WriteStream.moveCursor()`

PR-URL: #22893
Fixes: #9853
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
@davidmurdoch
Copy link

Related issue with the Definitely Typed TypeScript types definitions: DefinitelyTyped/DefinitelyTyped#68299

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

No branches or pull requests

3 participants