@@ -1670,6 +1670,20 @@ function ble/util/readlink {
16701670
16711671# ---------------------------------------
16721672
1673+ function ble/init/adjust-environment {
1674+ builtin unset -f " $FUNCNAME "
1675+
1676+ if [[ ${IN_NIX_SHELL-} ]]; then
1677+ # Since "nix-shell" overwrites BASH to the path to a binary image different
1678+ # from the current one, the Bash process crashes on attempting loading
1679+ # loadable builtins. We rewrite it to the correct one.
1680+ local ret=
1681+ ble/util/readlink " /proc/$$ /exe" 2> /dev/null
1682+ [[ -x $ret ]] && BASH=$ret
1683+ fi
1684+ }
1685+ ble/init/adjust-environment
1686+
16731687_ble_bash_path=
16741688function ble/bin/.load-builtin {
16751689 local name=$1 path=$2
@@ -2624,6 +2638,47 @@ function ble/base/load-rcfile {
26242638 fi
26252639}
26262640
2641+ # ble-attach needs to be performed at the very end of the Bash startup file.
2642+ # However, in some environment, the terminal or the session manager would start
2643+ # Bash with a custom startup file, and ~/.bashrc is sourced from the custom
2644+ # startup file. In this case, when the user puts "ble-attach" at the end of
2645+ # ~/.bashrc, other settings would continue to be executed even after the
2646+ # execution of "ble-attach".
2647+ function ble/base/attach/.needs-prompt-attach {
2648+ local ext=1
2649+
2650+ [[ $1 == * :force:* ]] && return 1
2651+
2652+ # nix-shell loads the Bash startup file from inside its custom file "rc".
2653+ if [[ ${IN_NIX_SHELL-} && " ${BASH_SOURCE[*]} " == * /rc ]]; then
2654+ # We force prompt-attach when ble-attach is run inside "nix-shell rc".
2655+ ext=0
2656+ fi
2657+
2658+ if [[ ${VSCODE_INJECTION-} ]]; then
2659+ # VS Code also tries to source ~/.bashrc from its
2660+ # "shellIntegration-bash.sh". VS Code shell integration seems to set the
2661+ # variable "VSCODE_INJECTION" while it sources the user's startup file, and
2662+ # it unsets the variable after the initialization.
2663+ ext=0
2664+ elif [[ ${kitty_bash_inject-} ]]; then
2665+ # When the startup file is sourced from kitty's shell ingteration
2666+ # "kitty.bash", the variable "kitty_bash_inject" is set. The variable is
2667+ # unset after the initialization. If we find it, we cancel the manual
2668+ # attaching and switch to the prompt attach.
2669+ ext=0
2670+ elif [[ ${ghostty_bash_inject-} ]]; then
2671+ # Ghostty seems to use a shell-integration code derived from kitty's. By
2672+ # the way, kitty is licensed under GPL-3.0, while Ghostty is licensed under
2673+ # the MIT license. Is it allowed to include a derivative of a part of
2674+ # kitty in the MIT-licensed Ghostty? It may be non-trivial whether the
2675+ # shell integration is an essential part of Ghostty.
2676+ ext=0
2677+ fi
2678+
2679+ return " $ext "
2680+ }
2681+
26272682# # @fn ble-attach [opts]
26282683function ble-attach {
26292684# %if leakvar
@@ -2670,31 +2725,24 @@ function ble-attach {
26702725# %if leakvar
26712726ble/debug/leakvar#check $" leakvar" A4-adjust
26722727# %end.i
2673- if [[ ${IN_NIX_SHELL-} ]]; then
2674- # nix-shell rc の中から実行している時は強制的に prompt-attach にする
2675- if [[ " ${BASH_SOURCE[*]} " == * /rc && $1 != * :force:* ]]; then
2676- ble/base/install-prompt-attach
2677- _ble_attached=
2678- BLE_ATTACHED=
2679- ble/base/restore-BASH_REMATCH
2680- ble/base/restore-bash-options
2681- ble/base/restore-builtin-wrappers
2682- ble/base/restore-POSIXLY_CORRECT
2728+
2729+ if ble/base/attach/.needs-prompt-attach; then
2730+ ble/base/install-prompt-attach
2731+ _ble_attached=
2732+ BLE_ATTACHED=
2733+ ble/base/restore-BASH_REMATCH
2734+ ble/base/restore-bash-options
2735+ ble/base/restore-builtin-wrappers
2736+ ble/base/restore-POSIXLY_CORRECT
26832737# %if leakvar
26842738ble/debug/leakvar#check $" leakvar" A4b1
26852739# %end.i
2686- builtin eval -- " $_ble_bash_FUNCNEST_restore "
2687- return 0
2688- fi
2689-
2690- # nix-shell は BASH を誤った値に書き換えるので上書きする。
2691- local ret
2692- ble/util/readlink " /proc/$$ /exe"
2693- [[ -x $ret ]] && BASH=$ret
2740+ builtin eval -- " $_ble_bash_FUNCNEST_restore "
2741+ return 0
2742+ fi
26942743# %if leakvar
26952744ble/debug/leakvar#check $" leakvar" A4b2
26962745# %end.i
2697- fi
26982746
26992747 # reconnect standard streams
27002748 ble/fd/save-external-standard-streams
0 commit comments