Skip to content

Conversation

@seefood
Copy link
Contributor

@seefood seefood commented Oct 4, 2025

Summary

Complete implementation of automatic .bashrc sourcing detection and setup for macOS, BSD, Solaris, and Illumos systems.

This PR has two parts:

  1. Diagnostic (in bash-it doctor) - Detects sourcing issues and warns users
  2. Auto-fix (in install.sh) - Automatically sets up sourcing during installation

Problem

On macOS/BSD/Solaris, login shells source .bash_profile or .profile, NOT .bashrc. This causes bash-it to fail loading in:

  • Terminal.app (macOS default)
  • SSH sessions
  • Any new login shell

Users would install bash-it successfully but wonder why it doesn't work in their terminal.

Solution

Part 1: Detection in bash-it doctor

New "Profile Configuration" section that intelligently detects if .bashrc is being sourced:

Detection Strategy (Two-Phase):

  1. Grep Detection (Primary - 99% of cases)

    • Fast pattern matching for common .bashrc sourcing patterns
    • Detects various syntaxes: source ~/.bashrc, . $HOME/.bashrc, BASH_VERSION checks
    • No file modifications, completely safe
  2. Brute Force Test (Fallback - Edge cases)

    • Only runs if grep is inconclusive
    • Safely tests actual behavior:
      • Move .bashrc aside using mv (handles symlinks correctly)
      • Create minimal test .bashrc with echo marker
      • Test in login shell: bash -l
      • Immediately restore original .bashrc
    • Atomic operations, immediate restore on success/failure

Example Output:

✅ Working:

## Profile Configuration
Note: /home/user/.bash_profile is a symlink to .dotfiles/bash_profile
✓ .bashrc is sourced from /home/user/.bash_profile

✗ Problem detected:

## Profile Configuration
✗ .bashrc is NOT sourced from /home/user/.bash_profile
  Warning: bash-it will not load in login shells (Terminal.app, SSH sessions)
  Fix: Add the following to /home/user/.bash_profile:

    if [ -n "$BASH_VERSION" ]; then
        if [ -f "$HOME/.bashrc" ]; then
            . "$HOME/.bashrc"
        fi
    fi

Part 2: Auto-Fix in install.sh

During installation, automatically detect and fix .bashrc sourcing:

  1. Platform Detection:

    • Only runs on affected platforms (Darwin, SunOS, Illumos, *BSD)
    • Linux users unaffected (no extra prompts)
  2. Smart Detection:

    • Reuses _bash-it-doctor-check-profile-sourcing-grep() from doctor
    • Fast grep-based detection of existing sourcing patterns
    • Skips if already configured
  3. Interactive Prompt:

    • Prompts user: "Would you like to add .bashrc sourcing to [file]? [Y/n]"
    • Shows manual instructions if user declines
    • Silent mode: auto-accepts (safe default behavior)
  4. Safe Modifications:

    • Backs up profile file with timestamp before modifying
    • Uses command cp to bypass user aliases
    • Creates .bash_profile if no profile file exists
    • Appends standard BASH_VERSION check snippet

User Experience:

Before (macOS user):

$ ./install.sh
Installation finished successfully!
$ # opens new terminal... bash-it doesn't work! 😞

After (macOS user):

$ ./install.sh
...
Warning: .bashrc is not sourced from /Users/me/.bash_profile
On macOS/BSD/Solaris, login shells won't load bash-it without this.

Would you like to add .bashrc sourcing to /Users/me/.bash_profile? [Y/n] y
Backed up /Users/me/.bash_profile to /Users/me/.bash_profile.bak.20251004_193245
✓ Added .bashrc sourcing to /Users/me/.bash_profile

Installation finished successfully!
$ # opens new terminal... bash-it works! 😃

Code Quality

Defensive Programming:

  • All sensitive commands prefixed with command to bypass user aliases:
    • command mv (prevents alias mv='mv -i' prompts)
    • command grep (prevents alias grep='grep --color' breakage)
    • command cp (prevents alias cp='cp -i' prompts)
  • Thanks to @akinomyoga for catching this!

Documentation:

  • Added to CLAUDE.md coding standards
  • Documents command prefix pattern for future contributors

Testing

  • ✅ Tested on Linux (shows "Not applicable")
  • ✅ Tested with .bashrc properly sourced (grep detection)
  • ✅ Tested installation flow (detection works)
  • ✅ Shellcheck passes
  • ✅ Pre-commit hooks pass
  • ✅ Handles symlinked profile files (common with homesick/dotfiles)
  • ✅ Silent mode compatible

Closes

Closes #1455 (Solaris/Illumos support)

Related

Built on top of merged PR #2341 (enhanced doctor command)


🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

seefood and others added 2 commits October 4, 2025 21:04
Adds comprehensive checking to detect if .bashrc is properly sourced from
login shell profile files on macOS, Solaris, Illumos, and BSD systems.

**Problem:**
On macOS/BSD/Solaris, login shells source .bash_profile or .profile, NOT
.bashrc. If these files don't source .bashrc, bash-it won't load in:
- Terminal.app (macOS)
- SSH sessions
- New login shells

**Solution:**
New "Profile Configuration" section in `bash-it doctor` that:

1. **Grep Detection (Primary)**
   - Fast, safe pattern matching for common .bashrc sourcing patterns
   - Detects: `source ~/.bashrc`, `. $HOME/.bashrc`, BASH_VERSION checks
   - Handles quoted paths and various syntaxes

2. **Brute Force Test (Fallback)**
   - Used only when grep is unclear
   - Temporarily replaces .bashrc with echo marker
   - Tests in login shell: `bash -l`
   - Immediately restores original .bashrc
   - Handles symlinks safely (via mv)

3. **User Guidance**
   - Shows if profile file is a symlink (common with homesick/dotfiles)
   - Provides clear ✓/✗ status with color coding
   - Displays fix snippet for copy/paste when needed

**Implementation Notes:**
- Only runs on affected platforms (Darwin, SunOS, Illumos, *BSD)
- Grep detection catches 99% of cases safely
- Brute force is safe: atomic mv operations, immediate restore
- Provides actionable fix instructions

**Development Guidelines:**
- Added Git workflow section to CLAUDE.md
- Documents "never commit to master" policy
- Ensures feature branch workflow

Related to issue Bash-it#1455

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
seefood and others added 2 commits October 4, 2025 21:17
Co-authored-by: Koichi Murase <[email protected]>
Addresses review feedback from @akinomyoga on PR Bash-it#2342.

**Problem:**
Users may have aliases like:
- `alias mv='mv -i'` (prompts for confirmation)
- `alias grep='grep --color=always'` (breaks parsing)
- `alias rm='rm -i'` (prompts for confirmation)

These aliases can break bash-it core functions that assume standard
command behavior.

**Solution:**
Prefix sensitive commands with `command` to bypass aliases:
- `command mv` - ensures atomic file operations without prompts
- `command grep` - ensures predictable output format
- Applied to all mv/grep calls in doctor functions

**Documentation:**
Added coding standard to CLAUDE.md:
- Documents the `command` prefix pattern
- Lists common commands that should be prefixed
- Explains why this prevents surprises in core functions

This is a defensive programming practice that makes bash-it more
robust against varied user configurations.

Thanks to @akinomyoga for catching this!

Related to PR Bash-it#2342

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@seefood seefood self-assigned this Oct 4, 2025
@seefood seefood merged commit e98418c into Bash-it:master Oct 4, 2025
6 checks passed
@seefood seefood deleted the feature/bashrc-sourcing-detection branch October 4, 2025 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Solaris & Illumos etc. (*easy* fix, code suggestion within)

2 participants