rcaloras / bash-preexec

⚡ preexec and precmd functions for Bash just like Zsh.
MIT License
862 stars 94 forks source link

Not working on Windows using Bash in MSYS2 #149

Open Dominiquini opened 9 months ago

Dominiquini commented 9 months ago

I download and added to my .bashrc this script, but it is not working! I also tested on Ubuntu (WSL2) and it works fine on Linux! But on Windows (MSYS2), only the 'precmd' method works! 'preexec' is not triggered by any command!

Is there any limitation about working on Windows? If not,. Is there anything I can do to help further resolve this bug?

Thanks.

dimo414 commented 9 months ago

Can you share more diagnostic details, such as the bash version you're using? Using set -x to print what's happening around the time of precmd could also be helpful.

Dominiquini commented 9 months ago

I typed 2 commands:

$ preexec_hello_world() { echo "You just entered $1"; } $ pwd

The output with set -x:


Rafael Dominiquini@DESKTOP:~ $ _preexec_helloworld() { echo "You just entered $1"; }

+++ __bp_preexec_invoke_exec -x
+++ __bp_last_argument_prev_command=-x
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z on ]]
+++ [[ 0 -eq 0 ]]
+++ __bp_preexec_interactive_mode=
+++ __bp_in_prompt_command __bp_precmd_invoke_cmd
+++ local prompt_command_array 'IFS=
;'
+++ read -rd '' -a prompt_command_array
+++ local trimmed_arg
+++ __bp_trim_whitespace trimmed_arg __bp_precmd_invoke_cmd
+++ local var=trimmed_arg text=__bp_precmd_invoke_cmd
+++ text=__bp_precmd_invoke_cmd
+++ text=__bp_precmd_invoke_cmd
+++ printf -v trimmed_arg %s __bp_precmd_invoke_cmd
+++ local command trimmed_command
+++ for command in "${prompt_command_array[@]:-}"
+++ __bp_trim_whitespace trimmed_command __bp_precmd_invoke_cmd
+++ local var=trimmed_command text=__bp_precmd_invoke_cmd
+++ text=__bp_precmd_invoke_cmd
+++ text=__bp_precmd_invoke_cmd
+++ printf -v trimmed_command %s __bp_precmd_invoke_cmd
+++ [[ __bp_precmd_invoke_cmd == \_\_\b\p\_\p\r\e\c\m\d\_\i\n\v\o\k\e\_\c\m\d ]]
+++ return 0
+++ __bp_preexec_interactive_mode=
+++ return
++ __bp_precmd_invoke_cmd
++ __bp_last_ret_value=0
++ BP_PIPESTATUS=("${PIPESTATUS[@]}")
++ ((  __bp_inside_precmd > 0  ))
++ local __bp_inside_precmd=1
++ local precmd_function
++ for precmd_function in "${precmd_functions[@]}"
++ type -t precmd
++ __bp_set_ret_value 0
++ return 0
+++ __bp_preexec_invoke_exec __bp_precmd_invoke_cmd
+++ __bp_last_argument_prev_command=__bp_precmd_invoke_cmd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ gitstatus_prompt_update
++ GITSTATUS_PROMPT=
++ gitstatus_query
++ unset OPTIND
++ timeout=()
++ local opt dir= timeout no_diff=0
++ getopts d:c:t:p opt
++ ((  OPTIND == 0 + 1  ))
++ [[ -n 10224 ]]
++ local req_id=17140.18395.25160.27689
++ [[ -z '' ]]
++ [[ '' == /* ]]
+++ pwd -P
++ dir='/c/Users/Rafael Dominiquini/'
++ echo -nE '17140.18395.25160.27689/c/Users/Rafael Dominiquini/0'
++ local -a resp
++ true
++ IFS=$'\037'
++ read -rd $'\036' -a resp -u 11
++ [[ 17140.18395.25160.27689 == \1\7\1\4\0\.\1\8\3\9\5\.\2\5\1\6\0\.\2\7\6\8\9 ]]
++ break
++ [[ 0 == 1 ]]
++ VCS_STATUS_RESULT=norepo-sync
++ unset VCS_STATUS_WORKDIR
++ unset VCS_STATUS_COMMIT
++ unset VCS_STATUS_LOCAL_BRANCH
++ unset VCS_STATUS_REMOTE_BRANCH
++ unset VCS_STATUS_REMOTE_NAME
++ unset VCS_STATUS_REMOTE_URL
++ unset VCS_STATUS_ACTION
++ unset VCS_STATUS_INDEX_SIZE
++ unset VCS_STATUS_NUM_STAGED
++ unset VCS_STATUS_NUM_UNSTAGED
++ unset VCS_STATUS_NUM_CONFLICTED
++ unset VCS_STATUS_NUM_UNTRACKED
++ unset VCS_STATUS_HAS_STAGED
++ unset VCS_STATUS_HAS_UNSTAGED
++ unset VCS_STATUS_HAS_CONFLICTED
++ unset VCS_STATUS_HAS_UNTRACKED
++ unset VCS_STATUS_COMMITS_AHEAD
++ unset VCS_STATUS_COMMITS_BEHIND
++ unset VCS_STATUS_STASHES
++ unset VCS_STATUS_TAG
++ unset VCS_STATUS_NUM_UNSTAGED_DELETED
++ unset VCS_STATUS_NUM_STAGED_NEW
++ unset VCS_STATUS_NUM_STAGED_DELETED
++ unset VCS_STATUS_PUSH_REMOTE_NAME
++ unset VCS_STATUS_PUSH_REMOTE_URL
++ unset VCS_STATUS_PUSH_COMMITS_AHEAD
++ unset VCS_STATUS_PUSH_COMMITS_BEHIND
++ unset VCS_STATUS_NUM_SKIP_WORKTREE
++ unset VCS_STATUS_NUM_ASSUME_UNCHANGED
++ unset VCS_STATUS_COMMIT_ENCODING
++ unset VCS_STATUS_COMMIT_SUMMARY
++ [[ norepo-sync == ok-sync ]]
++ return 0
+++ __bp_preexec_invoke_exec gitstatus_prompt_update
+++ __bp_last_argument_prev_command=gitstatus_prompt_update
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ :
+++ __bp_preexec_invoke_exec -x
+++ __bp_last_argument_prev_command=-x
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __ps1_newline
++ [[ -z true ]]
++ printf '\n'

+++ __bp_preexec_invoke_exec -x
+++ __bp_last_argument_prev_command=-x
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_interactive_mode
++ __bp_preexec_interactive_mode=on

Rafael Dominiquini@DESKTOP:~ $ pwd

++ __bp_preexec_invoke_exec -x
++ __bp_last_argument_prev_command=-x
++ ((  __bp_inside_preexec > 0  ))
++ local __bp_inside_preexec=1
++ [[ ! -t 1 ]]
++ [[ -n '' ]]
++ [[ -z on ]]
++ [[ 0 -eq 0 ]]
++ __bp_preexec_interactive_mode=
++ __bp_in_prompt_command pwd
++ local prompt_command_array 'IFS=
;'
++ read -rd '' -a prompt_command_array
++ local trimmed_arg
++ __bp_trim_whitespace trimmed_arg pwd
++ local var=trimmed_arg text=pwd
++ text=pwd
++ text=pwd
++ printf -v trimmed_arg %s pwd
++ local command trimmed_command
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __bp_precmd_invoke_cmd
++ local var=trimmed_command text=__bp_precmd_invoke_cmd
++ text=__bp_precmd_invoke_cmd
++ text=__bp_precmd_invoke_cmd
++ printf -v trimmed_command %s __bp_precmd_invoke_cmd
++ [[ __bp_precmd_invoke_cmd == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command gitstatus_prompt_update
++ local var=trimmed_command text=gitstatus_prompt_update
++ text=gitstatus_prompt_update
++ text=gitstatus_prompt_update
++ printf -v trimmed_command %s gitstatus_prompt_update
++ [[ gitstatus_prompt_update == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command :
++ local var=trimmed_command text=:
++ text=:
++ text=:
++ printf -v trimmed_command %s :
++ [[ : == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __ps1_newline
++ local var=trimmed_command text=__ps1_newline
++ text=__ps1_newline
++ text=__ps1_newline
++ printf -v trimmed_command %s __ps1_newline
++ [[ __ps1_newline == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __bp_interactive_mode
++ local var=trimmed_command text=__bp_interactive_mode
++ text=__bp_interactive_mode
++ text=__bp_interactive_mode
++ printf -v trimmed_command %s __bp_interactive_mode
++ [[ __bp_interactive_mode == \p\w\d ]]
++ return 1
++ local this_command
+++ export LC_ALL=C
+++ LC_ALL=C
+++ HISTTIMEFORMAT=
+++ builtin history 1
+++ sed '1 s/^ *[0-9][0-9]*[* ] //'
++ this_command=pwd
++ [[ -z pwd ]]
++ local preexec_function
++ local preexec_function_ret_value
++ local preexec_ret_value=0
++ for preexec_function in "${preexec_functions[@]:-}"
++ type -t preexec
++ __bp_set_ret_value 0 -x
++ return 0
+ pwd
/c/Users/Rafael Dominiquini
+++ __bp_preexec_invoke_exec pwd
+++ __bp_last_argument_prev_command=pwd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_precmd_invoke_cmd
++ __bp_last_ret_value=0
++ BP_PIPESTATUS=("${PIPESTATUS[@]}")
++ ((  __bp_inside_precmd > 0  ))
++ local __bp_inside_precmd=1
++ local precmd_function
++ for precmd_function in "${precmd_functions[@]}"
++ type -t precmd
++ __bp_set_ret_value 0
++ return 0
+++ __bp_preexec_invoke_exec __bp_precmd_invoke_cmd
+++ __bp_last_argument_prev_command=__bp_precmd_invoke_cmd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ gitstatus_prompt_update
++ GITSTATUS_PROMPT=
++ gitstatus_query
++ unset OPTIND
++ timeout=()
++ local opt dir= timeout no_diff=0
++ getopts d:c:t:p opt
++ ((  OPTIND == 0 + 1  ))
++ [[ -n 10224 ]]
++ local req_id=10155.17111.27270.18530
++ [[ -z '' ]]
++ [[ '' == /* ]]
+++ pwd -P
++ dir='/c/Users/Rafael Dominiquini/'
++ echo -nE '10155.17111.27270.18530/c/Users/Rafael Dominiquini/0'
++ local -a resp
++ true
++ IFS=$'\037'
++ read -rd $'\036' -a resp -u 11
++ [[ 10155.17111.27270.18530 == \1\0\1\5\5\.\1\7\1\1\1\.\2\7\2\7\0\.\1\8\5\3\0 ]]
++ break
++ [[ 0 == 1 ]]
++ VCS_STATUS_RESULT=norepo-sync
++ unset VCS_STATUS_WORKDIR
++ unset VCS_STATUS_COMMIT
++ unset VCS_STATUS_LOCAL_BRANCH
++ unset VCS_STATUS_REMOTE_BRANCH
++ unset VCS_STATUS_REMOTE_NAME
++ unset VCS_STATUS_REMOTE_URL
++ unset VCS_STATUS_ACTION
++ unset VCS_STATUS_INDEX_SIZE
++ unset VCS_STATUS_NUM_STAGED
++ unset VCS_STATUS_NUM_UNSTAGED
++ unset VCS_STATUS_NUM_CONFLICTED
++ unset VCS_STATUS_NUM_UNTRACKED
++ unset VCS_STATUS_HAS_STAGED
++ unset VCS_STATUS_HAS_UNSTAGED
++ unset VCS_STATUS_HAS_CONFLICTED
++ unset VCS_STATUS_HAS_UNTRACKED
++ unset VCS_STATUS_COMMITS_AHEAD
++ unset VCS_STATUS_COMMITS_BEHIND
++ unset VCS_STATUS_STASHES
++ unset VCS_STATUS_TAG
++ unset VCS_STATUS_NUM_UNSTAGED_DELETED
++ unset VCS_STATUS_NUM_STAGED_NEW
++ unset VCS_STATUS_NUM_STAGED_DELETED
++ unset VCS_STATUS_PUSH_REMOTE_NAME
++ unset VCS_STATUS_PUSH_REMOTE_URL
++ unset VCS_STATUS_PUSH_COMMITS_AHEAD
++ unset VCS_STATUS_PUSH_COMMITS_BEHIND
++ unset VCS_STATUS_NUM_SKIP_WORKTREE
++ unset VCS_STATUS_NUM_ASSUME_UNCHANGED
++ unset VCS_STATUS_COMMIT_ENCODING
++ unset VCS_STATUS_COMMIT_SUMMARY
++ [[ norepo-sync == ok-sync ]]
++ return 0
+++ __bp_preexec_invoke_exec gitstatus_prompt_update
+++ __bp_last_argument_prev_command=gitstatus_prompt_update
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ :
+++ __bp_preexec_invoke_exec pwd
+++ __bp_last_argument_prev_command=pwd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __ps1_newline
++ [[ -z true ]]
++ printf '\n'

+++ __bp_preexec_invoke_exec pwd
+++ __bp_last_argument_prev_command=pwd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_interactive_mode
++ __bp_preexec_interactive_mode=on

akinomyoga commented 9 months ago

As far as I see the log, preexec is correctly working. If you expect your function preexec_hello_world to be called before the execution of pwd, you need to register the function preexec_hello_world to the preexec_functions array.

preexec_functions+=(preexec_hello_world)
Dominiquini commented 9 months ago

Sorry, I made the wrong test, but the bug report is correct. I tested all the examples and the "PREEXEC" doesn't work, while the "PRECMD" works fine. The same tests on Ubuntu (WSL2) works fine for both methods!


Rafael Dominiquini@DESKTOP:~ $ _preexec_functions+=(preexec_helloworld)

++ __bp_preexec_invoke_exec -x
++ __bp_last_argument_prev_command=-x
++ ((  __bp_inside_preexec > 0  ))
++ local __bp_inside_preexec=1
++ [[ ! -t 1 ]]
++ [[ -n '' ]]
++ [[ -z on ]]
++ [[ 0 -eq 0 ]]
++ __bp_preexec_interactive_mode=
++ __bp_in_prompt_command 'preexec_functions+=(preexec_hello_world)'
++ local prompt_command_array 'IFS=
;'
++ read -rd '' -a prompt_command_array
++ local trimmed_arg
++ __bp_trim_whitespace trimmed_arg 'preexec_functions+=(preexec_hello_world)'
++ local var=trimmed_arg 'text=preexec_functions+=(preexec_hello_world)'
++ text='preexec_functions+=(preexec_hello_world)'
++ text='preexec_functions+=(preexec_hello_world)'
++ printf -v trimmed_arg %s 'preexec_functions+=(preexec_hello_world)'
++ local command trimmed_command
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __bp_precmd_invoke_cmd
++ local var=trimmed_command text=__bp_precmd_invoke_cmd
++ text=__bp_precmd_invoke_cmd
++ text=__bp_precmd_invoke_cmd
++ printf -v trimmed_command %s __bp_precmd_invoke_cmd
++ [[ __bp_precmd_invoke_cmd == \p\r\e\e\x\e\c\_\f\u\n\c\t\i\o\n\s\+\=\(\p\r\e\e\x\e\c\_\h\e\l\l\o\_\w\o\r\l\d\) ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command gitstatus_prompt_update
++ local var=trimmed_command text=gitstatus_prompt_update
++ text=gitstatus_prompt_update
++ text=gitstatus_prompt_update
++ printf -v trimmed_command %s gitstatus_prompt_update
++ [[ gitstatus_prompt_update == \p\r\e\e\x\e\c\_\f\u\n\c\t\i\o\n\s\+\=\(\p\r\e\e\x\e\c\_\h\e\l\l\o\_\w\o\r\l\d\) ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command :
++ local var=trimmed_command text=:
++ text=:
++ text=:
++ printf -v trimmed_command %s :
++ [[ : == \p\r\e\e\x\e\c\_\f\u\n\c\t\i\o\n\s\+\=\(\p\r\e\e\x\e\c\_\h\e\l\l\o\_\w\o\r\l\d\) ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __ps1_newline
++ local var=trimmed_command text=__ps1_newline
++ text=__ps1_newline
++ text=__ps1_newline
++ printf -v trimmed_command %s __ps1_newline
++ [[ __ps1_newline == \p\r\e\e\x\e\c\_\f\u\n\c\t\i\o\n\s\+\=\(\p\r\e\e\x\e\c\_\h\e\l\l\o\_\w\o\r\l\d\) ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __bp_interactive_mode
++ local var=trimmed_command text=__bp_interactive_mode
++ text=__bp_interactive_mode
++ text=__bp_interactive_mode
++ printf -v trimmed_command %s __bp_interactive_mode
++ [[ __bp_interactive_mode == \p\r\e\e\x\e\c\_\f\u\n\c\t\i\o\n\s\+\=\(\p\r\e\e\x\e\c\_\h\e\l\l\o\_\w\o\r\l\d\) ]]
++ return 1
++ local this_command
+++ export LC_ALL=C
+++ LC_ALL=C
+++ HISTTIMEFORMAT=
+++ builtin history 1
+++ sed '1 s/^ *[0-9][0-9]*[* ] //'
++ this_command='preexec_functions+=(preexec_hello_world)'
++ [[ -z preexec_functions+=(preexec_hello_world) ]]
++ local preexec_function
++ local preexec_function_ret_value
++ local preexec_ret_value=0
++ for preexec_function in "${preexec_functions[@]:-}"
++ type -t preexec
++ __bp_set_ret_value 0 -x
++ return 0
+ preexec_functions+=(preexec_hello_world)
+++ __bp_preexec_invoke_exec ''
+++ __bp_last_argument_prev_command=
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_precmd_invoke_cmd
++ __bp_last_ret_value=0
++ BP_PIPESTATUS=("${PIPESTATUS[@]}")
++ ((  __bp_inside_precmd > 0  ))
++ local __bp_inside_precmd=1
++ local precmd_function
++ for precmd_function in "${precmd_functions[@]}"
++ type -t precmd
++ __bp_set_ret_value 0
++ return 0
+++ __bp_preexec_invoke_exec __bp_precmd_invoke_cmd
+++ __bp_last_argument_prev_command=__bp_precmd_invoke_cmd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ gitstatus_prompt_update
++ GITSTATUS_PROMPT=
++ gitstatus_query
++ unset OPTIND
++ timeout=()
++ local opt dir= timeout no_diff=0
++ getopts d:c:t:p opt
++ ((  OPTIND == 0 + 1  ))
++ [[ -n 2127 ]]
++ local req_id=20677.18782.26594.15276
++ [[ -z '' ]]
++ [[ '' == /* ]]
+++ pwd -P
++ dir='/c/Users/Rafael Dominiquini/'
++ echo -nE '20677.18782.26594.15276/c/Users/Rafael Dominiquini/0'
++ local -a resp
++ true
++ IFS=$'\037'
++ read -rd $'\036' -a resp -u 11
++ [[ 20677.18782.26594.15276 == \2\0\6\7\7\.\1\8\7\8\2\.\2\6\5\9\4\.\1\5\2\7\6 ]]
++ break
++ [[ 0 == 1 ]]
++ VCS_STATUS_RESULT=norepo-sync
++ unset VCS_STATUS_WORKDIR
++ unset VCS_STATUS_COMMIT
++ unset VCS_STATUS_LOCAL_BRANCH
++ unset VCS_STATUS_REMOTE_BRANCH
++ unset VCS_STATUS_REMOTE_NAME
++ unset VCS_STATUS_REMOTE_URL
++ unset VCS_STATUS_ACTION
++ unset VCS_STATUS_INDEX_SIZE
++ unset VCS_STATUS_NUM_STAGED
++ unset VCS_STATUS_NUM_UNSTAGED
++ unset VCS_STATUS_NUM_CONFLICTED
++ unset VCS_STATUS_NUM_UNTRACKED
++ unset VCS_STATUS_HAS_STAGED
++ unset VCS_STATUS_HAS_UNSTAGED
++ unset VCS_STATUS_HAS_CONFLICTED
++ unset VCS_STATUS_HAS_UNTRACKED
++ unset VCS_STATUS_COMMITS_AHEAD
++ unset VCS_STATUS_COMMITS_BEHIND
++ unset VCS_STATUS_STASHES
++ unset VCS_STATUS_TAG
++ unset VCS_STATUS_NUM_UNSTAGED_DELETED
++ unset VCS_STATUS_NUM_STAGED_NEW
++ unset VCS_STATUS_NUM_STAGED_DELETED
++ unset VCS_STATUS_PUSH_REMOTE_NAME
++ unset VCS_STATUS_PUSH_REMOTE_URL
++ unset VCS_STATUS_PUSH_COMMITS_AHEAD
++ unset VCS_STATUS_PUSH_COMMITS_BEHIND
++ unset VCS_STATUS_NUM_SKIP_WORKTREE
++ unset VCS_STATUS_NUM_ASSUME_UNCHANGED
++ unset VCS_STATUS_COMMIT_ENCODING
++ unset VCS_STATUS_COMMIT_SUMMARY
++ [[ norepo-sync == ok-sync ]]
++ return 0
+++ __bp_preexec_invoke_exec gitstatus_prompt_update
+++ __bp_last_argument_prev_command=gitstatus_prompt_update
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ :
+++ __bp_preexec_invoke_exec ''
+++ __bp_last_argument_prev_command=
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __ps1_newline
++ [[ -z true ]]
++ printf '\n'

+++ __bp_preexec_invoke_exec ''
+++ __bp_last_argument_prev_command=
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_interactive_mode
++ __bp_preexec_interactive_mode=on

Rafael Dominiquini@DESKTOP:~ $ pwd

++ __bp_preexec_invoke_exec ''
++ __bp_last_argument_prev_command=
++ ((  __bp_inside_preexec > 0  ))
++ local __bp_inside_preexec=1
++ [[ ! -t 1 ]]
++ [[ -n '' ]]
++ [[ -z on ]]
++ [[ 0 -eq 0 ]]
++ __bp_preexec_interactive_mode=
++ __bp_in_prompt_command pwd
++ local prompt_command_array 'IFS=
;'
++ read -rd '' -a prompt_command_array
++ local trimmed_arg
++ __bp_trim_whitespace trimmed_arg pwd
++ local var=trimmed_arg text=pwd
++ text=pwd
++ text=pwd
++ printf -v trimmed_arg %s pwd
++ local command trimmed_command
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __bp_precmd_invoke_cmd
++ local var=trimmed_command text=__bp_precmd_invoke_cmd
++ text=__bp_precmd_invoke_cmd
++ text=__bp_precmd_invoke_cmd
++ printf -v trimmed_command %s __bp_precmd_invoke_cmd
++ [[ __bp_precmd_invoke_cmd == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command gitstatus_prompt_update
++ local var=trimmed_command text=gitstatus_prompt_update
++ text=gitstatus_prompt_update
++ text=gitstatus_prompt_update
++ printf -v trimmed_command %s gitstatus_prompt_update
++ [[ gitstatus_prompt_update == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command :
++ local var=trimmed_command text=:
++ text=:
++ text=:
++ printf -v trimmed_command %s :
++ [[ : == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __ps1_newline
++ local var=trimmed_command text=__ps1_newline
++ text=__ps1_newline
++ text=__ps1_newline
++ printf -v trimmed_command %s __ps1_newline
++ [[ __ps1_newline == \p\w\d ]]
++ for command in "${prompt_command_array[@]:-}"
++ __bp_trim_whitespace trimmed_command __bp_interactive_mode
++ local var=trimmed_command text=__bp_interactive_mode
++ text=__bp_interactive_mode
++ text=__bp_interactive_mode
++ printf -v trimmed_command %s __bp_interactive_mode
++ [[ __bp_interactive_mode == \p\w\d ]]
++ return 1
++ local this_command
+++ export LC_ALL=C
+++ LC_ALL=C
+++ HISTTIMEFORMAT=
+++ builtin history 1
+++ sed '1 s/^ *[0-9][0-9]*[* ] //'
++ this_command=pwd
++ [[ -z pwd ]]
++ local preexec_function
++ local preexec_function_ret_value
++ local preexec_ret_value=0
++ for preexec_function in "${preexec_functions[@]:-}"
++ type -t preexec
++ for preexec_function in "${preexec_functions[@]:-}"
++ type -t preexec_hello_world
++ __bp_set_ret_value 0
++ return 0
++ preexec_hello_world pwd
++ echo 'You just entered pwd'
You just entered pwd
++ preexec_function_ret_value=0
++ [[ 0 != 0 ]]
++ __bp_set_ret_value 0 ''
++ return 0
+ pwd
/c/Users/Rafael Dominiquini
+++ __bp_preexec_invoke_exec pwd
+++ __bp_last_argument_prev_command=pwd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_precmd_invoke_cmd
++ __bp_last_ret_value=0
++ BP_PIPESTATUS=("${PIPESTATUS[@]}")
++ ((  __bp_inside_precmd > 0  ))
++ local __bp_inside_precmd=1
++ local precmd_function
++ for precmd_function in "${precmd_functions[@]}"
++ type -t precmd
++ __bp_set_ret_value 0
++ return 0
+++ __bp_preexec_invoke_exec __bp_precmd_invoke_cmd
+++ __bp_last_argument_prev_command=__bp_precmd_invoke_cmd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ gitstatus_prompt_update
++ GITSTATUS_PROMPT=
++ gitstatus_query
++ unset OPTIND
++ timeout=()
++ local opt dir= timeout no_diff=0
++ getopts d:c:t:p opt
++ ((  OPTIND == 0 + 1  ))
++ [[ -n 2127 ]]
++ local req_id=17907.1994.13803.7542
++ [[ -z '' ]]
++ [[ '' == /* ]]
+++ pwd -P
++ dir='/c/Users/Rafael Dominiquini/'
++ echo -nE '17907.1994.13803.7542/c/Users/Rafael Dominiquini/0'
++ local -a resp
++ true
++ IFS=$'\037'
++ read -rd $'\036' -a resp -u 11
++ [[ 17907.1994.13803.7542 == \1\7\9\0\7\.\1\9\9\4\.\1\3\8\0\3\.\7\5\4\2 ]]
++ break
++ [[ 0 == 1 ]]
++ VCS_STATUS_RESULT=norepo-sync
++ unset VCS_STATUS_WORKDIR
++ unset VCS_STATUS_COMMIT
++ unset VCS_STATUS_LOCAL_BRANCH
++ unset VCS_STATUS_REMOTE_BRANCH
++ unset VCS_STATUS_REMOTE_NAME
++ unset VCS_STATUS_REMOTE_URL
++ unset VCS_STATUS_ACTION
++ unset VCS_STATUS_INDEX_SIZE
++ unset VCS_STATUS_NUM_STAGED
++ unset VCS_STATUS_NUM_UNSTAGED
++ unset VCS_STATUS_NUM_CONFLICTED
++ unset VCS_STATUS_NUM_UNTRACKED
++ unset VCS_STATUS_HAS_STAGED
++ unset VCS_STATUS_HAS_UNSTAGED
++ unset VCS_STATUS_HAS_CONFLICTED
++ unset VCS_STATUS_HAS_UNTRACKED
++ unset VCS_STATUS_COMMITS_AHEAD
++ unset VCS_STATUS_COMMITS_BEHIND
++ unset VCS_STATUS_STASHES
++ unset VCS_STATUS_TAG
++ unset VCS_STATUS_NUM_UNSTAGED_DELETED
++ unset VCS_STATUS_NUM_STAGED_NEW
++ unset VCS_STATUS_NUM_STAGED_DELETED
++ unset VCS_STATUS_PUSH_REMOTE_NAME
++ unset VCS_STATUS_PUSH_REMOTE_URL
++ unset VCS_STATUS_PUSH_COMMITS_AHEAD
++ unset VCS_STATUS_PUSH_COMMITS_BEHIND
++ unset VCS_STATUS_NUM_SKIP_WORKTREE
++ unset VCS_STATUS_NUM_ASSUME_UNCHANGED
++ unset VCS_STATUS_COMMIT_ENCODING
++ unset VCS_STATUS_COMMIT_SUMMARY
++ [[ norepo-sync == ok-sync ]]
++ return 0
+++ __bp_preexec_invoke_exec gitstatus_prompt_update
+++ __bp_last_argument_prev_command=gitstatus_prompt_update
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ :
+++ __bp_preexec_invoke_exec pwd
+++ __bp_last_argument_prev_command=pwd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __ps1_newline
++ [[ -z true ]]
++ printf '\n'

+++ __bp_preexec_invoke_exec pwd
+++ __bp_last_argument_prev_command=pwd
+++ ((  __bp_inside_preexec > 0  ))
+++ local __bp_inside_preexec=1
+++ [[ ! -t 1 ]]
+++ [[ -n '' ]]
+++ [[ -z '' ]]
+++ return
++ __bp_interactive_mode
++ __bp_preexec_interactive_mode=on

Sorry for the mistake and thanks for the reply!

akinomyoga commented 9 months ago

As far as I see the above log, it is working. preexec_hello_world pwd is executed, and the message You just entered pwd is printed. Isn't this the behavior you expect? What is the problem? You say preexec does not work in MSYS2, but it is unclear what the term "work" means in your mind.

Could you describe 1) how you set up your preexec and precmd configurations, 2) how you test it, 3) what is the behavior you expect, and 4) what is the actual result and how it is different from the expectation? None of the above items is provided, so essentially, I can only read from the report that "something (described in terms of the unknown word "working") is happening in MSYS2".

Dominiquini commented 9 months ago

Without the "set -x" command, the phrase "You just entered pwd" doesn't appear in the terminal! Following the example provided on the README of the project, bash should run every function registered on the 'preexec_functions' before the output of the command, and every function registered on 'precmd_functions' after the output of the command (and before the next prompt). Testing on WSL2, everything works fine, but on MSYS2, only the 'precmd_functions' work! Without the 'set -x', the functions defined on 'preexec_functions' doesn't appear to be running!

Sorry if I'm not clear!

Thanks.

akinomyoga commented 9 months ago

Thank you for the explanation. So the problem goes away if you specify set -x, which means that the above log is actually not useful.

I tried to reproduce the problem in MSYS2 at my side, but the symptom does not appear here.

One possibility might be that there is intereference with other shell configurations. Do you observe the problem with the minimal configuration of Bash? For example, if you start a Bash session with bash --norc and manually load bash-preexec, does the problem reproduce? See below for the sequence of commands.

$ bash --norc                          # <-- start a new child Bash session without configs
$ source /path/to/bash-preexec.sh      # <-- load bash-preexec. Please replace /path/to with an appropriate one
$ preexec_hello_world() { echo "You just entered $1"; }     # <-- define a function
$ preexec_functions+=(preexec_hello_world)                  # <- register the function to preexec.
$ pwd      # <- please see if the preexec message is printed
$ exit     # <- after testing, you can exit the child Bash session and come back to the original Bash session