Skip to content

fix(npm-globals): Prune stale Bun globals - #1448

Merged
shunkakinoki merged 1 commit into
mainfrom
shunkakinoki/fix/prune-stale-bun-globals
Apr 12, 2026
Merged

fix(npm-globals): Prune stale Bun globals#1448
shunkakinoki merged 1 commit into
mainfrom
shunkakinoki/fix/prune-stale-bun-globals

Conversation

@shunkakinoki

@shunkakinoki shunkakinoki commented Apr 12, 2026

Copy link
Copy Markdown
Owner

Prune stale Bun globals when syncing npm globals.

The npm-global installer only added or updated packages listed in dotfiles/package.json, so packages removed from that manifest could stay behind in ~/.bun/install/global and keep stale shims like ~/.bun/bin/bd alive. That left Bun state inconsistent and could shadow the repo-local bd binary until manually deleted.

This change prunes Bun global dependencies that are no longer declared, falls back to removing stale metadata if Bun leaves it behind, and sweeps dangling shims from ~/.bun/bin. It also adds a shell regression spec covering the stale @beads/bd case.


Summary by cubic

Prunes stale Bun globals during npm-global sync to keep the PATH clean and prevent repo binaries from being shadowed. Removes undeclared packages and dangling shims, covering the @beads/bd case.

  • Bug Fixes
    • Remove globals not declared in dotfiles (runs bun remove, prunes leftover metadata, deletes modules).
    • Sweep dangling shims in ~/.bun/bin to clear broken commands.
    • Add shell regression spec for @beads/bd.

Written for commit 257dc0a. Summary will update on new commits.

Remove packages from Bun global state when they are no longer declared in dotfiles/package.json instead of only installing missing packages.

Also clean dangling shims in ~/.bun/bin and cover the stale @beads/bd case with shell specs so the local repo binary remains the only bd on PATH.

Co-authored-by: Codex <noreply@openai.com>
@mesa-dot-dev

mesa-dot-dev Bot commented Apr 12, 2026

Copy link
Copy Markdown

You do not have enough credits to review this pull request. Please purchase more credits to continue.

@coderabbitai

coderabbitai Bot commented Apr 12, 2026

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b2aa7294-1b55-4742-aca9-1843ff958584

📥 Commits

Reviewing files that changed from the base of the PR and between 376c8a1 and 257dc0a.

📒 Files selected for processing (2)
  • home-manager/modules/npm-globals/install-npm-globals.sh
  • spec/npm_globals_spec.sh

Disabled knowledge base sources:

  • Linear integration is disabled

You can enable these sources in your CodeRabbit configuration.


📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • New Features

    • Added automatic cleanup of stale global package dependencies no longer present in your configuration
    • Removal of orphaned package manager shim files from your system
  • Tests

    • Added comprehensive test suite to validate global package cleanup functionality

Walkthrough

This PR adds logic to remove stale Bun global packages no longer present in the dotfiles package.json file, including cleanup of corresponding entries from the Bun global package registry and removal of dangling symlink shims, while reorganizing variable assignment order in the install script.

Changes

Cohort / File(s) Summary
Stale Bun Global Pruning
home-manager/modules/npm-globals/install-npm-globals.sh
Introduces computation and removal of stale Bun global dependencies by deleting entries from ~/.bun/install/global/package.json, removing associated node_modules directories, and cleaning up dangling symlinks under ~/.bun/bin. Reorganizes script to assign GLOBAL_PKG earlier before stale-package removal logic executes.
Pruning Test Coverage
spec/npm_globals_spec.sh
Adds new test suite section validating stale Bun global package removal, including mocked bun remove command behavior, verification that stale package entries are deleted from the global package.json, and confirmation that corresponding shim files are removed.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 Hop, skip, and clean those symlinks away!
Stale packages banished, fresh starts today,
Dangling shims removed without delay,
Bun's globals pruned in the most tidy way! 🌿✨

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch shunkakinoki/fix/prune-stale-bun-globals

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mesa-dot-dev

mesa-dot-dev Bot commented Apr 12, 2026

Copy link
Copy Markdown

Mesa Description

TL;DR

Fixes the npm-global installer to prune stale Bun global packages and shims, resolving inconsistencies and preventing shadowed binaries.

What changed?

  • Modified the npm-global installer to prune undeclared Bun global dependencies.
  • Added logic to remove stale Bun metadata if left behind.
  • Implemented sweeping of dangling shims from ~/.bun/bin.
  • Added a shell regression spec to cover the stale @beads/bd case.

Description generated by Mesa. Update settings

@shunkakinoki
shunkakinoki merged commit 16532f4 into main Apr 12, 2026
29 of 30 checks passed
@shunkakinoki
shunkakinoki deleted the shunkakinoki/fix/prune-stale-bun-globals branch April 12, 2026 12:17

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces logic to prune stale global packages and dangling shims in the Bun environment. It adds a process to compare installed global packages against the dependencies declared in the configuration's package.json, removing any that are no longer required. Additionally, it implements a cleanup step for broken symbolic links in the Bun binary directory and includes comprehensive tests for these new features. A review comment suggests improving the robustness of the shim cleanup loop by using IFS= read -r to correctly handle filenames that may contain whitespace.

# Remove stale shims left behind by prior Bun global installs
BUN_BIN="${HOME}/.bun/bin"
if [ -d "$BUN_BIN" ]; then
find "$BUN_BIN" -mindepth 1 -maxdepth 1 -type l 2>/dev/null | while read -r shim; do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To robustly parse the output of the find command and avoid issues with filenames that might contain leading or trailing whitespace, you should use IFS= read -r. This aligns with the general rule for robustly parsing command output and maintains consistency with the while loop used earlier in the script (line 57).

Suggested change
find "$BUN_BIN" -mindepth 1 -maxdepth 1 -type l 2>/dev/null | while read -r shim; do
find "$BUN_BIN" -mindepth 1 -maxdepth 1 -type l 2>/dev/null | while IFS= read -r shim; do
References
  1. To robustly parse command output in shell scripts, use a unique delimiter (e.g., tab) in the format string and read with a matching IFS. This is safer than splitting by spaces with cut, especially when data fields might contain spaces.

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.

1 participant