diff --git a/home-manager/modules/npm-globals/install-npm-globals.sh b/home-manager/modules/npm-globals/install-npm-globals.sh index d717f0515..858f861b8 100755 --- a/home-manager/modules/npm-globals/install-npm-globals.sh +++ b/home-manager/modules/npm-globals/install-npm-globals.sh @@ -62,4 +62,19 @@ if [ -n "$OVERRIDES" ]; then fi fi +# Deduplicate overridden packages from nested node_modules +# Bun can install the same package at both top-level and nested locations. +# When packages use Symbols (like pino), two copies create incompatible +# instances. Remove nested copies of any overridden package so everything +# resolves to the single top-level version. +GLOBAL_MODULES="${HOME}/.bun/install/global/node_modules" +for pkg in $(echo "$OVERRIDES" | jq -r 'keys[]' 2>/dev/null); do + find "$GLOBAL_MODULES" -mindepth 3 -maxdepth 4 -type d -name "$pkg" \ + -path "*/node_modules/$pkg" \ + ! -path "$GLOBAL_MODULES/$pkg" 2>/dev/null | while read -r nested; do + rm -r "$nested" + echo "Deduplicated nested $pkg: $nested" + done +done + echo "npm globals installation complete" diff --git a/spec/npm_globals_spec.sh b/spec/npm_globals_spec.sh index 273c92002..7943079e7 100644 --- a/spec/npm_globals_spec.sh +++ b/spec/npm_globals_spec.sh @@ -86,5 +86,15 @@ It 'runs bun install in global dir after applying overrides' When run bash -c "grep 'cd.*bun/install/global.*bun install' '$SCRIPT'" The output should include 'bun install' End + +It 'deduplicates nested copies of overridden packages' +When run bash -c "grep 'Deduplicated nested' '$SCRIPT'" +The output should include 'Deduplicated' +End + +It 'finds nested node_modules with find command' +When run bash -c "grep 'find.*GLOBAL_MODULES.*node_modules' '$SCRIPT'" +The output should include 'find' +End End End