dotfiles icon indicating copy to clipboard operation
dotfiles copied to clipboard

Improvement: Add deduplication check in bashrc/zshrc 00-init to prevent unbounded PATH growth

Open tetam01 opened this issue 2 months ago • 2 comments

Describe the bug

In ~/.config/bashrc/00-init and ~/.config/zshrc/00-init, the cargo and local bin directories are appended directly to $PATH without checking if they are already present:

export PATH=$PATH:~/.cargo/bin/
export PATH=$PATH:~/.local/bin/

In standard daily terminal usage, this goes unnoticed because a fresh terminal only sources .bashrc once. However, in more advanced setups (e.g. running background widgets like Quickshell, custom toolchains, nested subshells invoking bash -c, or scripts that re-source the shell environment), 00-init can be executed multiple times within the same session hierarchy. Because there is no duplicate check, the exact same paths are appended over and over again. Eventually, $PATH can grow to hundreds of kilobytes and hit the Linux kernel limit (ARG_MAX), resulting in:

  • E2BIG: Argument list too long on new process executions.
  • Compositor shortcuts failing to spawn programs. Adding a simple check before appending makes 00-init idempotent and bulletproof against repeated sourcing.

Steps to reproduce

1. Open a terminal and check the length of `$PATH`:
   `echo $PATH | wc -c`
2. Simulate repeated subshell invocations or run the desktop session across multiple sleep/wake cycles:
   `for i in {1..50}; do bash -c 'true'; done`
3. Inspect `$PATH`:
   `echo $PATH`
4. Notice dozens of duplicated `~/.cargo/bin` and `~/.local/bin` entries inflating the environment size.

### Expected behavior

Pads should only be added to `$PATH` if they are not already present, preventing duplicate entries and buffer overflows regardless of how often subshells are spawned.
**Proposed fix for `00-init`:**
```bash
# Add ~/.cargo/bin if not already in PATH
case ":$PATH:" in
    *":$HOME/.cargo/bin:"*) ;;
    *) export PATH="$PATH:$HOME/.cargo/bin" ;;
esac
# Add ~/.local/bin if not already in PATH
case ":$PATH:" in
    *":$HOME/.local/bin:"*) ;;
    *) export PATH="$PATH:$HOME/.local/bin" ;;
esac

### Screenshots

_No response_

### Distribution

Other (please specify below)

### If Other, specify

CachyOS

### Additional context

_No response_

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

tetam01 avatar Sep 05 '26 09:09 tetam01

Confirmed reproducible — thanks for the detailed report I can verify this bug in ~/.config/bashrc/00-init and ~/.config/zshrc/00-init: both append the cargo and local bin directories to $PATH without checking for duplicates:

export PATH=$PATH:~/.cargo/bin/
export PATH=$PATH:~/.local/bin/

On a normal login the rc file runs exactly once, so this goes unnoticed. But as soon as it is sourced again within the same session hierarchy (nested bash -c, Quickshell widgets, scripts that re-source the environment, desktop-session restarts without a full teardown), the same paths keep accumulating with no upper bound. A $PATH duplicated N times reaches hundreds of kilobytes on a long session, which hits the kernel ARG_MAX limit → E2BIG: Argument list too long when spawning processes — this breaks process creation in general, including compositor shortcuts.

Reproduction matches the report exactly. Check before/after:

echo $PATH | wc -c
for i in {1..50}; do bash -c 'true'; done
echo $PATH | wc -c   # ballooned: dozens of ~/.cargo/bin and ~/.local/bin copies

Proposed fix Make 00-init idempotent — append a path only when it is not already present.

Apply to both ~/.config/bashrc/00-init and ~/.config/zshrc/00-init:

# Add ~/.cargo/bin only if not already in PATH
case ":$PATH:" in
    *":$HOME/.cargo/bin:"*) ;;
    *) export PATH="$PATH:$HOME/.cargo/bin" ;;
esac

# Add ~/.local/bin only if not already in PATH
case ":$PATH:" in
    *":$HOME/.local/bin:"*) ;;
    *) export PATH="$PATH:$HOME/.local/bin" ;;
esac
Notes beyond the minimal edit:

Notes beyond the minimal edit:

Use $HOME instead of ~ — tilde expands fine in export PATH="$PATH:~/." for bash/zsh, but not in dash, sh -c, or tooling without tilde expansion. $HOME is more robust and behaves identically in both shells. Drop the trailing slash — use $HOME/.cargo/bin and not $HOME/.cargo/bin/. Otherwise the guard test and the stored value differ and the duplicate check silently fails, so the bug comes back. The tested and appended strings must match exactly. Optional — extract a helper if you want the pattern in one place:

pathmunge() {
    case ":$PATH:" in
        *":$1:"*) ;;
        *) export PATH="${PATH:+$PATH:}$1" ;;
    esac
}
pathmunge "$HOME/.cargo/bin"
pathmunge "$HOME/.local/bin"

Open a PR fixing both files (bashrc and zshrc), and verify idempotency with echo $PATH | wc -c staying stable across repeated bash -c 'true' invocations — that confirms the duplicate issue is gone and rules out E2BIG on re-source.

andfriden avatar Oct 05 '26 14:10 andfriden

Thank you for bringing this issue up. I will fix it accordingly. Will send you both a note when it's available in Rolling Release. Would be great if you could test then.

mylinuxforwork avatar Oct 05 '26 17:10 mylinuxforwork