garabik / grc

generic colouriser
http://kassiopeia.juls.savba.sk/~garabik/software/grc.html
Other
1.9k stars 162 forks source link

GNU make: grc causes shell builtin `read -p "prompt text: "` to not print to terminal in Makefile #82

Open trinitronx opened 6 years ago

trinitronx commented 6 years ago

When using the read -r -p "some prompt here: " bash builtin and having grc loaded into the shell, it appears that the prompt is never displayed. This can cause confusion with things that spawn sub-shells such as make, and probably some bash scripts (maybe other tools too).

Here is a Makefile to reproduce the issue

To reproduce:

  1. Install & load grc into ~/.bashrc
    • For example: brew install grc , then add the following to ~/.bashrc: source "$(brew --prefix)/etc/grc.bashrc"
  2. Spawn a bash shell & cd to the directory with this Makefile
  3. Run make test
  4. Watch as you are not prompted by read (as the Makefile is written to do)
  5. Press enter and see that read was actually expecting input, which you were unaware of
  6. Try again, this time typing something and hitting enter... now it shows you it did read some input and set a variable.

Then, try commenting out the source line which loads grc into the shell. Retry the same make test and see the prompt is shown.

I'm stumped as to why loading grc is affecting shell builtins in this way... the entire contents of the file under $(brew --prefix)/etc/grc.bashrc only has alias definitions:

GRC=`which grc`
if [ "$TERM" != dumb ] && [ -n "$GRC" ]
then
    alias colourify="$GRC -es --colour=auto"
    alias configure='colourify ./configure'
    alias diff='colourify diff'
    alias make='colourify make'
    alias gcc='colourify gcc'
    alias g++='colourify g++'
    alias as='colourify as'
    alias gas='colourify gas'
    alias ld='colourify ld'
    alias netstat='colourify netstat'
    alias ping='colourify ping'
    alias traceroute='colourify /usr/sbin/traceroute'
    alias head='colourify head'
    alias tail='colourify tail'
    alias dig='colourify dig'
    alias mount='colourify mount'
    alias ps='colourify ps'
    alias mtr='colourify mtr'
    alias df='colourify df'
fi

I've tested this with:

bash --version = GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin15.6.0) make --version = GNU Make 3.81 grc --version = Generic Colouriser 1.11.1 (from Homebrew)

And it also appears to affect the default shell /bin/sh on mac as well:

/bin/sh
sh-3.2$ source "`brew --prefix`/etc/grc.bashrc"
sh-3.2$ make test

You must specify SOME_ENV_VAR or set env variable SOME_ENV_VAR !
Enter SOME_ENV_VAR (eg 'bar') (set environment variable SOME_ENV_VAR to skip this prompt): make: *** [test] Error 1

/bin/sh --version = GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16) make --version = GNU Make 3.81 grc --version = Generic Colouriser 1.11.1 (from Homebrew)

trinitronx commented 6 years ago

Ah, I see why it's being used in this case... it's this: alias make='colourify make'

trinitronx commented 6 years ago

FYI: I have validated that this behavior has something to do with the -es flags defined in the alias colourify="$GRC -es --colour=auto"

Editing the grc.bashrc file and taking these out for the make alias seems to fix the output from read:

vi $(brew --prefix)/etc/grc.bashrc:

Edits made:

GRC="$(which grc)"
if [ "$TERM" != dumb ] && [ -n "$GRC" ]; then
    alias colourify="$GRC -es --colour=auto"
# [...SNIP...]
# BEGIN EDIT
#    alias make='colourify make'
    alias colorspecial='$GRC --colour=auto'
    alias make='colorspecial make'
# END EDIT
# [...SNIP...]
trinitronx commented 2 months ago

I have a hunch that this was related to pipe buffering, and could possibly be mitigated by stdbuf. Although it's untested on macOS, and I don't have the same system and particular Makefile setup from years ago to reproduce.