msys2.github.io icon indicating copy to clipboard operation
msys2.github.io copied to clipboard

mintty.exe is a strange terminal

Open gabyx opened this issue 3 years ago • 5 comments

It would be really nice if somebody could look at the following issue: https://github.com/golang/go/issues/46681

It demonstrates the fact that git-bash.exe and also mingw.exe really behave strange, when we want to the get the controlling terminal file input CONIN$ on windows.

This is basically done in GO with:

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"syscall"

	"golang.org/x/term"
)

func GetCttyIn() (io.Reader, error) {
	in, err := syscall.Open("CONIN$", syscall.O_RDWR, 0)
	if err != nil {
		return nil, err
	}

	fmt.Println("$CONIN Isterm: ", isatty.IsTerminal(uintptr(in)))

	return os.NewFile(uintptr(in), "/dev/tty"), nil
}

func main() {

	fmt.Println("In: ", term.IsTerminal(int(os.Stdin.Fd())))
	fmt.Println("Out: ", term.IsTerminal(int(os.Stdout.Fd())))

	in, err := GetCttyIn()
	if err != nil {
		panic(err)
	}
	scanner := bufio.NewScanner(in)

	success := scanner.Scan()
	if !success {
		panic(err)
	}
	fmt.Println("Res:", scanner.Text())
}

Running the above with echo "asd" | go run main.go in vs code integrate terminal with bash.exe (MSYS2) **works **(suprisingly) but

running it in the git-bash.exe or msyswin64.exe (mintty.exe) does not work:

VS Code: (bash.exe)

In:  false
Out:  true
$CONIN Isterm:  true
asd
Res: asd

Correct!

Straight mintty.exe

In:  false      
Out:  false  // <<<< Thats wrong!
$CONIN Isterm:  true
                 //  <<<<--- STUCK at READ here!

gabyx avatar Jun 11 '21 17:06 gabyx

Technically speaking, there is something common between git-for-windows and msys2.

Biswa96 avatar Jun 11 '21 17:06 Biswa96

winpty? conpty? Basically mintty uses "cygwin" ptys, not native Windows consoles, so trying to interact with the attached native Windows console from within it is usually doomed to failure. winpty is a program that tries to work around this, and conpty is a new thing that should help, but is still buggy so not enabled by default here (or in git bash).

jeremyd2019 avatar Jun 11 '21 17:06 jeremyd2019

Personally, I don't use mintty because of this. I either run within the native console host, or now in Windows Terminal.

jeremyd2019 avatar Jun 11 '21 17:06 jeremyd2019

@gabyx Can you provide equivalent code in C?

Biswa96 avatar Jun 13 '21 12:06 Biswa96

I suspect it is the same problem that I have which causes python to report that stdout is not a tty. Add ConPTY=on to your minttyrc file.

julie777 avatar Feb 26 '22 21:02 julie777