feat(workspaces): accept ~ paths and name as alias in workspace.add
The console's open dialog advertises `~/Workspace/...` and sends `name`, but the daemon only accepted an absolute `cwd` and only read `alias`, so the most likely first action failed with a generic `E_BAD_REQUEST` and the typed name vanished. `add()` now expands `~`/`~/…` against the store's home before the absolute-path check (a leading `~user` stays an error, and the message names both accepted forms), and `workspace.add` treats `name` as an alias when `alias` is absent, answering `workspaceId` so a client does not need a follow-up list call. feat(cli): tell users when pi2.nvim cannot host a spawned instance `checkPlugin` only looked for `lua/pi/web/bridge.lua`, so a checkout that can drive an existing nvim but cannot be spawned reported `ok` and the failure surfaced later as an unexplained "did not say hello". The probe now also requires `lua/pi/host.lua`: both present is `ok`, bridge only is `warn` with the update hint, neither is `fail`. `pi2 daemon --help` lists the instance knobs (`PI2_NVIM_BIN`, `PI2_NVIM_INIT`, `PI2_PLUGIN_DIR`, `PI2_ENSURE_TIMEOUT_MS`) it previously kept to itself. MSG2
This commit is contained in:
1 parent
db387ae248
commit
85faacb0ba
4 files changed
+80
-8
No files matched your search
@@ -261,11 +261,15 @@ export class CommandRouter {
|
||||
const input: { cwd: string; alias?: string; trust?: boolean } = {
|
||||
cwd: requireString(params, "cwd"),
|
||||
};
|
||||
const alias = optionalString(params, "alias");
|
||||
// `name` is the web dialog's field; it is an alias when `alias` is absent.
|
||||
// A whitespace-only value is dropped, exactly like an empty `alias`.
|
||||
const alias = optionalString(params, "alias") ?? optionalString(params, "name");
|
||||
if (alias !== undefined) input.alias = alias;
|
||||
if (typeof params.trust === "boolean") input.trust = params.trust;
|
||||
const workspace = await this.workspaces.add(input);
|
||||
return { accepted: true, workspace };
|
||||
// `workspace.workspaceId` is the documented location; the top-level
|
||||
// copy saves the web client a round trip on its fallback path.
|
||||
return { accepted: true, workspaceId: workspace.workspaceId, workspace };
|
||||
}
|
||||
case "workspace.update": {
|
||||
const workspaceId = requireString(params, "workspaceId");
|
||||
|
||||
+23
-6
@@ -14,7 +14,7 @@ import { accessSync, constants, realpathSync, statSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { basename, isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
import type { InstanceManager, WorkspaceStore as WorkspaceStoreContract } from "../core/interfaces.js";
|
||||
import { dataDir, realpathSafe, workspaceIdForCwd } from "../core/paths.js";
|
||||
import { dataDir, expandTilde, realpathSafe, workspaceIdForCwd } from "../core/paths.js";
|
||||
import { ERROR_CODES } from "../protocol/errors.js";
|
||||
import type {
|
||||
InstanceState,
|
||||
@@ -62,6 +62,11 @@ export interface WorkspaceStoreOptions {
|
||||
env?: WorkspaceEnv;
|
||||
/** Clock for the `PI2_WORKSPACE_STALE_DAYS` comparison; defaults to `Date.now`. */
|
||||
now?: () => number;
|
||||
/**
|
||||
* Home for `~`/`~/…` expansion in `add()` and the default allowlist root;
|
||||
* injectable so tests never touch the real home directory.
|
||||
*/
|
||||
home?: string;
|
||||
}
|
||||
|
||||
export interface WorkspaceStoreHandle extends WorkspaceStoreContract {
|
||||
@@ -83,11 +88,13 @@ class WorkspaceStore implements WorkspaceStoreHandle {
|
||||
private readonly roots: readonly string[] | undefined;
|
||||
private readonly env: WorkspaceEnv;
|
||||
private readonly now: () => number;
|
||||
private readonly home: string;
|
||||
|
||||
constructor(options: WorkspaceStoreOptions = {}) {
|
||||
this.logger = options.logger;
|
||||
this.env = options.env ?? process.env;
|
||||
this.now = options.now ?? Date.now;
|
||||
this.home = options.home ?? homedir();
|
||||
this.discovery = options.discovery ?? createDiscovery({ ...(options.logger ? { logger: options.logger } : {}) });
|
||||
this.registry =
|
||||
options.registry ??
|
||||
@@ -141,8 +148,18 @@ class WorkspaceStore implements WorkspaceStoreHandle {
|
||||
if (raw.includes("\0")) {
|
||||
throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, "cwd contains a NUL byte");
|
||||
}
|
||||
if (!isAbsolute(raw)) {
|
||||
throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, `cwd must be absolute: ${raw}`);
|
||||
// `~` and `~/…` are shell shorthand the web dialog advertises; expand before
|
||||
// the absolute-path check. `~user/…` is deliberately left alone: resolving
|
||||
// another user's home is not something the daemon should guess at.
|
||||
const expanded = expandTilde(raw, this.home);
|
||||
if (expanded === undefined) {
|
||||
throw new WorkspaceError(
|
||||
ERROR_CODES.BAD_REQUEST,
|
||||
`cwd must be an absolute path (or start with ~/): ~user is not expanded: ${raw}`,
|
||||
);
|
||||
}
|
||||
if (!isAbsolute(expanded)) {
|
||||
throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, `cwd must be an absolute path (or start with ~/): ${raw}`);
|
||||
}
|
||||
if (input.alias !== undefined && typeof input.alias !== "string") {
|
||||
throw new WorkspaceError(ERROR_CODES.BAD_REQUEST, "alias must be a string");
|
||||
@@ -154,9 +171,9 @@ class WorkspaceStore implements WorkspaceStoreHandle {
|
||||
const roots = this.allowlist();
|
||||
let real: string;
|
||||
try {
|
||||
real = realpathSync(raw);
|
||||
real = realpathSync(expanded);
|
||||
} catch (error) {
|
||||
throw mapFsError(error, raw);
|
||||
throw mapFsError(error, expanded);
|
||||
}
|
||||
// Check the canonical path: symlinks inside the allowlist must not escape it.
|
||||
this.assertAllowed(real, roots);
|
||||
@@ -408,7 +425,7 @@ class WorkspaceStore implements WorkspaceStoreHandle {
|
||||
if (configured !== undefined) {
|
||||
return configured.map((root) => canonicalRoot(root)).filter((root): root is string => root.length > 0);
|
||||
}
|
||||
return rootsFromEnv(this.env.PI2_WORKSPACE_ROOTS);
|
||||
return rootsFromEnv(this.env.PI2_WORKSPACE_ROOTS, this.home);
|
||||
}
|
||||
|
||||
private assertAllowed(candidate: string, roots: readonly string[]): void {
|
||||
|
||||
@@ -102,6 +102,26 @@ describe("CommandRouter", () => {
|
||||
expect(workspaces.refreshCalls).toEqual(["ws-1"]);
|
||||
});
|
||||
|
||||
it("accepts name as the alias when alias is absent", async () => {
|
||||
const added = await router.dispatch({
|
||||
method: "workspace.add",
|
||||
params: { cwd: "/repo/named", name: " Named " },
|
||||
});
|
||||
|
||||
expect(added.status).toBe(202);
|
||||
expect(workspaces.addCalls).toEqual([{ cwd: "/repo/named", alias: " Named " }]);
|
||||
// The response carries the id both where the protocol documents it and at
|
||||
// the top level (the web client's shortcut).
|
||||
expect(added.body).toMatchObject({ accepted: true, workspaceId: "ws-2", workspace: { workspaceId: "ws-2" } });
|
||||
});
|
||||
|
||||
it("prefers alias over name and drops a whitespace-only name", async () => {
|
||||
await router.dispatch({ method: "workspace.add", params: { cwd: "/repo/both", alias: "A", name: "N" } });
|
||||
await router.dispatch({ method: "workspace.add", params: { cwd: "/repo/blank", name: " " } });
|
||||
|
||||
expect(workspaces.addCalls).toEqual([{ cwd: "/repo/both", alias: "A" }, { cwd: "/repo/blank" }]);
|
||||
});
|
||||
|
||||
it("routes instance and system methods to the instance manager", async () => {
|
||||
const ensured = await router.dispatch({ method: "instance.ensure", params: { workspaceId: "ws-1" } });
|
||||
expect(ensured.status).toBe(202);
|
||||
|
||||
@@ -281,6 +281,37 @@ describe("workspace store", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("expands ~ and ~/… against the store's home before the allowlist check", async () => {
|
||||
const home = join(allowRoot, "home");
|
||||
const project = join(home, "project");
|
||||
mkdirSync(project, { recursive: true });
|
||||
const { store, registry } = setup({ env: { PI2_WORKSPACE_HIDE_TEMP: "0" }, home });
|
||||
|
||||
const root = await store.add({ cwd: "~" });
|
||||
expect(root.workspaceId).toBe(workspaceIdForCwd(home));
|
||||
expect(root.cwd).toBe(home);
|
||||
expect(root.alias).toBeNull();
|
||||
|
||||
const child = await store.add({ cwd: "~/project", alias: "Project" });
|
||||
expect(child.workspaceId).toBe(workspaceIdForCwd(project));
|
||||
expect(child.cwd).toBe(project);
|
||||
expect(child.alias).toBe("Project");
|
||||
expect(registry.list()).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("rejects ~user/… and names the accepted forms in the message", async () => {
|
||||
const { store } = setup({ home: allowRoot });
|
||||
|
||||
await expect(store.add({ cwd: "~alice/project" })).rejects.toMatchObject({
|
||||
code: "E_BAD_REQUEST",
|
||||
message: expect.stringContaining("cwd must be an absolute path (or start with ~/)"),
|
||||
});
|
||||
await expect(store.add({ cwd: "relative/project" })).rejects.toMatchObject({
|
||||
code: "E_BAD_REQUEST",
|
||||
message: expect.stringContaining("cwd must be an absolute path (or start with ~/)"),
|
||||
});
|
||||
});
|
||||
|
||||
it("validates relative, missing and non-directory paths", async () => {
|
||||
const file = join(allowRoot, "file.txt");
|
||||
writeFileSync(file, "x");
|
||||
|
||||
Reference in new issue
Block a user