-
-
Notifications
You must be signed in to change notification settings - Fork 736
fix: return not-found for missing directories #454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -676,6 +676,35 @@ export async function listDirectory(dirPath: string, depth: number = 2): Promise | |
| const results: string[] = []; | ||
|
|
||
| const MAX_NESTED_ITEMS = 100; // Maximum items to show per nested directory | ||
| const isAccessDeniedError = (err: NodeJS.ErrnoException) => | ||
| err.code === 'EPERM' || err.code === 'EACCES' || err.code === 'ETIMEDOUT'; | ||
|
|
||
| function addDeniedEntry(displayPath: string, err: NodeJS.ErrnoException): void { | ||
| // Keep [DENIED] prefix so UI parser regex still matches. | ||
| // Append a hint for permission/timeout errors so user gets context. | ||
| if (isAccessDeniedError(err)) { | ||
| results.push(`[DENIED] ${displayPath} — not accessible (permission denied, cloud-only file, or Full Disk Access not granted)`); | ||
| } else { | ||
| results.push(`[DENIED] ${displayPath}`); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const stats = await fs.stat(validPath); | ||
| if (!stats.isDirectory()) { | ||
| throw new Error(`Path is not a directory: ${dirPath}`); | ||
| } | ||
| } catch (error) { | ||
| const err = error as NodeJS.ErrnoException; | ||
| if (err.code === 'ENOENT' || err.code === 'ENOTDIR') { | ||
| throw new Error(`Directory not found: ${dirPath}`); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
| if (isAccessDeniedError(err)) { | ||
| addDeniedEntry(path.basename(validPath), err); | ||
| return results; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| throw error; | ||
| } | ||
|
Comment on lines
+689
to
+704
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: The new top-level Severity Level: Major
|
||
|
|
||
| async function listRecursive(currentPath: string, currentDepth: number, relativePath: string = '', isTopLevel: boolean = true): Promise<void> { | ||
| if (currentDepth <= 0) return; | ||
|
|
@@ -685,14 +714,11 @@ export async function listDirectory(dirPath: string, depth: number = 2): Promise | |
| entries = await fs.readdir(currentPath, { withFileTypes: true }); | ||
| } catch (error) { | ||
| const err = error as NodeJS.ErrnoException; | ||
| const displayPath = relativePath || path.basename(currentPath); | ||
| // Keep [DENIED] prefix so UI parser regex still matches. | ||
| // Append a hint for permission/timeout errors so user gets context. | ||
| if (err.code === 'EPERM' || err.code === 'EACCES' || err.code === 'ETIMEDOUT') { | ||
| results.push(`[DENIED] ${displayPath} — not accessible (permission denied, cloud-only file, or Full Disk Access not granted)`); | ||
| } else { | ||
| results.push(`[DENIED] ${displayPath}`); | ||
| if (isTopLevel && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) { | ||
| throw new Error(`Directory not found: ${dirPath}`); | ||
| } | ||
| const displayPath = relativePath || path.basename(currentPath); | ||
| addDeniedEntry(displayPath, err); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -1010,4 +1036,4 @@ export async function writePdf( | |
| } else { | ||
| throw new Error('Invalid content type for writePdf. Expected string (markdown) or array of operations.'); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import assert from 'assert'; | ||
| import fs from 'fs/promises'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
|
|
||
| import { configManager } from '../dist/config-manager.js'; | ||
| import { listDirectory } from '../dist/tools/filesystem.js'; | ||
|
|
||
| const TEST_ROOT_PREFIX = path.join(os.tmpdir(), 'desktop-commander-list-directory-errors-'); | ||
|
|
||
| async function setup() { | ||
| const originalConfig = await configManager.getConfig(); | ||
| const testRoot = await fs.realpath(await fs.mkdtemp(TEST_ROOT_PREFIX)); | ||
| await configManager.setValue('allowedDirectories', [testRoot]); | ||
| return { originalConfig, testRoot }; | ||
| } | ||
|
|
||
| async function teardown(originalConfig, testRoot) { | ||
| await configManager.updateConfig(originalConfig); | ||
| await fs.rm(testRoot, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| async function testMissingDirectoryReturnsNotFound(testRoot) { | ||
| const missingDir = path.join(testRoot, 'missing-dir'); | ||
|
|
||
| await assert.rejects( | ||
| listDirectory(missingDir, 1), | ||
| (error) => { | ||
| assert(error instanceof Error); | ||
| assert(error.message.includes(`Directory not found: ${missingDir}`)); | ||
| assert(!error.message.includes('[DENIED]')); | ||
| return true; | ||
| }, | ||
| 'Missing directories should raise a not-found error instead of returning [DENIED]', | ||
| ); | ||
|
|
||
| console.log('✓ missing directory returns not-found error'); | ||
| } | ||
|
|
||
| async function testTopLevelStatAccessDeniedReturnsDeniedEntry(testRoot) { | ||
| const deniedDir = path.join(testRoot, 'stat-denied'); | ||
| await fs.mkdir(deniedDir); | ||
|
|
||
| const originalStat = fs.stat; | ||
| fs.stat = async (target, ...args) => { | ||
| if (path.resolve(String(target)) === deniedDir) { | ||
| const error = new Error(`EACCES: permission denied, stat '${deniedDir}'`); | ||
| error.code = 'EACCES'; | ||
| throw error; | ||
| } | ||
| return originalStat.call(fs, target, ...args); | ||
| }; | ||
|
|
||
| try { | ||
| const entries = await listDirectory(deniedDir, 1); | ||
| assert.strictEqual(entries.length, 1); | ||
| assert(entries[0].startsWith('[DENIED] stat-denied'), 'Top-level stat access errors should keep [DENIED] output'); | ||
| assert(entries[0].includes('not accessible'), 'Permission-like stat errors should include the access hint'); | ||
| console.log('✓ top-level stat access error returns denied entry'); | ||
| } finally { | ||
| fs.stat = originalStat; | ||
| } | ||
| } | ||
|
|
||
| export default async function runTests() { | ||
| let originalConfig; | ||
| let testRoot; | ||
| try { | ||
| ({ originalConfig, testRoot } = await setup()); | ||
| await testMissingDirectoryReturnsNotFound(testRoot); | ||
| await testTopLevelStatAccessDeniedReturnsDeniedEntry(testRoot); | ||
| console.log('\n✅ list_directory error tests passed!'); | ||
| return true; | ||
| } catch (error) { | ||
| console.error('❌ list_directory error test failed:', error instanceof Error ? error.message : String(error)); | ||
| return false; | ||
| } finally { | ||
| if (originalConfig && testRoot) { | ||
| await teardown(originalConfig, testRoot); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (import.meta.url === `file://${process.argv[1]}`) { | ||
| runTests().then((ok) => { | ||
| process.exit(ok ? 0 : 1); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep
[DENIED]entries path-only to preserve downstream compatibility.Line 686 changes denied entries from
[DENIED] <path>to[DENIED] <path> — .... That mutates the path payload and can break consumers that parse the post-prefix content as a path (including UI tree parsing). It also conflicts with the stated goal of preserving existing denied output format.Suggested fix
function addDeniedEntry(displayPath: string, err: NodeJS.ErrnoException): void { - // Keep [DENIED] prefix so UI parser regex still matches. - // Append a hint for permission/timeout errors so user gets context. - if (isAccessDeniedError(err)) { - results.push(`[DENIED] ${displayPath} — not accessible (permission denied, cloud-only file, or Full Disk Access not granted)`); - } else { - results.push(`[DENIED] ${displayPath}`); - } + results.push(`[DENIED] ${displayPath}`); }🤖 Prompt for AI Agents