fix(setup): honor config roots and display terminal dry-run plans

Normalize empty XDG config homes once for configuration writers and registration readers. Render the resolved plan for terminal dry runs while preserving structured output. Cover registration placement and a real PTY dry run, including absence of configuration writes.
This commit is contained in:
morluto
2026-10-13 03:22:53 +08:00
parent cecc6ecbff
commit 2397137615
5 changed files with 95 additions and 12 deletions
+9 -11
View File
@@ -75,6 +75,10 @@ interface ClientDefinition {
) => NonNullable<SetupClient["geminiSettings"]>;
}
/** XDG treats an empty config-home value as unset. */
const xdgConfigHome = ({ home, env }: ClientPathContext): string =>
env.XDG_CONFIG_HOME || join(home, ".config");
const vscodeUserDirectory = ({
home,
platform,
@@ -88,7 +92,7 @@ const vscodeUserDirectory = ({
);
if (platform === "darwin")
return join(home, "Library", "Application Support", "Code", "User");
return join(env.XDG_CONFIG_HOME ?? join(home, ".config"), "Code", "User");
return join(xdgConfigHome({ home, platform, env }), "Code", "User");
};
const traeUserDirectory = ({
@@ -104,7 +108,7 @@ const traeUserDirectory = ({
);
if (platform === "darwin")
return join(home, "Library", "Application Support", "Trae", "User");
return join(env.XDG_CONFIG_HOME ?? join(home, ".config"), "Trae", "User");
return join(xdgConfigHome({ home, platform, env }), "Trae", "User");
};
const claudeDesktopDirectory = ({
@@ -115,7 +119,7 @@ const claudeDesktopDirectory = ({
if (platform === "win32")
return join(env.APPDATA ?? join(home, "AppData", "Roaming"), "Claude");
if (platform === "linux")
return join(env.XDG_CONFIG_HOME ?? join(home, ".config"), "Claude");
return join(xdgConfigHome({ home, platform, env }), "Claude");
return join(home, "Library", "Application Support", "Claude");
};
@@ -268,19 +272,13 @@ const openCodeOverrideDirectory = (
const opencodeDirectory = (context: ClientPathContext): string =>
openCodeOverrideDirectory(context.env) ??
join(
context.env.XDG_CONFIG_HOME ?? join(context.home, ".config"),
"opencode",
);
join(xdgConfigHome(context), "opencode");
/** OpenCode loads global JSON before JSONC, then explicit file and directory overrides. */
const openCodeConfigurationPaths = (
context: ClientPathContext,
): readonly string[] => {
const directory = join(
context.env.XDG_CONFIG_HOME ?? join(context.home, ".config"),
"opencode",
);
const directory = join(xdgConfigHome(context), "opencode");
const overrideDirectory = openCodeOverrideDirectory(context.env);
const paths = [
join(directory, "config.json"),
+7
View File
@@ -82,6 +82,13 @@ export const renderSetupProgress = (event: SetupProgressEvent): void => {
writeLine(`${symbol} ${event.label}${suffix}`);
};
/** Show the resolved dry-run plan to a human at an interactive terminal. */
export const renderSetupDryRun = (actions: readonly SetupAction[]): void => {
intro("REA setup plan", promptStreams);
renderPreflight(actions);
outro("Dry run complete. No changes were made.", promptStreams);
};
/** Finish an interactive setup journey without exposing the full doctor catalog. */
export const renderInteractiveSetupResult = (
result: SetupResult,
+6
View File
@@ -11,6 +11,7 @@ import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js";
import { isUpdateFailure } from "../application/UpdateFailure.js";
import {
confirmInteractiveSetup,
renderSetupDryRun,
renderInteractiveSetupResult,
renderSetupProgress,
} from "./interactiveSetup.js";
@@ -225,6 +226,8 @@ const runSetupCommand = async (
: undefined,
);
if (interactive) renderInteractiveSetupResult(result, environment);
else if (options.dryRun && !input.formatExplicit && hasInteractiveTerminal())
renderSetupDryRun(result.plannedActions);
return result;
};
@@ -282,6 +285,9 @@ const setupIsInteractive = (
!options.yes &&
!options.dryRun &&
!formatExplicit &&
hasInteractiveTerminal();
const hasInteractiveTerminal = (): boolean =>
process.stdin.isTTY === true &&
process.stdout.isTTY === true &&
process.stderr.isTTY === true;
+57 -1
View File
@@ -1,5 +1,6 @@
import { execFile } from "node:child_process";
import { rm } from "node:fs/promises";
import { access, rm } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { spawn } from "@lydell/node-pty";
@@ -73,6 +74,61 @@ const clientStates = [
const agentActions = actions.filter(({ kind }) => kind !== "install_hopper");
describe("TTY setup dry run", () => {
it("prints the plan and no-mutation notice for a TTY dry run", async () => {
const home = await createTestTempDirectory("rea-cli-setup-dry-run-");
const terminal = spawn(
process.execPath,
[
"scripts/rea.mjs",
"setup",
"--dry-run",
"--client",
"codex",
"--skill=false",
],
{
cwd: process.cwd(),
env: {
...process.env,
HOME: home,
USERPROFILE: home,
XDG_CONFIG_HOME: join(home, ".config"),
REA_LOG_LEVEL: "silent",
NO_COLOR: "1",
},
name: "xterm-256color",
cols: 120,
rows: 40,
},
);
let output = "";
terminal.onData((data) => {
output += data;
});
const exitCode = await new Promise<number>((resolvePromise, reject) => {
const timeout = setTimeout(() => {
terminal.kill();
reject(new Error(`TTY dry run timed out after output:\n${output}`));
}, 15_000);
terminal.onExit(({ exitCode: code }) => {
clearTimeout(timeout);
resolvePromise(code);
});
});
expect(exitCode, output).toBe(0);
expect(output).toContain("CREATE Codex");
expect(output).toContain(join(home, ".codex/config.toml"));
expect(output).toContain("Dry run complete. No changes were made.");
await expect(
access(join(home, ".codex/config.toml")),
).rejects.toMatchObject({
code: "ENOENT",
});
});
});
describe("interactive setup journey", () => {
it("shows detected and absent clients without preselecting detected clients", async () => {
const result = await runJourney(
@@ -37,6 +37,22 @@ const getClients = (home: string) =>
);
describe("additional client configuration dialects", () => {
it("treats an empty XDG config home as unset for OpenCode setup", async () => {
const home = await createTestTempDirectory("rea-opencode-empty-xdg-");
const client = supportedClients(home, "linux", {
XDG_CONFIG_HOME: "",
}).find(({ name }) => name === "opencode");
const expectedPath = join(home, ".config/opencode/opencode.json");
if (client === undefined) throw new Error("missing opencode client");
expect(client.configPath).toBe(expectedPath);
expect(client.configPaths).toContain(expectedPath);
expect(
await configureClientConfiguration(client, {}, command),
).toMatchObject({ status: "configured" });
expect(await readFile(expectedPath, "utf8")).toContain('"mcp"');
});
it.each([
"opencode",
"antigravity",