-
-
Notifications
You must be signed in to change notification settings - Fork 734
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 1 commit
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 |
|---|---|---|
|
|
@@ -677,6 +677,19 @@ export async function listDirectory(dirPath: string, depth: number = 2): Promise | |
|
|
||
| const MAX_NESTED_ITEMS = 100; // Maximum items to show per nested directory | ||
|
|
||
| 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}`); | ||
| } | ||
| 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,6 +698,9 @@ 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; | ||
| if (isTopLevel && (err.code === 'ENOENT' || err.code === 'ENOTDIR')) { | ||
| throw new Error(`Directory not found: ${dirPath}`); | ||
| } | ||
| 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. | ||
|
|
@@ -1010,4 +1026,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,61 @@ | ||
| 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 = path.join(os.tmpdir(), 'desktop-commander-list-directory-errors'); | ||
|
|
||
| async function setup() { | ||
| const originalConfig = await configManager.getConfig(); | ||
| await fs.mkdir(TEST_ROOT, { recursive: true }); | ||
| await configManager.setValue('allowedDirectories', [TEST_ROOT]); | ||
| return originalConfig; | ||
| } | ||
|
|
||
| async function teardown(originalConfig) { | ||
| await configManager.updateConfig(originalConfig); | ||
| await fs.rm(TEST_ROOT, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| async function testMissingDirectoryReturnsNotFound() { | ||
| const missingDir = path.join(TEST_ROOT, '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'); | ||
| } | ||
|
|
||
| export default async function runTests() { | ||
| let originalConfig; | ||
| try { | ||
| originalConfig = await setup(); | ||
| await testMissingDirectoryReturnsNotFound(); | ||
| 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) { | ||
| await teardown(originalConfig); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (import.meta.url === `file://${process.argv[1]}`) { | ||
| runTests().then((ok) => { | ||
| process.exit(ok ? 0 : 1); | ||
| }); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.