Skip to content

Commit b7d82aa

Browse files
committed
fix(completion): resolve CLI package root correctly for tab completion script
Fixes "Tab Completion script not found" error when running via npx/dlx. Previously used rootPath which resolved to node_modules/@SocketSecurity instead of node_modules/@socketsecurity/cli, causing the script lookup to fail with path missing /cli directory. Now uses require.resolve('@socketsecurity/cli/package.json') to find the actual CLI package root, with fallback to relative path for development.
1 parent 34a2689 commit b7d82aa

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/cli/src/commands/install/setup-tab-completion.mts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import fs from 'node:fs'
2+
import { createRequire } from 'node:module'
23
import path from 'node:path'
4+
import { fileURLToPath } from 'node:url'
35

46
import { debug } from '@socketsecurity/lib/debug'
57
import { safeMkdirSync } from '@socketsecurity/lib/fs'
68

79
import ENV from '../../constants/env.mts'
8-
import { homePath, rootPath } from '../../constants/paths.mts'
10+
import { homePath } from '../../constants/paths.mts'
911
import { getBashrcDetails } from '../../utils/cli/completion.mjs'
1012

13+
const __filename = fileURLToPath(import.meta.url)
14+
const __dirname = path.dirname(__filename)
15+
const require = createRequire(import.meta.url)
16+
1117
import type { CResult } from '../../types.mts'
1218

1319
export async function setupTabCompletion(targetName: string): Promise<
@@ -79,7 +85,17 @@ export async function setupTabCompletion(targetName: string): Promise<
7985
}
8086

8187
function getTabCompletionScriptRaw(): CResult<string> {
82-
const sourcePath = path.join(rootPath, 'data', 'socket-completion.bash')
88+
// Resolve the @socketsecurity/cli package root to find the data directory.
89+
// This works whether running from source, installed globally, or via npx/dlx.
90+
let sourcePath: string
91+
try {
92+
const cliPackageJson = require.resolve('@socketsecurity/cli/package.json')
93+
const cliPackageRoot = path.dirname(cliPackageJson)
94+
sourcePath = path.join(cliPackageRoot, 'data', 'socket-completion.bash')
95+
} catch {
96+
// Fallback for development: look relative to this file.
97+
sourcePath = path.resolve(__dirname, '../../../data/socket-completion.bash')
98+
}
8399

84100
if (!fs.existsSync(sourcePath)) {
85101
return {

0 commit comments

Comments
 (0)