diff --git a/src/utils/__tests__/claude-settings.test.ts b/src/utils/__tests__/claude-settings.test.ts index 855389d5e..14481fa42 100644 --- a/src/utils/__tests__/claude-settings.test.ts +++ b/src/utils/__tests__/claude-settings.test.ts @@ -21,6 +21,7 @@ import { getClaudeJsonPath, getClaudeSettingsPath, getExistingStatusLine, + getPackageCommandAvailability, getRefreshInterval, getRemoteControlStatus, getSandboxConfig, @@ -790,6 +791,34 @@ describe('isClaudeCodeVersionAtLeast', () => { }); }); +describe('getPackageCommandAvailability', () => { + const originalPath = process.env.PATH; + + afterEach(() => { + process.env.PATH = originalPath; + }); + + // Minimal Linux images (Fedora, UBI, Arch without the which package) have no `which` + it.skipIf(process.platform === 'win32')('finds package managers on PATH when there is no which command', () => { + const binDir = path.join(testClaudeConfigDir, 'bin'); + fs.mkdirSync(path.join(binDir, 'bunx'), { recursive: true }); + fs.writeFileSync(path.join(binDir, 'npm'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(binDir, 'npx'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(binDir, 'bun'), '#!/bin/sh\n', { mode: 0o644 }); + process.env.PATH = binDir; + vi.spyOn(childProcess, 'execFileSync').mockImplementation((command) => { + throw Object.assign(new Error(`spawnSync ${command} ENOENT`), { code: 'ENOENT' }); + }); + + expect(getPackageCommandAvailability()).toEqual({ + npm: true, + npx: true, + bun: false, + bunx: false + }); + }); +}); + describe('getVoiceConfig', () => { let testProjectDir = ''; diff --git a/src/utils/__tests__/executable-path.test.ts b/src/utils/__tests__/executable-path.test.ts index dfe18e63d..27b2b1174 100644 --- a/src/utils/__tests__/executable-path.test.ts +++ b/src/utils/__tests__/executable-path.test.ts @@ -1,10 +1,16 @@ +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; import { describe, expect, it } from 'vitest'; -import { resolveExecutable } from '../executable-path'; +import { + findAllOnPosixPath, + resolveExecutable +} from '../executable-path'; function windows(files: string[], env: NodeJS.ProcessEnv = { Path: 'C:\\Windows\\System32;C:\\Program Files\\Git\\cmd' }) { const present = new Set(files.map(file => file.toLowerCase())); @@ -59,3 +65,72 @@ describe('resolveExecutable', () => { expect(resolveExecutable('C:\\tools\\git.exe', windows([]))).toBe('C:\\tools\\git.exe'); }); }); + +function posix(executables: string[], pathValue: string) { + const present = new Set(executables); + return { + env: { PATH: pathValue }, + isExecutableFile: (candidate: string) => present.has(candidate) + }; +} + +describe('findAllOnPosixPath', () => { + it('lists every match in PATH order, as which -a does', () => { + const search = posix( + ['/usr/local/bin/ccstatusline', '/home/alice/.bun/bin/ccstatusline'], + '/home/alice/.bun/bin:/usr/bin:/usr/local/bin' + ); + + expect(findAllOnPosixPath('ccstatusline', search)).toEqual([ + '/home/alice/.bun/bin/ccstatusline', + '/usr/local/bin/ccstatusline' + ]); + }); + + it('lists a directory that PATH repeats once', () => { + expect(findAllOnPosixPath('npm', posix(['/usr/bin/npm'], '/usr/bin:/bin:/usr/bin'))).toEqual(['/usr/bin/npm']); + }); + + it('skips empty and relative PATH entries', () => { + expect(findAllOnPosixPath('npm', posix(['./npm', 'node_modules/.bin/npm', '/bin/npm'], '/usr/bin::node_modules/.bin:/bin'))).toEqual(['/bin/npm']); + }); + + it('finds nothing without a PATH', () => { + expect(findAllOnPosixPath('npm', { env: {}, isExecutableFile: () => true })).toEqual([]); + }); + + // A test or a caller may swap in a new env object; the lookup must see it + it.skipIf(process.platform === 'win32')('reads PATH from process.env as it is when called', () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-path-env-')); + const originalEnv = process.env; + try { + fs.writeFileSync(path.join(binDir, 'npm'), '#!/bin/sh\n', { mode: 0o755 }); + process.env = { ...originalEnv, PATH: binDir }; + + expect(findAllOnPosixPath('npm')).toEqual([path.join(binDir, 'npm')]); + } finally { + process.env = originalEnv; + fs.rmSync(binDir, { recursive: true, force: true }); + } + }); + + it.skipIf(process.platform === 'win32')('only lists files the user can run', () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-path-scan-')); + const originalPath = process.env.PATH; + try { + fs.writeFileSync(path.join(binDir, 'npm'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(binDir, 'bun'), '#!/bin/sh\n', { mode: 0o644 }); + fs.mkdirSync(path.join(binDir, 'bunx')); + fs.symlinkSync('npm', path.join(binDir, 'npx')); + process.env.PATH = binDir; + + expect(findAllOnPosixPath('npm')).toEqual([path.join(binDir, 'npm')]); + expect(findAllOnPosixPath('npx')).toEqual([path.join(binDir, 'npx')]); + expect(findAllOnPosixPath('bun')).toEqual([]); + expect(findAllOnPosixPath('bunx')).toEqual([]); + } finally { + process.env.PATH = originalPath; + fs.rmSync(binDir, { recursive: true, force: true }); + } + }); +}); diff --git a/src/utils/__tests__/global-command-resolution.test.ts b/src/utils/__tests__/global-command-resolution.test.ts index bb9fbd5c3..141023964 100644 --- a/src/utils/__tests__/global-command-resolution.test.ts +++ b/src/utils/__tests__/global-command-resolution.test.ts @@ -1,4 +1,7 @@ import * as childProcess from 'node:child_process'; +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; import { afterEach, describe, @@ -9,6 +12,7 @@ import { import { getCommandResolutionPaths, + getRealDirectory, inspectGlobalCommandResolution } from '../global-command-resolution'; import { @@ -16,7 +20,7 @@ import { getPackageManagerShellOptions } from '../package-manager-executable'; -function mockExecFileSync(responses: Record) { +function mockExecFileSync(responses: Record) { const spy = vi.spyOn(childProcess, 'execFileSync').mockImplementation((command, args) => { const key = `${command} ${(args as string[]).join(' ')}`; const response = responses[key]; @@ -25,6 +29,10 @@ function mockExecFileSync(responses: Record) { throw new Error(`Unexpected command: ${key}`); } + if (response instanceof Error) { + throw response; + } + return response; }); @@ -37,6 +45,27 @@ function mockExecFileSync(responses: Record) { return spy; } +// What running a program that isn't installed throws +function missingProgramError(command: string): Error { + return Object.assign(new Error(`spawnSync ${command} ENOENT`), { code: 'ENOENT' }); +} + +// What a program that runs and exits with status 1 throws, as `which` does when it finds nothing +function failedCommandError(command: string): Error { + return Object.assign(new Error(`Command failed: ${command}`), { status: 1 }); +} + +// macOS and Windows file systems ignore letter case by default; most Linux ones don't +function tempFileSystemIgnoresCase(): boolean { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-case-')); + try { + fs.mkdirSync(path.join(dir, 'probe')); + return fs.existsSync(path.join(dir, 'PROBE')); + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +} + describe('global command resolution', () => { afterEach(() => { vi.restoreAllMocks(); @@ -79,6 +108,43 @@ describe('global command resolution', () => { ]); }); + // Minimal Linux images (Fedora, UBI, Arch without the which package) have no `which` + it.skipIf(process.platform === 'win32')('looks through PATH itself when there is no which command', () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-path-')); + const originalPath = process.env.PATH; + try { + fs.writeFileSync(path.join(binDir, 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + process.env.PATH = `${path.join(binDir, 'missing')}:${binDir}`; + mockExecFileSync({ + 'which -a ccstatusline': missingProgramError('which'), + 'bun pm bin -g': `${binDir}\n` + }); + + const resolution = inspectGlobalCommandResolution('bun', { platform: 'linux' }); + + expect(resolution.resolvedPaths).toEqual([path.join(binDir, 'ccstatusline')]); + expect(resolution.warning).toBeNull(); + } finally { + process.env.PATH = originalPath; + fs.rmSync(binDir, { recursive: true, force: true }); + } + }); + + it.skipIf(process.platform === 'win32')('does not look through PATH itself when which runs and fails', () => { + const binDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-path-')); + const originalPath = process.env.PATH; + try { + fs.writeFileSync(path.join(binDir, 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + process.env.PATH = binDir; + mockExecFileSync({ 'which -a ccstatusline': failedCommandError('which -a ccstatusline') }); + + expect(getCommandResolutionPaths('ccstatusline', { platform: 'linux' })).toEqual([]); + } finally { + process.env.PATH = originalPath; + fs.rmSync(binDir, { recursive: true, force: true }); + } + }); + it('silences child stderr on best-effort probes so failures cannot leak to the terminal', () => { const execFileSyncSpy = mockExecFileSync({ 'which -a ccstatusline': '/home/alice/.bun/bin/ccstatusline\n', @@ -118,6 +184,75 @@ describe('global command resolution', () => { expect(resolution.warning).toContain('/usr/local/bin/ccstatusline'); }); + // Merged /usr (Ubuntu 22.04+, Debian 12): /bin links to usr/bin and PATH lists both + it.skipIf(process.platform === 'win32')('counts a PATH directory reached through a symlink once', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-merged-usr-')); + try { + fs.mkdirSync(path.join(root, 'usr', 'bin'), { recursive: true }); + fs.writeFileSync(path.join(root, 'usr', 'bin', 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + fs.symlinkSync('usr/bin', path.join(root, 'bin')); + mockExecFileSync({ + 'which -a ccstatusline': `${root}/usr/bin/ccstatusline\n${root}/bin/ccstatusline\n`, + 'npm prefix -g': `${root}/usr\n` + }); + + const resolution = inspectGlobalCommandResolution('npm', { platform: 'linux' }); + + expect(resolution.resolvedPaths).toEqual([`${root}/usr/bin/ccstatusline`, `${root}/bin/ccstatusline`]); + expect(resolution.warning).toBeNull(); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + + it.skipIf(process.platform === 'win32')('lists another install once and leaves out other spellings of the first match', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-merged-usr-')); + try { + fs.mkdirSync(path.join(root, 'usr', 'bin'), { recursive: true }); + fs.mkdirSync(path.join(root, '.bun', 'bin'), { recursive: true }); + fs.writeFileSync(path.join(root, 'usr', 'bin', 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(root, '.bun', 'bin', 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + fs.symlinkSync('usr/bin', path.join(root, 'bin')); + mockExecFileSync({ + 'which -a ccstatusline': `${root}/usr/bin/ccstatusline\n${root}/bin/ccstatusline\n${root}/.bun/bin/ccstatusline\n`, + 'npm prefix -g': `${root}/usr\n` + }); + + const resolution = inspectGlobalCommandResolution('npm', { platform: 'linux' }); + + expect(resolution.warning).toBe(`⚠ Multiple ccstatusline binaries are on PATH. Claude Code will run the first match: ${root}/usr/bin/ccstatusline.\nOther matches: ${root}/.bun/bin/ccstatusline`); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + + // Where letter case is ignored, PATH can spell one directory two ways + it.skipIf(!tempFileSystemIgnoresCase())('resolves a directory spelled in another letter case to one path', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-case-')); + try { + fs.mkdirSync(path.join(root, 'bin')); + + expect(getRealDirectory(path.join(root, 'Bin'))).toBe(getRealDirectory(path.join(root, 'bin'))); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + + // Bun's plain realpathSync already reports the on-disk case, so only the + // call itself shows that Node gets the same answer + it('resolves directories with the native realpath', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-case-')); + try { + const nativeSpy = vi.spyOn(fs.realpathSync, 'native'); + + getRealDirectory(root); + + expect(nativeSpy).toHaveBeenCalledWith(root); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + it('ignores transient bunx status line shims when resolving global commands', () => { mockExecFileSync({ 'which -a ccstatusline': '/var/folders/demo/T/bunx-501-ccstatusline@latest/node_modules/.bin/ccstatusline\n/Users/alice/.bun/bin/ccstatusline\n', diff --git a/src/utils/__tests__/global-package-manager.test.ts b/src/utils/__tests__/global-package-manager.test.ts index d7a065927..82e0b5adf 100644 --- a/src/utils/__tests__/global-package-manager.test.ts +++ b/src/utils/__tests__/global-package-manager.test.ts @@ -1,5 +1,7 @@ import * as childProcess from 'node:child_process'; import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; import { afterEach, describe, @@ -103,6 +105,88 @@ describe('global package manager inspection', () => { }); }); + // Merged /usr (Ubuntu 22.04+, Debian 12): /bin links to usr/bin and PATH lists both + it.skipIf(process.platform === 'win32')('counts a PATH directory reached through a symlink once', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-merged-usr-')); + try { + const packageDir = path.join(root, 'usr', 'lib', 'node_modules', 'ccstatusline'); + fs.mkdirSync(path.join(root, 'usr', 'bin'), { recursive: true }); + fs.mkdirSync(packageDir, { recursive: true }); + fs.writeFileSync(path.join(root, 'usr', 'bin', 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(packageDir, 'package.json'), '{"version":"2.2.13"}'); + fs.symlinkSync('usr/bin', path.join(root, 'bin')); + mockExecFileSync({ + 'which -a ccstatusline': `${root}/usr/bin/ccstatusline\n${root}/bin/ccstatusline\n`, + 'npm prefix -g': `${root}/usr\n`, + 'npm root -g': `${root}/usr/lib/node_modules\n` + }); + + const activeCommand = inspectActiveGlobalCommand({ + commandAvailability: { + npm: true, + bun: false + }, + platform: 'linux' + }); + + expect(activeCommand).toEqual({ + packageManager: 'npm', + resolvedPath: `${root}/usr/bin/ccstatusline`, + resolvedPaths: [ + `${root}/usr/bin/ccstatusline`, + `${root}/bin/ccstatusline` + ], + binDir: `${root}/usr/bin`, + version: '2.2.13', + warning: null + }); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + + it.skipIf(process.platform === 'win32')('lists another install once and leaves out other spellings of the active one', () => { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ccstatusline-merged-usr-')); + try { + const packageDir = path.join(root, 'usr', 'lib', 'node_modules', 'ccstatusline'); + fs.mkdirSync(path.join(root, 'usr', 'bin'), { recursive: true }); + fs.mkdirSync(path.join(root, '.bun', 'bin'), { recursive: true }); + fs.mkdirSync(packageDir, { recursive: true }); + fs.writeFileSync(path.join(root, 'usr', 'bin', 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(root, '.bun', 'bin', 'ccstatusline'), '#!/bin/sh\n', { mode: 0o755 }); + fs.writeFileSync(path.join(packageDir, 'package.json'), '{"version":"2.2.13"}'); + fs.symlinkSync('usr/bin', path.join(root, 'bin')); + mockExecFileSync({ + 'which -a ccstatusline': `${root}/usr/bin/ccstatusline\n${root}/bin/ccstatusline\n${root}/.bun/bin/ccstatusline\n`, + 'npm prefix -g': `${root}/usr\n`, + 'npm root -g': `${root}/usr/lib/node_modules\n` + }); + + const activeCommand = inspectActiveGlobalCommand({ + commandAvailability: { + npm: true, + bun: false + }, + platform: 'linux' + }); + + expect(activeCommand).toEqual({ + packageManager: 'npm', + resolvedPath: `${root}/usr/bin/ccstatusline`, + resolvedPaths: [ + `${root}/usr/bin/ccstatusline`, + `${root}/bin/ccstatusline`, + `${root}/.bun/bin/ccstatusline` + ], + binDir: `${root}/usr/bin`, + version: '2.2.13', + warning: `⚠ Multiple ccstatusline binaries are on PATH. Claude Code will run the first match: ${root}/usr/bin/ccstatusline.\nOther matches: ${root}/.bun/bin/ccstatusline` + }); + } finally { + fs.rmSync(root, { recursive: true, force: true }); + } + }); + it('warns about distinct POSIX bin directories that differ only in case', () => { mockExecFileSync({ 'which -a ccstatusline': '/home/alice/Bin/ccstatusline\n/home/alice/bin/ccstatusline\n', diff --git a/src/utils/claude-settings.ts b/src/utils/claude-settings.ts index 6397770c0..02da9a5d4 100644 --- a/src/utils/claude-settings.ts +++ b/src/utils/claude-settings.ts @@ -17,6 +17,7 @@ import { isCustomConfigPath, saveInstallationMetadata } from './config'; +import { getCommandResolutionPaths } from './global-command-resolution'; // Re-export for backward compatibility export type { ClaudeSettings }; @@ -249,9 +250,12 @@ export async function isInstalled(): Promise { } function isExecutableAvailable(executable: string): boolean { + if (process.platform !== 'win32') { + return getCommandResolutionPaths(executable).length > 0; + } + try { - const command = process.platform === 'win32' ? `where ${executable}` : `which ${executable}`; - execSync(command, { stdio: 'ignore', windowsHide: true }); + execSync(`where ${executable}`, { stdio: 'ignore', windowsHide: true }); return true; } catch { return false; diff --git a/src/utils/executable-path.ts b/src/utils/executable-path.ts index c5d0c6491..7cc218604 100644 --- a/src/utils/executable-path.ts +++ b/src/utils/executable-path.ts @@ -85,3 +85,53 @@ export function resolveExecutable(name: string, lookup: ExecutableLookup = DEFAU } return resolved; } + +// Outside Windows, the shell searches PATH and `which` lists what it finds. +// Minimal Linux images (Fedora, UBI, Arch without the which package) have no +// `which`, so lookups that ask it fall back to searching PATH here. + +export interface PosixPathSearch { + env: NodeJS.ProcessEnv; + isExecutableFile: (candidate: string) => boolean; +} + +function isExecutableFile(candidate: string): boolean { + try { + fs.accessSync(candidate, fs.constants.X_OK); + return fs.statSync(candidate).isFile(); + } catch { + return false; + } +} + +function defaultPosixPathSearch(): PosixPathSearch { + return { env: process.env, isExecutableFile }; +} + +/** + * Every file named `name` on PATH that the user can run, in PATH order: what + * `which -a` lists. The default search reads process.env when called, so an env + * object swapped in after this module loaded is still seen. + */ +export function findAllOnPosixPath(name: string, search: PosixPathSearch = defaultPosixPathSearch()): string[] { + const pathValue = search.env.PATH; + if (!pathValue) { + return []; + } + + const matches = new Set(); + for (const entry of pathValue.split(':')) { + // An empty or relative entry names a directory relative to wherever the + // TUI runs, which says nothing about what Claude Code runs; like the + // Windows lookup above, only absolute entries count + if (!path.posix.isAbsolute(entry)) { + continue; + } + const candidate = `${entry}/${name}`; + if (search.isExecutableFile(candidate)) { + matches.add(candidate); + } + } + + return Array.from(matches); +} diff --git a/src/utils/global-command-resolution.ts b/src/utils/global-command-resolution.ts index 1b9976020..310a71ae1 100644 --- a/src/utils/global-command-resolution.ts +++ b/src/utils/global-command-resolution.ts @@ -1,6 +1,8 @@ import { execFileSync } from 'node:child_process'; +import * as fs from 'node:fs'; import * as path from 'node:path'; +import { findAllOnPosixPath } from './executable-path'; import { getPackageManagerExecutable, getPackageManagerShellOptions @@ -75,8 +77,11 @@ export function getCommandResolutionPaths( }); return splitCommandOutput(output); - } catch { - return []; + } catch (error) { + // No `which` to ask: list what it would from PATH + return platform !== 'win32' && (error as NodeJS.ErrnoException).code === 'ENOENT' + ? findAllOnPosixPath(command) + : []; } } @@ -172,22 +177,37 @@ function getDirectoryName(filePath: string): string { : normalized.slice(0, lastSlashIndex); } -function getUniqueResolvedDirs(resolvedPaths: string[]): string[] { +// A directory with symlinks resolved, so two spellings of one, such as /bin and +// /usr/bin where /bin links to usr/bin (merged /usr), count as one install +// location. The path as given when it can't be resolved. The native call also +// returns the letter case on disk where case is ignored (macOS), as Bun's +// realpathSync does but Node's doesn't, so both runtimes count .../Bin and +// .../bin as one. +export function getRealDirectory(dir: string): string { + try { + return fs.realpathSync.native(dir); + } catch { + return dir; + } +} + +// The first listed path in each directory, comparing directories with symlinks +// resolved: one path per install, starting with the first match +function getOnePathPerDirectory(resolvedPaths: string[]): string[] { const seen = new Set(); - const dirs: string[] = []; + const paths: string[] = []; for (const resolvedPath of resolvedPaths) { - const dir = getDirectoryName(resolvedPath); - const comparableDir = normalizePathForComparison(dir); + const comparableDir = normalizePathForComparison(getRealDirectory(getDirectoryName(resolvedPath))); if (seen.has(comparableDir)) { continue; } seen.add(comparableDir); - dirs.push(dir); + paths.push(resolvedPath); } - return dirs; + return paths; } export function isPathInsideDir(filePath: string, dir: string): boolean { @@ -215,9 +235,9 @@ function getResolutionWarning( return '⚠ ccstatusline is not currently resolvable on PATH. Claude Code runs ccstatusline, so restart your shell or update PATH if it cannot launch.'; } - const resolvedDirs = getUniqueResolvedDirs(resolvedPaths); - if (resolvedDirs.length > 1) { - return `⚠ Multiple ccstatusline binaries are on PATH. Claude Code will run the first match: ${firstResolvedPath}.\nOther matches: ${formatPathList(resolvedPaths.slice(1))}`; + const installPaths = getOnePathPerDirectory(resolvedPaths); + if (installPaths.length > 1) { + return `⚠ Multiple ccstatusline binaries are on PATH. Claude Code will run the first match: ${firstResolvedPath}.\nOther matches: ${formatPathList(installPaths.slice(1))}`; } if (expectedBinDir && !isPathInsideDir(firstResolvedPath, expectedBinDir)) { diff --git a/src/utils/global-package-manager.ts b/src/utils/global-package-manager.ts index 8fb0999f2..f6d04312d 100644 --- a/src/utils/global-package-manager.ts +++ b/src/utils/global-package-manager.ts @@ -9,6 +9,7 @@ import { getCommandResolutionPaths, getExpectedGlobalBinDir, getPersistentCommandResolutionPaths, + getRealDirectory, isPathInsideDir, type GlobalPackageManager } from './global-command-resolution'; @@ -139,22 +140,23 @@ function getComparablePath(filePath: string): string { : normalized; } -function getUniqueResolvedDirs(resolvedPaths: string[]): string[] { +// The first listed path in each directory, comparing directories with symlinks +// resolved: one path per install, starting with the active one +function getOnePathPerDirectory(resolvedPaths: string[]): string[] { const seen = new Set(); - const dirs: string[] = []; + const paths: string[] = []; for (const resolvedPath of resolvedPaths) { - const dir = getDirectoryName(resolvedPath); - const comparableDir = getComparablePath(dir); + const comparableDir = getComparablePath(getRealDirectory(getDirectoryName(resolvedPath))); if (seen.has(comparableDir)) { continue; } seen.add(comparableDir); - dirs.push(dir); + paths.push(resolvedPath); } - return dirs; + return paths; } function formatPathList(paths: string[]): string { @@ -306,9 +308,9 @@ function getActiveResolutionWarning( return '⚠ ccstatusline is not currently resolvable on PATH. Claude Code runs ccstatusline, so restart your shell or update PATH if it cannot launch.'; } - const resolvedDirs = getUniqueResolvedDirs(resolvedPaths); - if (resolvedDirs.length > 1) { - return `⚠ Multiple ccstatusline binaries are on PATH. Claude Code will run the first match: ${active.resolvedPath}.\nOther matches: ${formatPathList(resolvedPaths.slice(1))}`; + const installPaths = getOnePathPerDirectory(resolvedPaths); + if (installPaths.length > 1) { + return `⚠ Multiple ccstatusline binaries are on PATH. Claude Code will run the first match: ${active.resolvedPath}.\nOther matches: ${formatPathList(installPaths.slice(1))}`; } if (active.packageManager === 'unknown') {