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
24 changes: 21 additions & 3 deletions scripts/install.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,20 @@ function atomicWriteJson(path, content) {
function installLink() {
const dryRun = values["dry-run"];
mkdirSync(target, { recursive: true });
function prepareLinkDestination(dest) {
let st;
try {
st = lstatSync(dest);
} catch (err) {
if (err?.code === "ENOENT") return;
throw err;
}
if (!st.isSymbolicLink() && !values.force) {
throw new UserError(`Refusing to replace existing non-symlink path during --link install: ${dest}
Use --force to replace it.`);
}
if (!dryRun) rmSync(dest, { recursive: true, force: true });
}
for (const dir of COMPONENT_DIRS) {
const from = join(root, dir);
if (!existsSync(from)) continue;
Expand All @@ -188,8 +202,10 @@ function installLink() {
const dest = join(target, "plugins", name);
if (!dryRun) {
mkdirSync(dirname(dest), { recursive: true });
rmSync(dest, { recursive: true, force: true });
prepareLinkDestination(dest);
symlinkSync(src, dest);
} else {
prepareLinkDestination(dest);
}
console.log(`${dryRun ? "Would link" : "Linked"} ${dest} -> ${src}`);
}
Expand All @@ -200,8 +216,10 @@ function installLink() {
const dest = join(target, dir, rel);
if (!dryRun) {
mkdirSync(dirname(dest), { recursive: true });
rmSync(dest, { force: true });
prepareLinkDestination(dest);
symlinkSync(src, dest);
} else {
prepareLinkDestination(dest);
}
console.log(`${dryRun ? "Would link" : "Linked"} ${dest} -> ${src}`);
}
Expand Down Expand Up @@ -343,4 +361,4 @@ try {
if (DEBUG) throw err;
console.error(err instanceof UserError ? `Error: ${err.message}` : `Error: ${err?.message || err}`);
process.exit(1);
}
}
55 changes: 53 additions & 2 deletions tests/install.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import { mkdtempSync, existsSync } from "node:fs";
import { mkdtempSync, existsSync, lstatSync, mkdirSync, readlinkSync, symlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { fileURLToPath } from "node:url";
Expand All @@ -24,4 +24,55 @@ describe("install.mjs", () => {
assert.ok(existsSync(join(target, "agents", "goal.md")));
assert.ok(existsSync(join(target, ".goal-mode-manifest.json")));
});
});

it("link install refuses to delete existing non-symlink plugin paths", () => {
const target = join(mkdtempSync(join(tmpdir(), "goal-mode-link-conflict-")), "cfg");
const localPluginDir = join(target, "plugins", "goal-mode");
mkdirSync(localPluginDir, { recursive: true });
writeFileSync(join(localPluginDir, "keep.txt"), "user-data", "utf8");

const r = spawnSync(process.execPath, ["scripts/install.mjs", "--target", target, "--link"], {
cwd: root,
encoding: "utf8",
});

assert.equal(r.status, 1);
assert.match(r.stderr, /Refusing to replace existing non-symlink path/);
assert.ok(existsSync(join(localPluginDir, "keep.txt")));
});

it("link install replaces existing non-symlink paths only with force", () => {
const target = join(mkdtempSync(join(tmpdir(), "goal-mode-link-force-")), "cfg");
const localPluginDir = join(target, "plugins", "goal-mode");
mkdirSync(localPluginDir, { recursive: true });
writeFileSync(join(localPluginDir, "keep.txt"), "user-data", "utf8");

const r = spawnSync(process.execPath, ["scripts/install.mjs", "--target", target, "--link", "--force"], {
cwd: root,
encoding: "utf8",
});

assert.equal(r.status, 0, r.stderr || r.stdout);
assert.equal(lstatSync(localPluginDir).isSymbolicLink(), true);
assert.equal(existsSync(join(localPluginDir, "keep.txt")), false);
});

it("link install with force replaces dangling symlink plugin paths", () => {
const target = join(mkdtempSync(join(tmpdir(), "goal-mode-link-dangling-")), "cfg");
const pluginsDir = join(target, "plugins");
const localPluginDir = join(pluginsDir, "goal-mode");
mkdirSync(pluginsDir, { recursive: true });
symlinkSync(join(target, "missing-plugin-dir"), localPluginDir, "dir");
assert.equal(existsSync(localPluginDir), false);
assert.equal(lstatSync(localPluginDir).isSymbolicLink(), true);

const r = spawnSync(process.execPath, ["scripts/install.mjs", "--target", target, "--link", "--force"], {
cwd: root,
encoding: "utf8",
});

assert.equal(r.status, 0, r.stderr || r.stdout);
assert.equal(lstatSync(localPluginDir).isSymbolicLink(), true);
assert.equal(readlinkSync(localPluginDir), join(root, "plugins", "goal-mode"));
});
});