diff --git a/README.md b/README.md index 09b4677..fab1228 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,9 @@ export DOTFILES="$HOME/.dotfiles" # <<< dotfiles:base <<< ``` +(The `~/.bashrc` block has one more line after the `export`, `case $- in *i*) ;; *) return ;; esac`: +bash reads `~/.bashrc` for ssh commands too, and they must not load the shell setup.) + omnishell and the `shell.d` block then *append* their marker blocks to those files. If the rc file were a stow symlink into the repo, this appends would be written straight back into version control, so bootstrap writes a real file that diff --git a/bootstrap.sh b/bootstrap.sh index fc1963f..2cc1937 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -596,14 +596,20 @@ install_omnishell() { # rc file (relative to $HOME) : the rc library its base block sources RC_BASES=(".zshrc:zsh/zshrc.zsh" ".bashrc:bash/bashrc.bash") -# the dotfiles:base block that points an rc file at this checkout +# the dotfiles:base block that points an rc file at this checkout. bash reads ~/.bashrc +# for non-interactive shells too (sshd starts every remote command through it), so the +# bash block returns early there: nothing below it is meant for ssh commands, and +# Ansible's become prompt detection breaks when it runs. _rc_base_block() { - cat <>> dotfiles:base >>> -export DOTFILES="$DOTFILES" -[ -r "\$DOTFILES/$1" ] && . "\$DOTFILES/$1" -# <<< dotfiles:base <<< -EOF + printf '%s\n' '# >>> dotfiles:base >>>' + printf 'export DOTFILES="%s"\n' "$DOTFILES" + case "$1" in + bash/*) + # shellcheck disable=SC2016 # literal text for the rc file, not an expansion here + printf '%s\n' 'case $- in *i*) ;; *) return ;; esac' ;; + esac + printf '[ -r "$DOTFILES/%s" ] && . "$DOTFILES/%s"\n' "$1" "$1" + printf '%s\n' '# <<< dotfiles:base <<<' } # the DOTFILES= path inside an rc file's base block; empty without a block diff --git a/tests/test-bashrc-noninteractive.sh b/tests/test-bashrc-noninteractive.sh new file mode 100755 index 0000000..f3f32f6 --- /dev/null +++ b/tests/test-bashrc-noninteractive.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# bootstrap.sh write_rc_base: ~/.bashrc stops early in non-interactive shells. bash reads +# ~/.bashrc for every ssh command, and running the rc libraries there breaks callers that +# expect a quiet shell (Ansible's become prompt detection); ~/.zshrc is interactive-only +# and gets no guard. +# shellcheck disable=SC2034 # variables are read inside the eval'd check expressions +set -euo pipefail + +REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT +failures=0 + +pass() { printf ' ok %s\n' "$1"; } +fail() { printf ' FAIL %s\n' "$1"; failures=$((failures + 1)); } +check() { if eval "$2"; then pass "$1"; else fail "$1"; fi; } + +# a fake checkout whose rc libraries only leave a marker file behind +CHECKOUT="$WORK/checkout" +mkdir -p "$CHECKOUT/bash" "$CHECKOUT/zsh" +cp "$REPO/bootstrap.sh" "$CHECKOUT/" +cp -R "$REPO/lib" "$CHECKOUT/" +printf 'touch "$MARK"\n' > "$CHECKOUT/bash/bashrc.bash" +printf 'touch "$MARK"\n' > "$CHECKOUT/zsh/zshrc.zsh" + +H="$WORK/home" +mkdir -p "$H" +env HOME="$H" BOOTSTRAP_SOURCE_ONLY=1 "$BASH" -c ". '$CHECKOUT/bootstrap.sh'; write_rc_base" > "$WORK/out" 2>&1 + +echo ">> generated rc files" +check "bashrc has one base block" "[ \"\$(grep -c '^# >>> dotfiles:base >>>\$' '$H/.bashrc')\" = 1 ]" +check "bashrc returns early when not interactive" "grep -qxF 'case \$- in *i*) ;; *) return ;; esac' '$H/.bashrc'" +check "the guard sits after DOTFILES is exported and before the library is sourced" \ + "awk '/^export DOTFILES=/{e=NR} /^case /{g=NR} /^\\[ -r /{s=NR} END{exit !(e> behaviour" +MARK="$WORK/mark-noninteractive" +env HOME="$H" MARK="$MARK" "$BASH" -c ". '$H/.bashrc'; echo sourced-ok" > "$WORK/ni.out" 2>&1 +check "a non-interactive shell does not load the rc library" "[ ! -e '$MARK' ] && grep -q sourced-ok '$WORK/ni.out'" +MARK="$WORK/mark-interactive" +env HOME="$H" MARK="$MARK" "$BASH" -ic ". '$H/.bashrc'" > /dev/null 2>&1 || true +check "an interactive shell loads the rc library" "[ -e '$MARK' ]" + +if [ "$failures" -ne 0 ]; then + printf 'FAILED: %s\n' "$failures" + exit 1 +fi +echo "PASS: bashrc guard"