fix(coding-agent): dedupe symlinked resources (#3818)
* fix(coding-agent): dedupe symlinked resources Fixes #3767 * refactor(coding-agent): extract canonicalizePath util for symlink resolution
This commit is contained in:
@@ -158,6 +158,63 @@ Content`,
|
||||
expect(result.prompts.some((r) => r.path === promptPath && !r.enabled)).toBe(true);
|
||||
});
|
||||
|
||||
it("should resolve symlinked user and project resources once", async () => {
|
||||
const sharedDir = join(tempDir, "shared-resources");
|
||||
const sharedExtensionsDir = join(sharedDir, "extensions");
|
||||
const sharedSkillsDir = join(sharedDir, "skills");
|
||||
const sharedPromptsDir = join(sharedDir, "prompts");
|
||||
const sharedThemesDir = join(sharedDir, "themes");
|
||||
mkdirSync(sharedExtensionsDir, { recursive: true });
|
||||
mkdirSync(sharedSkillsDir, { recursive: true });
|
||||
mkdirSync(sharedPromptsDir, { recursive: true });
|
||||
mkdirSync(sharedThemesDir, { recursive: true });
|
||||
|
||||
writeFileSync(join(sharedExtensionsDir, "shared.ts"), "export default function() {}");
|
||||
mkdirSync(join(sharedSkillsDir, "shared-skill"), { recursive: true });
|
||||
writeFileSync(
|
||||
join(sharedSkillsDir, "shared-skill", "SKILL.md"),
|
||||
`---
|
||||
name: shared-skill
|
||||
description: Shared skill
|
||||
---
|
||||
Content`,
|
||||
);
|
||||
writeFileSync(join(sharedPromptsDir, "shared.md"), "Shared prompt");
|
||||
writeFileSync(join(sharedThemesDir, "shared.json"), JSON.stringify({ name: "shared-theme" }));
|
||||
|
||||
mkdirSync(join(agentDir), { recursive: true });
|
||||
mkdirSync(join(tempDir, ".pi"), { recursive: true });
|
||||
symlinkSync(sharedExtensionsDir, join(agentDir, "extensions"), "dir");
|
||||
symlinkSync(sharedSkillsDir, join(agentDir, "skills"), "dir");
|
||||
symlinkSync(sharedPromptsDir, join(agentDir, "prompts"), "dir");
|
||||
symlinkSync(sharedThemesDir, join(agentDir, "themes"), "dir");
|
||||
symlinkSync(sharedExtensionsDir, join(tempDir, ".pi", "extensions"), "dir");
|
||||
symlinkSync(sharedSkillsDir, join(tempDir, ".pi", "skills"), "dir");
|
||||
symlinkSync(sharedPromptsDir, join(tempDir, ".pi", "prompts"), "dir");
|
||||
symlinkSync(sharedThemesDir, join(tempDir, ".pi", "themes"), "dir");
|
||||
|
||||
const result = await packageManager.resolve();
|
||||
|
||||
expect({
|
||||
extensions: result.extensions.length,
|
||||
skills: result.skills.length,
|
||||
prompts: result.prompts.length,
|
||||
themes: result.themes.length,
|
||||
}).toEqual({
|
||||
extensions: 1,
|
||||
skills: 1,
|
||||
prompts: 1,
|
||||
themes: 1,
|
||||
});
|
||||
|
||||
// Project auto-discovered has higher precedence than user auto-discovered,
|
||||
// so the surviving entry should be scoped to project.
|
||||
expect(result.extensions[0].metadata.scope).toBe("project");
|
||||
expect(result.skills[0].metadata.scope).toBe("project");
|
||||
expect(result.prompts[0].metadata.scope).toBe("project");
|
||||
expect(result.themes[0].metadata.scope).toBe("project");
|
||||
});
|
||||
|
||||
it("should auto-discover project prompts with overrides", async () => {
|
||||
const promptsDir = join(tempDir, ".pi", "prompts");
|
||||
mkdirSync(promptsDir, { recursive: true });
|
||||
|
||||
84
packages/coding-agent/test/paths.test.ts
Normal file
84
packages/coding-agent/test/paths.test.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { canonicalizePath, isLocalPath } from "../src/utils/paths.js";
|
||||
|
||||
let tempDir: string;
|
||||
|
||||
afterEach(() => {
|
||||
if (tempDir) {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
tempDir = "";
|
||||
}
|
||||
});
|
||||
|
||||
function createTempDir(): string {
|
||||
tempDir = mkdtempSync(join(tmpdir(), "pi-paths-"));
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
describe("canonicalizePath", () => {
|
||||
it("returns the real path for a regular file", () => {
|
||||
const dir = createTempDir();
|
||||
const file = join(dir, "file.txt");
|
||||
writeFileSync(file, "hello");
|
||||
expect(canonicalizePath(file)).toBe(realpathSync(file));
|
||||
});
|
||||
|
||||
it("resolves symlinks to their targets", () => {
|
||||
const dir = createTempDir();
|
||||
const target = join(dir, "target.txt");
|
||||
const link = join(dir, "link.txt");
|
||||
writeFileSync(target, "hello");
|
||||
symlinkSync(target, link);
|
||||
expect(canonicalizePath(link)).toBe(realpathSync(target));
|
||||
});
|
||||
|
||||
it("resolves directory symlinks", () => {
|
||||
const dir = createTempDir();
|
||||
const targetDir = join(dir, "target-dir");
|
||||
const linkDir = join(dir, "link-dir");
|
||||
mkdirSync(targetDir);
|
||||
symlinkSync(targetDir, linkDir, "dir");
|
||||
expect(canonicalizePath(linkDir)).toBe(realpathSync(targetDir));
|
||||
});
|
||||
|
||||
it("falls back to the raw path when the target does not exist", () => {
|
||||
const dir = createTempDir();
|
||||
const nonexistent = join(dir, "no-such-file");
|
||||
expect(canonicalizePath(nonexistent)).toBe(nonexistent);
|
||||
});
|
||||
|
||||
it("falls back to the raw path for a dangling symlink", () => {
|
||||
const dir = createTempDir();
|
||||
const target = join(dir, "target.txt");
|
||||
const link = join(dir, "link.txt");
|
||||
// Create a symlink whose target does not exist.
|
||||
symlinkSync(target, link);
|
||||
// realpathSync would throw, so canonicalizePath returns the link path.
|
||||
expect(canonicalizePath(link)).toBe(link);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isLocalPath", () => {
|
||||
it("returns true for bare names", () => {
|
||||
expect(isLocalPath("my-package")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true for relative paths", () => {
|
||||
expect(isLocalPath("./foo")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for npm: protocol", () => {
|
||||
expect(isLocalPath("npm:package")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for git: protocol", () => {
|
||||
expect(isLocalPath("git://repo")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false for https: protocol", () => {
|
||||
expect(isLocalPath("https://example.com")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
@@ -156,6 +156,36 @@ Project skill`,
|
||||
expect(theme?.sourcePath).toBe(projectThemePath);
|
||||
});
|
||||
|
||||
it("should load symlinked user and project extensions once", async () => {
|
||||
const sharedExtDir = join(tempDir, "shared-extensions");
|
||||
mkdirSync(sharedExtDir, { recursive: true });
|
||||
writeFileSync(
|
||||
join(sharedExtDir, "shared.ts"),
|
||||
`export default function(pi) {
|
||||
pi.registerCommand("shared", {
|
||||
description: "shared command",
|
||||
handler: async () => {},
|
||||
});
|
||||
}`,
|
||||
);
|
||||
|
||||
mkdirSync(agentDir, { recursive: true });
|
||||
mkdirSync(join(cwd, ".pi"), { recursive: true });
|
||||
symlinkSync(sharedExtDir, join(agentDir, "extensions"), "dir");
|
||||
symlinkSync(sharedExtDir, join(cwd, ".pi", "extensions"), "dir");
|
||||
|
||||
const loader = new DefaultResourceLoader({ cwd, agentDir });
|
||||
await loader.reload();
|
||||
|
||||
const extensionsResult = loader.getExtensions();
|
||||
expect(extensionsResult.extensions).toHaveLength(1);
|
||||
expect(extensionsResult.errors).toEqual([]);
|
||||
|
||||
// mergePaths processes project paths before user paths, so the project
|
||||
// alias is the canonical survivor.
|
||||
expect(extensionsResult.extensions[0].path).toBe(join(cwd, ".pi", "extensions", "shared.ts"));
|
||||
});
|
||||
|
||||
it("should keep both extensions loaded when command names collide", async () => {
|
||||
const userExtDir = join(agentDir, "extensions");
|
||||
const projectExtDir = join(cwd, ".pi", "extensions");
|
||||
|
||||
Reference in New Issue
Block a user