Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion cli/src/__tests__/path-completion.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
import {
mkdirSync,
mkdtempSync,
rmSync,
symlinkSync,
writeFileSync,
} from 'fs'
import os from 'os'
import path from 'path'

Expand Down Expand Up @@ -239,5 +245,17 @@ describe('getPathCompletion', () => {
// Files should be ignored, common prefix from directories only
expect(result).toBe(path.join(tempDir, 'project-'))
})

test('completes symlinks pointing to directories', () => {
const targetDir = path.join(tempDir, 'actual-dir')
mkdirSync(targetDir)
try {
symlinkSync(targetDir, path.join(tempDir, 'symlinked-dir'))
const result = getPathCompletion(path.join(tempDir, 'sym'))
expect(result).toBe(path.join(tempDir, 'symlinked-dir') + path.sep)
} catch {
// Skip on systems where symlink creation is unprivileged/unsupported
}
})
})
})
27 changes: 17 additions & 10 deletions cli/src/utils/path-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,29 @@ export function getPathCompletion(inputPath: string): string | null {

// List directories in parent that match the partial
try {
const items = readdirSync(parentDir)
const entries = readdirSync(parentDir, { withFileTypes: true })
const matches: string[] = []

for (const item of items) {
for (const entry of entries) {
const name = entry.name
// Skip hidden files unless user typed a dot
if (item.startsWith('.') && !partial.startsWith('.')) continue
if (name.startsWith('.') && !partial.startsWith('.')) continue

const fullPath = path.join(parentDir, item)
try {
if (!statSync(fullPath).isDirectory()) continue
} catch {
continue
// Filter by prefix first before doing any directory resolution
if (!name.toLowerCase().startsWith(partial)) continue

let isDirectory = entry.isDirectory()
if (!isDirectory && entry.isSymbolicLink()) {
try {
const fullPath = path.join(parentDir, name)
isDirectory = statSync(fullPath).isDirectory()
} catch {
isDirectory = false
}
}

if (item.toLowerCase().startsWith(partial)) {
matches.push(item)
if (isDirectory) {
matches.push(name)
}
}

Expand Down
Loading