Skip to content
Draft
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
12 changes: 3 additions & 9 deletions packages/cli/src/lib/init/tools/list-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ type WalkState = {
truncated: boolean;
};

async function readDirEntries(dir: string): Promise<fs.Dirent[]> {
try {
return await fs.promises.readdir(dir, { withFileTypes: true });
} catch {
return [];
}
}

function omissionReason(
entry: fs.Dirent,
state: WalkState,
Expand Down Expand Up @@ -147,7 +139,9 @@ async function walkDirectory(
return;
}

for (const entry of await readDirEntries(dir)) {
for (const entry of await fs.promises.readdir(dir, {
withFileTypes: true,
})) {
if (state.entries.length >= state.maxEntries) {
state.truncated = true;
return;
Expand Down
15 changes: 15 additions & 0 deletions packages/cli/test/lib/init/tools/filesystem-tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ describe("filesystem tools", () => {
expect(result.error).toContain("outside project directory");
});

test("returns a failed tool result when a directory cannot be read", async () => {
const result = await executeTool(
{
type: "tool",
operation: "list-dir",
cwd: testDir,
params: { path: "does-not-exist" },
},
makeContext(testDir)
);

expect(result.ok).toBe(false);
expect(result.error).toContain("ENOENT");
});

test("lists and precomputes directory contents", async () => {
fs.writeFileSync(path.join(testDir, "index.ts"), "export {};\n");
fs.mkdirSync(path.join(testDir, "src"));
Expand Down
11 changes: 4 additions & 7 deletions packages/cli/test/lib/init/tools/list-dir.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,9 @@ describe("listDir", () => {
}
});

test("returns empty entries for a missing subpath", async () => {
// `safePath` allows nonexistent paths under the sandbox; `readdir`
// throws, which the walker swallows.
const entries = entriesOf(
await listDir(makePayload(testDir, { path: "does-not-exist" }))
);
expect(entries).toEqual([]);
test("surfaces readdir errors for a missing subpath", async () => {
await expect(
listDir(makePayload(testDir, { path: "does-not-exist" }))
).rejects.toMatchObject({ code: "ENOENT" });
});
});
Loading