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
7 changes: 7 additions & 0 deletions .changeset/rc-folder-picker-symlinks-products.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@moonshot-ai/kimi-code": patch
"kimi-code-app": patch
"kimi-code": patch
---

Display symlinked directories in the Remote Control workspace folder picker.
7 changes: 7 additions & 0 deletions .changeset/rc-folder-picker-symlinks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@moonshot-ai/agent-core-v2": patch
"@moonshot-ai/kap-server": patch
"@moonshot-ai/kimi-code-sdk": patch
---

Follow symbolic links when enumerating directories in the host folder browser, skipping broken, looped, and permission-denied links while propagating unexpected filesystem stat failures.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readdir, realpath } from 'node:fs/promises';
import { readdir, realpath, stat } from 'node:fs/promises';
import { homedir } from 'node:os';
import { dirname, isAbsolute, join } from 'node:path';

Expand Down Expand Up @@ -40,13 +40,31 @@ export class HostFolderBrowser implements IHostFolderBrowser {
throw mapFsError(err, realTarget);
}

const entries: FsBrowseEntry[] = dirents
.filter((d) => d.isDirectory())
.map((d) => ({
name: d.name,
path: join(realTarget, d.name),
is_dir: true as const,
}));
const entries: FsBrowseEntry[] = [];
for (const d of dirents) {
const entryPath = join(realTarget, d.name);
if (d.isDirectory()) {
entries.push({ name: d.name, path: entryPath, is_dir: true as const });
} else if (d.isSymbolicLink()) {
try {
const targetStat = await stat(entryPath);
if (targetStat.isDirectory()) {
entries.push({ name: d.name, path: entryPath, is_dir: true as const });
}
} catch (err) {
const code = (err as NodeJS.ErrnoException).code;
if (
code !== 'ENOENT' &&
code !== 'ENOTDIR' &&
code !== 'ELOOP' &&
code !== 'EACCES' &&
code !== 'EPERM'
) {
throw err;
}
}
}
}

entries.sort(compareBrowseEntries);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as fsPromises from "node:fs/promises";
import { mkdtemp, mkdir, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";

import type { IProjectService } from "#/app/project/project";
import { HostFolderBrowser } from "#/app/hostFolderBrowser/hostFolderBrowserService";

vi.mock("node:fs/promises", async (importOriginal) => {
const actual = await importOriginal<typeof import("node:fs/promises")>();
return {
...actual,
stat: vi.fn((...args: Parameters<typeof actual.stat>) =>
actual.stat(...args),
),
};
});

const fakeRegistry = {
list: async () => [],
} as unknown as IProjectService;

describe("HostFolderBrowser", () => {
const dirs: string[] = [];

afterEach(async () => {
while (dirs.length > 0) {
const dir = dirs.pop()!;
await rm(dir, { recursive: true, force: true });
}
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add a regression test for unexpected stat errors

If stat errors are swallowed again, this sole test still passes because it exercises only successful directory links plus intentionally ignored broken, looped, and file links; the previous catch-all therefore satisfies every assertion here. Add a fault-injected case such as EIO that asserts browse() rejects, so the regression test actually fails on the behavior this follow-up corrected.

AGENTS.md reference: AGENTS.md:L61-L61

Useful? React with 👍 / 👎.

it("includes symlinked directories and skips broken, looped, and file symlinks", async () => {
const root = await mkdtemp(join(tmpdir(), "kimi-folder-browser-"));
dirs.push(root);

const projects = join(root, "projects");
const external = join(root, "external");
const targetDir = join(projects, "real-dir");
const targetFile = join(root, "file.txt");

await mkdir(projects, { recursive: true });
await mkdir(external, { recursive: true });
await mkdir(targetDir, { recursive: true });
await writeFile(targetFile, "hello", "utf8");

await symlink(targetDir, join(projects, "internal-link"));
await symlink(external, join(projects, "external-link"));
await symlink(targetFile, join(projects, "file-link"));
await symlink(join(root, "missing-target"), join(projects, "broken-link"));
await symlink(join(projects, "loop-b"), join(projects, "loop-a"));
await symlink(join(projects, "loop-a"), join(projects, "loop-b"));

const browser = new HostFolderBrowser(fakeRegistry);
const res = await browser.browse(projects);

expect(res.entries.map((e) => e.name)).toEqual([
"external-link",
"internal-link",
"real-dir",
]);
expect(res.entries.every((e) => e.is_dir)).toBe(true);

const nav = await browser.browse(join(projects, "external-link"));
expect(nav.entries).toEqual([]);
});

it("propagates unexpected stat errors instead of hiding symlinked directories", async () => {
const root = await mkdtemp(join(tmpdir(), "kimi-folder-browser-eio-"));
dirs.push(root);

const projects = join(root, "projects");
const targetDir = join(root, "target");
await mkdir(projects, { recursive: true });
await mkdir(targetDir, { recursive: true });
await symlink(targetDir, join(projects, "link-dir"));

vi.mocked(fsPromises.stat).mockRejectedValueOnce(
Object.assign(new Error("I/O error"), { code: "EIO" }),
);

const browser = new HostFolderBrowser(fakeRegistry);
await expect(browser.browse(projects)).rejects.toMatchObject({
code: "EIO",
});
});
});
Loading