fix(linux): add user bin directories to systemd service PATH for skill installation (#1512)

* fix(linux): add user bin directories to systemd service PATH

Fixes #1503

On Linux, the systemd service PATH was hardcoded to only include system
directories (/usr/local/bin, /usr/bin, /bin), causing binaries installed
via npm global with custom prefix or node version managers to not be found.

This adds common Linux user bin directories to the PATH:
- ~/.local/bin (XDG standard, pip, etc.)
- ~/.npm-global/bin (npm custom prefix)
- ~/bin (user's personal bin)
- Node version manager paths (nvm, fnm, volta, asdf)
- ~/.local/share/pnpm (pnpm global)
- ~/.bun/bin (Bun)

User directories are added before system directories so user-installed
binaries take precedence.

🤖 AI-assisted (Claude Opus 4.5 via Clawdbot)
📋 Testing: Existing unit tests pass (7/7)

* test: add comprehensive tests for Linux user bin directory resolution

- Add dedicated tests for resolveLinuxUserBinDirs() function
- Test path ordering (extraDirs > user dirs > system dirs)
- Test buildMinimalServicePath() with HOME set/unset
- Test platform-specific behavior (Linux vs macOS vs Windows)

Test count: 7 → 20 (+13 tests)

* test: add comprehensive tests for Linux user bin directory handling

- Test Linux user directories included when HOME is set
- Test Linux user directories excluded when HOME is missing
- Test path ordering (extraDirs > user dirs > system dirs)
- Test platform-specific behavior (Linux vs macOS vs Windows)
- Test buildMinimalServicePath() with HOME in env

Covers getMinimalServicePathParts() and buildMinimalServicePath()
for all Linux user bin directory edge cases.

Test count: 7 → 16 (+9 tests)
This commit is contained in:
Robby
2026-01-23 20:06:14 +01:00
committed by GitHub
parent cad7ed1cb8
commit 3d958d5466
2 changed files with 170 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ import {
export type MinimalServicePathOptions = {
platform?: NodeJS.Platform;
extraDirs?: string[];
home?: string;
};
type BuildServicePathOptions = MinimalServicePathOptions & {
@@ -33,6 +34,31 @@ function resolveSystemPathDirs(platform: NodeJS.Platform): string[] {
return [];
}
/**
* Resolve common user bin directories for Linux.
* These are paths where npm global installs and node version managers typically place binaries.
*/
export function resolveLinuxUserBinDirs(home: string | undefined): string[] {
if (!home) return [];
const dirs: string[] = [];
// Common user bin directories
dirs.push(`${home}/.local/bin`); // XDG standard, pip, etc.
dirs.push(`${home}/.npm-global/bin`); // npm custom prefix (recommended for non-root)
dirs.push(`${home}/bin`); // User's personal bin
// Node version managers
dirs.push(`${home}/.nvm/current/bin`); // nvm with current symlink
dirs.push(`${home}/.fnm/current/bin`); // fnm
dirs.push(`${home}/.volta/bin`); // Volta
dirs.push(`${home}/.asdf/shims`); // asdf
dirs.push(`${home}/.local/share/pnpm`); // pnpm global bin
dirs.push(`${home}/.bun/bin`); // Bun
return dirs;
}
export function getMinimalServicePathParts(options: MinimalServicePathOptions = {}): string[] {
const platform = options.platform ?? process.platform;
if (platform === "win32") return [];
@@ -41,12 +67,17 @@ export function getMinimalServicePathParts(options: MinimalServicePathOptions =
const extraDirs = options.extraDirs ?? [];
const systemDirs = resolveSystemPathDirs(platform);
// Add Linux user bin directories (npm global, nvm, fnm, volta, etc.)
const linuxUserDirs = platform === "linux" ? resolveLinuxUserBinDirs(options.home) : [];
const add = (dir: string) => {
if (!dir) return;
if (!parts.includes(dir)) parts.push(dir);
};
for (const dir of extraDirs) add(dir);
// User dirs first so user-installed binaries take precedence
for (const dir of linuxUserDirs) add(dir);
for (const dir of systemDirs) add(dir);
return parts;
@@ -59,7 +90,10 @@ export function buildMinimalServicePath(options: BuildServicePathOptions = {}):
return env.PATH ?? "";
}
return getMinimalServicePathParts(options).join(path.delimiter);
return getMinimalServicePathParts({
...options,
home: options.home ?? env.HOME,
}).join(path.delimiter);
}
export function buildServiceEnvironment(params: {