Sid
Sid

Reputation: 1719

Cannot abort bash script started from a git alias in VSCode using Git Bash

Assume I have a git alias like this:

[alias]
    abortalias = "! f() { \
        read -p '> Press ctrl+c to cancel'; \
        echo '> This should never be seen'; \
    }; \
    f"

And run git abortalias, then press ctrl+c when prompted. I'm expecting the script to cancel, instead what happens is that I pop out to the terminal, and my next input will trigger this error:

$ environment: line 0: read: read error: 0: Input/output error
> This should never be seen

This is also reproducible by adding a sleep 5 instead of the read -p command and exiting during the sleep. The script will still resume.

Edit: Upon further testing, this only seems to happen inside a MINGW64 shell. Running from Powershell or CMD works just fine. Has to be some Windows quirk then I guess.

Edit 2: Specifically, it's git bash (MINGW64), ran through Visual Studio Code that's causing this issue. Running it in git bash (MINGW64) alone works, and running it in VSCode with another terminal works.

I've opened an issue in the VSCode repo meanwhile: https://github.com/microsoft/vscode/issues/149155

Upvotes: 1

Views: 137

Answers (1)

j6t
j6t

Reputation: 13507

Ctrl-C on Windows is a fake. It kills only the process that is started from the command line, but does not propagate through to child processes. Hence, the shell and other child processes that are spawned by git abortalias keep doing their thing without being interrupted.

Your best bet is to write defensive shell scripts:

[alias]
    abortalias = "! f() { \
        read -p '> Press ctrl+c to cancel' && \
        echo '> This should never be seen'; \
    }; \
    f"

Note the && after the read command.

Upvotes: 1

Related Questions