Skip to content

Commit 030144f

Browse files
committed
fix(filesystem): let a list_directory_with_sizes stat failure reach the caller
The per-entry `fs.stat` had a catch that substituted `size: 0` and `mtime: new Date(0)`. `formatSize` renders 0 as "0 B", so an entry whose size could not be read came out identical to an empty file, and the fabricated 0 was added to the combined size the tool reports. A stat failure is not a measurement, so let it reject. `list_directory` and `directory_tree` already let their `readdir` reject, and `search_files` now does the same for its own recursion.
1 parent f46d957 commit 030144f

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

‎src/filesystem/__tests__/structured-content.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ describe('structuredContent schema compliance', () => {
9696
// The content should contain directory listing info
9797
expect(structuredContent.content).toContain('[FILE]');
9898
});
99+
100+
// A dangling symlink is listed by readdir but `fs.stat` cannot resolve it,
101+
// so this is the real path to a failing stat. Reporting the entry as size 0
102+
// makes it indistinguishable from an empty file -- `formatSize` renders both
103+
// as "0 B" -- and adds a fabricated 0 to the combined size.
104+
it('should reject when an entry cannot be stat-ed rather than report it as 0 bytes', async () => {
105+
await fs.symlink(path.join(testDir, 'no-such-target'), path.join(testDir, 'dangling'));
106+
107+
const result = await client.callTool({
108+
name: 'list_directory_with_sizes',
109+
arguments: { path: testDir }
110+
});
111+
112+
expect(result.isError).toBe(true);
113+
const content = (result.content as Array<{ type: string; text: string }>).map(c => c.text).join('\n');
114+
expect(content).toMatch(/ENOENT|dangling/);
115+
expect(content).not.toMatch(/dangling\s+0 B/);
116+
});
99117
});
100118

101119
describe('move_file', () => {

‎src/filesystem/index.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -485,25 +485,21 @@ server.registerTool(
485485
const entries = await fs.readdir(validPath, { withFileTypes: true });
486486

487487
// Get detailed information for each entry
488+
//
489+
// A stat that fails is not an entry of size 0: `formatSize` renders that
490+
// as "0 B", so an unreadable file and an empty one come out identical, and
491+
// the entry's 0 is added to the combined size below. Let it reject, the
492+
// way `list_directory` and `directory_tree` already let `readdir` reject.
488493
const detailedEntries = await Promise.all(
489494
entries.map(async (entry) => {
490495
const entryPath = path.join(validPath, entry.name);
491-
try {
492-
const stats = await fs.stat(entryPath);
493-
return {
494-
name: entry.name,
495-
isDirectory: entry.isDirectory(),
496-
size: stats.size,
497-
mtime: stats.mtime
498-
};
499-
} catch (error) {
500-
return {
501-
name: entry.name,
502-
isDirectory: entry.isDirectory(),
503-
size: 0,
504-
mtime: new Date(0)
505-
};
506-
}
496+
const stats = await fs.stat(entryPath);
497+
return {
498+
name: entry.name,
499+
isDirectory: entry.isDirectory(),
500+
size: stats.size,
501+
mtime: stats.mtime
502+
};
507503
})
508504
);
509505

0 commit comments

Comments
 (0)