Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions bin/min-vscode
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,10 @@ put() {
# A multi-line value is dropped whole: its later lines could pass as NAME=...
# A `min:motd` line that differs (an older one) is replaced. bash login shells
# (sshd's) read ~/.bash_profile: one is created that sources ~/.bashrc, or a
# marked `min:bashrc` line is added to one that does not mention it.
# marked `min:bashrc` line is added to one that does not mention it. The line
# skips a nested source (MIN_BASHRC), so a ~/.bashrc that sources
# ~/.bash_profile does not loop; an older, unguarded line of ours (marked, or
# a profile that is exactly the line we used to create) is replaced.
# Remote-SSH defaults remote.autoForwardPortsSource to output, which leaves
# VS Code's /proc port finder off: a listener that prints no URL, as Claude's
# login callback does, is then forwarded only if VS Code opens its login URL
Expand Down Expand Up @@ -317,11 +320,15 @@ for rc in "$HOME/.bashrc" "$HOME/.zshrc"; do
printf "%s\n" "# min:motd (managed by $0)" "$motd" >> "$rc"
fi
done
if [ ! -e "$HOME/.bash_profile" ]; then
printf "%s\n" "[ -f \"\$HOME/.bashrc\" ] && . \"\$HOME/.bashrc\"" > "$HOME/.bash_profile"
elif ! grep -Eqs "(min:bashrc|\.bashrc)" "$HOME/.bash_profile"; then
[ -z "$(tail -c 1 "$HOME/.bash_profile")" ] || echo >> "$HOME/.bash_profile"
printf "%s\n" "# min:bashrc (managed by $0)" "[ -f \"\$HOME/.bashrc\" ] && . \"\$HOME/.bashrc\"" >> "$HOME/.bash_profile"
bp="$HOME/.bash_profile"
src="if [ -z \"\${MIN_BASHRC:-}\" ] && [ -f \"\$HOME/.bashrc\" ]; then MIN_BASHRC=1; . \"\$HOME/.bashrc\"; unset MIN_BASHRC; fi"
if [ ! -e "$bp" ] || [ "$(cat "$bp")" = "[ -f \"\$HOME/.bashrc\" ] && . \"\$HOME/.bashrc\"" ]; then
printf "%s\n" "# min:bashrc (managed by $0)" "$src" > "$bp"
elif grep -qs "^# min:bashrc " "$bp"; then
grep -qsxF -- "$src" "$bp" || { awk -v s="$src" "/^# min:bashrc /{print; print s; x=1; next} x{x=0; next} 1" "$bp" > "$bp.$$" && cat "$bp.$$" > "$bp"; rm -f "$bp.$$"; }
elif ! grep -qs "\.bashrc" "$bp"; then
[ -z "$(tail -c 1 "$bp")" ] || echo >> "$bp"
printf "%s\n" "# min:bashrc (managed by $0)" "$src" >> "$bp"
fi
v="$HOME/.vscode-server/data/Machine/settings.json"
if [ ! -L "$v" ] && ! grep -qsF remote.autoForwardPortsSource "$v"; then
Expand Down
19 changes: 13 additions & 6 deletions loadouts/vscode.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,19 @@ for rc in "$HOME/.bashrc" "$HOME/.zshrc"; do
done
# bash login shells (what sshd starts) read ~/.bash_profile, not ~/.bashrc:
# create one that sources it, or add the (marked) line to one that does not
# (after a newline, if its last line has none).
if [ ! -e "$HOME/.bash_profile" ]; then
printf '%s\n' '[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"' > "$HOME/.bash_profile" </dev/null || true
elif ! grep -Eqs '(min:bashrc|\.bashrc)' "$HOME/.bash_profile" </dev/null; then
[ -z "$(tail -c 1 "$HOME/.bash_profile" </dev/null)" ] || echo >> "$HOME/.bash_profile" </dev/null || true
printf '%s\n' '# min:bashrc (managed by min-vscode)' '[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"' >> "$HOME/.bash_profile" </dev/null || true
# (after a newline, if its last line has none). The line skips a nested source
# (MIN_BASHRC), so a ~/.bashrc that sources ~/.bash_profile does not loop; an
# older, unguarded line of ours (marked, or a profile that is exactly the line
# we used to create) is replaced.
bp="$HOME/.bash_profile"
src='if [ -z "${MIN_BASHRC:-}" ] && [ -f "$HOME/.bashrc" ]; then MIN_BASHRC=1; . "$HOME/.bashrc"; unset MIN_BASHRC; fi'
if [ ! -e "$bp" ] || [ "$(cat "$bp" </dev/null)" = '[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"' ]; then
printf '%s\n' '# min:bashrc (managed by min-vscode)' "$src" > "$bp" </dev/null || true
elif grep -qs '^# min:bashrc ' "$bp" </dev/null; then
grep -qsxF -- "$src" "$bp" </dev/null || { awk -v s="$src" '/^# min:bashrc /{print; print s; x=1; next} x{x=0; next} 1' "$bp" > "$bp.$$" </dev/null && cat "$bp.$$" > "$bp" </dev/null; rm -f "$bp.$$" </dev/null; } || true
elif ! grep -qs '\.bashrc' "$bp" </dev/null; then
[ -z "$(tail -c 1 "$bp" </dev/null)" ] || echo >> "$bp" </dev/null || true
printf '%s\n' '# min:bashrc (managed by min-vscode)' "$src" >> "$bp" </dev/null || true
fi
true
'''
35 changes: 34 additions & 1 deletion tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,39 @@ case_bash_profile_sources_bashrc() {
done
}

# A ~/.bashrc that sources ~/.bash_profile does not loop through our line: a
# bash login shell and an interactive one both finish, ~/.bashrc run once per
# entry. Either writer upgrades an older, unguarded line of ours (marked, or
# the profile we used to create) to the guarded one and keeps the rest.
# shellcheck disable=SC2016 # rc lines, literal
case_bash_profile_guards_reentry() {
command -v bash >/dev/null 2>&1 || { skip "no bash"; return 0; }
for w in loadout prep; do
for start in none created marked; do
rm -rf "$T/box"; mkdir -p "$T/box"
printf '%s\n' 'N=$((${N:-0} + 1))' '[ -f "$HOME/.bash_profile" ] && . "$HOME/.bash_profile"' > "$T/box/.bashrc"
case $start in
created) printf '%s\n' '[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"' > "$T/box/.bash_profile" ;;
marked) printf '%s\n' 'export FOO=1' "# min:bashrc (managed by $TOOL)" '[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"' 'export BAR=2' > "$T/box/.bash_profile" ;;
esac
for _ in 1 2; do
if [ $w = loadout ]; then HOME=$T/box sh -c "$(loadout_hook 0)" < /dev/null
else run_prep "$T/p1" || fail "PREP failed: $(cat "$T/p1.err")"; fi
done
p=$T/box/.bash_profile
[ "$(grep -c 'min:bashrc' "$p")" = 1 ] || fail "$w/$start: $(cat "$p")"
[ "$(grep -c 'MIN_BASHRC=1' "$p")" = 1 ] || fail "$w/$start: $(cat "$p")"
grep -qxF '[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"' "$p" && fail "$w/$start: unguarded line left: $(cat "$p")"
if [ $start = marked ]; then
[ "$(head -1 "$p")" = "export FOO=1" ] || fail "$w/$start: $(cat "$p")"
[ "$(tail -1 "$p")" = "export BAR=2" ] || fail "$w/$start: $(cat "$p")"
fi
o=$(HOME=$T/box bash -l -c 'echo "n=$N"' 2>&1 < /dev/null)
[ "$o" = "n=1" ] || fail "$w/$start: bash -l printed: $o"
done
done
}

# ---- ssh-config ----------------------------------------------------------------

case_sshconfig_install_creates_include_and_stanza() {
Expand Down Expand Up @@ -3043,7 +3076,7 @@ config_print_and_bad_option config_and_doctor_keep_entries_when_min_ls_fails mer
merger_symlink_written_through merger_preserves_foreign_entries_vscode_put_in_block
merger_moves_foreign_keys_out_of_block merger_upgrades_old_block merger_bad_jsonc_left_alone doctor_rows doctor_min_version
new_empty_no_window attach_no_window destroy_if_idle destroy_unsaved_needs_force status_json setup_installs_follows_and_removes setup_remove_stops_when_config_refuses setup_through_link setup_shares_skills_with_other_tools
loadout_rc_line_once loadout_env_keeps_motd loadout_env_unquoted loadout_env_drops_multiline loadout_env_without_awk prep_and_loadout_rc_lines_agree motd_rc_line_quiet_without_tty motd_rc_line_on_tty motd_rc_line_survives_errexit motd_rc_line_skips_attach_shell motd_rc_line_replaces_older bash_profile_sources_bashrc
loadout_rc_line_once loadout_env_keeps_motd loadout_env_unquoted loadout_env_drops_multiline loadout_env_without_awk prep_and_loadout_rc_lines_agree motd_rc_line_quiet_without_tty motd_rc_line_on_tty motd_rc_line_survives_errexit motd_rc_line_skips_attach_shell motd_rc_line_replaces_older bash_profile_sources_bashrc bash_profile_guards_reentry
sshconfig_install_creates_include_and_stanza sshconfig_remove_restores_original
sshconfig_coexists_with_another_tool sshconfig_keeps_symlinked_config sshconfig_print_touches_nothing
sshconfig_destroy_drops_stanza sshconfig_stale_and_invalid_dirs_skipped sshconfig_real_ssh_resolves
Expand Down