diff --git a/src/cli/daemon.ts b/src/cli/daemon.ts index e8d944c..e2521ab 100644 --- a/src/cli/daemon.ts +++ b/src/cli/daemon.ts @@ -46,6 +46,11 @@ Environment: PI2_PORT, PI2_HOST (${DEFAULT_HOST} (all interfaces)), PI2_STATIC_DIR, PI2_ORIGINS, PI2_RUNTIME_DIR, PI2_DATA_DIR, PI2_STATE_DIR, PI2_LOG_LEVEL, PI2_WORKSPACE_ROOTS, PI2_PUBLIC_URL +Managed instances: + PI2_NVIM_BIN= nvim binary on the daemon's PATH (default nvim) + PI2_NVIM_INIT= -u ; "user" loads your own config; unset = generated init + PI2_PLUGIN_DIR= pi2.nvim checkout the generated init prepends + PI2_ENSURE_TIMEOUT_MS= spawn hello timeout in ms (default 10000) Runtime files ($PI2_RUNTIME_DIR): d.sock plugin unix socket (nvim → daemon) diff --git a/src/cli/doctor.ts b/src/cli/doctor.ts index dee700f..5a6088c 100644 --- a/src/cli/doctor.ts +++ b/src/cli/doctor.ts @@ -13,7 +13,7 @@ import { promisify } from "node:util"; import { DEFAULT_HOST, DEFAULT_PORT, describeHost, loadConfig } from "../config.js"; import { defaultStaticDir, executablePath, isBundledRuntime, runtimeDescription } from "../core/install.js"; import { isLoopbackHost } from "../core/network.js"; -import { daemonSocketPath, runtimeDir } from "../core/paths.js"; +import { daemonSocketPath, expandTilde, runtimeDir } from "../core/paths.js"; import { PROTOCOL_VERSION } from "../protocol/types.js"; import { defaultServicePaths, @@ -71,7 +71,7 @@ export interface DoctorContext { nvim: BinaryProbe; pi: BinaryProbe; curl: BinaryProbe; - plugin: { dir: string | undefined; bridgeFound: boolean }; + plugin: { dir: string | undefined; bridgeFound: boolean; hostFound?: boolean }; runtime: { path: string; exists: boolean; mode: number | undefined }; cliToken: { path: string; exists: boolean; mode: number | undefined }; socket: { path: string; exists: boolean }; @@ -182,7 +182,16 @@ export function checkCurl(probe: BinaryProbe): DoctorCheck { export function checkPlugin(probe: DoctorContext["plugin"]): DoctorCheck { if (probe.bridgeFound) { - return { name: "plugin", status: "ok", detail: `${probe.dir ?? "pi2.nvim"} (lua/pi/web/bridge.lua)` }; + // The daemon spawns headless hosts with `lua require("pi.host").start()`, + // so a plugin without `lua/pi/host.lua` cannot serve the web console. + if (probe.hostFound === true) { + return { name: "plugin", status: "ok", detail: `${probe.dir ?? "pi2.nvim"} (pi2.nvim host-capable)` }; + } + return { + name: "plugin", + status: "warn", + detail: `pi2.nvim at ${probe.dir ?? "~/.local/share/nvim/lazy/pi2.nvim"} has no lua/pi/host.lua — update the plugin; the daemon cannot start a workspace from the web console`, + }; } return { name: "plugin", @@ -586,6 +595,13 @@ async function binaryProbe(command: string, args: readonly string[], options: Do return { found: true, path, version }; } +/** `PI2_PLUGIN_DIR` → plugin root; `~`/`~/…` expand against the resolved home. */ +function pluginHome(raw: string | undefined, home: string): string { + const configured = raw?.trim(); + if (configured === undefined || configured === "") return join(home, ".local", "share", "nvim", "lazy", "pi2.nvim"); + return expandTilde(configured, home) ?? configured; +} + export async function collectContext(options: DoctorOptions = {}): Promise { const env = options.env ?? process.env; const platform = options.platform ?? osPlatform(); @@ -602,8 +618,9 @@ export async function collectContext(options: DoctorOptions = {}): Promise = {}): DoctorContext { path: "/usr/bin/curl", version: "curl 8.7.1 (x86_64-pc-linux-gnu) libcurl/8.7.1 OpenSSL/3.3.1", }, - plugin: { dir: "/home/u/.local/share/nvim/lazy/pi2.nvim", bridgeFound: true }, + plugin: { dir: "/home/u/.local/share/nvim/lazy/pi2.nvim", bridgeFound: true, hostFound: true }, runtime: { path: "/run/user/1000/pi2", exists: true, mode: 0o700 }, cliToken: { path: "/run/user/1000/pi2/cli.token", exists: true, mode: 0o600 }, socket: { path: "/run/user/1000/pi2/d.sock", exists: true }, @@ -99,6 +100,24 @@ describe("doctor checks (all passing)", () => { }); }); +describe("doctor plugin check", () => { + it("passes only when bridge.lua and lua/pi/host.lua both exist", () => { + expect(checkPlugin({ dir: "/p", bridgeFound: true, hostFound: true })).toEqual({ + name: "plugin", + status: "ok", + detail: "/p (pi2.nvim host-capable)", + }); + + const stale = checkPlugin({ dir: "/p", bridgeFound: true, hostFound: false }); + expect(stale.status).toBe("warn"); + expect(stale.detail).toContain("/p has no lua/pi/host.lua"); + expect(checkPlugin({ dir: "/p", bridgeFound: true }).status).toBe("warn"); + + expect(checkPlugin({ dir: "/p", bridgeFound: false })).toMatchObject({ status: "fail" }); + expect(checkPlugin({ dir: undefined, bridgeFound: false }).detail).toContain("set PI2_PLUGIN_DIR or install"); + }); +}); + describe("doctor bind check", () => { it("is ok for a loopback bind", () => { const check = checkBind({ host: "127.0.0.1", port: 7676, publicUrl: null }); @@ -436,6 +455,15 @@ describe("doctor failure cases", () => { expect(report.ok).toBe(true); }); + it("warns when the plugin lacks lua/pi/host.lua", async () => { + const report = await checks({ + plugin: { dir: "/home/u/nvim/pi2.nvim", bridgeFound: true, hostFound: false }, + }); + expect(statusOf(report, "plugin")).toBe("warn"); + expect(report.ok).toBe(true); + expect(report.checks.find((check) => check.name === "plugin")?.detail).toContain("lua/pi/host.lua"); + }); + it("fails when the plugin is not installed", async () => { const report = await checks({ plugin: { dir: "/home/u/nvim/pi2.nvim", bridgeFound: false } }); expect(statusOf(report, "plugin")).toBe("fail"); @@ -611,6 +639,30 @@ describe("collectContext", () => { expect(report.checks.find((check) => check.name === "web-dist")?.status).toBe("warn"); }); + it("expands a leading ~/ in PI2_PLUGIN_DIR", async () => { + const home = join(dir, "home"); + const pluginDir = join(home, "plugins", "pi2.nvim"); + mkdirSync(join(pluginDir, "lua", "pi", "web"), { recursive: true }); + writeFileSync(join(pluginDir, "lua", "pi", "web", "bridge.lua"), ""); + writeFileSync(join(pluginDir, "lua", "pi", "host.lua"), ""); + + const ctx = await collectContext({ + platform: "linux", + home, + uid: 1000, + env: { + PATH: "/usr/bin", + PI2_PLUGIN_DIR: "~/plugins/pi2.nvim", + PI2_RUNTIME_DIR: join(dir, "run"), + PI2_STATIC_DIR: join(dir, "web"), + }, + which: () => undefined, + run: async () => ({ ok: false, stdout: "", stderr: "not found" }), + }); + + expect(ctx.plugin).toEqual({ dir: pluginDir, bridgeFound: true, hostFound: true }); + }); + it("records a missing daemon as unreachable", async () => { const ctx = await collectContext({ platform: "darwin",