Skip to content

Commit cba138a

Browse files
committed
fix(cli): tell config get apart a typo from an unset key
config get answered identically for a real-but-unset schema path and a path that does not exist, and pointed both at config validate, which only reports whether the current file parses. Use the schema runConfigGet already builds to split the two cases: a known path reports that it is unset and offers a runnable config set command for that exact path; an unknown path points at config schema. Exit code stays 1 for both, in text and --json.
1 parent 81ace37 commit cba138a

3 files changed

Lines changed: 106 additions & 16 deletions

File tree

‎src/cli/config-cli-path.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ function schemasAtPath(
320320
return schemas;
321321
}
322322

323+
export function isConfigSchemaPath(
324+
schema: JsonSchemaRecord | undefined,
325+
path: readonly PathSegment[],
326+
): boolean {
327+
return schemasAtPath(schema, path).length > 0;
328+
}
329+
323330
function schemaPrefersArrayAtPath(
324331
schema: JsonSchemaRecord | undefined,
325332
path: readonly PathSegment[],

‎src/cli/config-cli.test.ts‎

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,43 @@ vi.mock("../secrets/resolve.js", () => ({
9898
}));
9999

100100
vi.mock("../config/runtime-schema.js", () => ({
101-
buildRuntimeConfigSchemaFromRegistry: () => ({ uiHints: {} }),
101+
buildRuntimeConfigSchemaFromRegistry: () => ({
102+
schema: {
103+
type: "object",
104+
properties: {
105+
models: {
106+
type: "object",
107+
properties: {
108+
providers: {
109+
type: "object",
110+
additionalProperties: {
111+
type: "object",
112+
properties: {
113+
models: {
114+
type: "array",
115+
items: {
116+
type: "object",
117+
properties: { id: { type: "string" } },
118+
},
119+
},
120+
},
121+
},
122+
},
123+
},
124+
},
125+
gateway: {
126+
type: "object",
127+
properties: {
128+
bind: { type: "string" },
129+
port: { type: "number" },
130+
},
131+
},
132+
},
133+
},
134+
uiHints: {},
135+
version: "test",
136+
generatedAt: "2026-03-25T00:00:00.000Z",
137+
}),
102138
readBestEffortRuntimeConfigSchema: () => mockReadBestEffortRuntimeConfigSchema(),
103139
}));
104140

@@ -1303,6 +1339,7 @@ describe("config cli", () => {
13031339

13041340
expect(mockReadConfigFileSnapshot).toHaveBeenCalledWith({ observe: false });
13051341
expect(parseLastLogPayload()).toBe(18789);
1342+
expect(mockExit).not.toHaveBeenCalled();
13061343
});
13071344

13081345
it("redacts sensitive values", async () => {
@@ -1358,19 +1395,64 @@ describe("config cli", () => {
13581395
expect(mockWriteStdout).toHaveBeenCalledWith("60\n");
13591396
});
13601397

1361-
it("outputs JSON error to stdout when path is not found and --json is set", async () => {
1398+
it.each([
1399+
{
1400+
name: "valid but unset schema path",
1401+
path: "gateway.bind",
1402+
message:
1403+
"Config path is valid but unset: gateway.bind. The runtime default applies until you set an authored value with openclaw config set gateway.bind <value>.",
1404+
},
1405+
{
1406+
name: "valid but unset array path",
1407+
path: "models.providers.example.models[0].id",
1408+
message:
1409+
"Config path is valid but unset: models.providers.example.models[0].id. The runtime default applies until you set an authored value with openclaw config set 'models.providers.example.models[0].id' <value>.",
1410+
},
1411+
{
1412+
name: "unknown path",
1413+
path: "nonexistent.path",
1414+
message:
1415+
"Unknown config path: nonexistent.path. Run openclaw config schema to inspect valid paths.",
1416+
},
1417+
])("reports a $name to the operator", async (testCase) => {
13621418
setGatewaySnapshot();
13631419

1364-
await expect(
1365-
runConfigCommand(["config", "get", "nonexistent.path", "--json"]),
1366-
).rejects.toThrow(ExitError);
1420+
await expect(runConfigCommand(["config", "get", testCase.path])).rejects.toThrow(ExitError);
1421+
1422+
expectErrorIncludes(testCase.message);
1423+
expect(mockExit).toHaveBeenCalledWith(1);
1424+
expect(mockLog).not.toHaveBeenCalled();
1425+
});
1426+
1427+
it.each([
1428+
{
1429+
name: "valid but unset schema path",
1430+
path: "gateway.bind",
1431+
message:
1432+
"Config path is valid but unset: gateway.bind. The runtime default applies until you set an authored value with openclaw config set gateway.bind <value>.",
1433+
},
1434+
{
1435+
name: "unknown path",
1436+
path: "nonexistent.path",
1437+
message:
1438+
"Unknown config path: nonexistent.path. Run openclaw config schema to inspect valid paths.",
1439+
},
1440+
])("outputs a JSON error for a $name", async (testCase) => {
1441+
setGatewaySnapshot();
1442+
1443+
await expect(runConfigCommand(["config", "get", testCase.path, "--json"])).rejects.toThrow(
1444+
ExitError,
1445+
);
13671446

13681447
expect(mockError).not.toHaveBeenCalled();
1369-
const payload = parseLastLogPayload() as { error: { type: string; message: string } };
1370-
expect(payload.error).toEqual({
1371-
type: "cli_error",
1372-
message: "Config path not found: nonexistent.path",
1448+
expect(parseLastLogPayload()).toEqual({
1449+
ok: false,
1450+
error: {
1451+
type: "cli_error",
1452+
message: testCase.message,
1453+
},
13731454
});
1455+
expect(mockExit).toHaveBeenCalledWith(1);
13741456
});
13751457

13761458
it.each([

‎src/cli/config-cli.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { normalizeConfigMutationModelRefs } from "./config-cli-model-normalizati
3838
import {
3939
formatConfigUnsetMissingPathMessage,
4040
getAtPath,
41+
isConfigSchemaPath,
4142
parseConfigSetPath,
4243
unsetAtPath,
4344
} from "./config-cli-path.js";
@@ -67,6 +68,7 @@ import {
6768
import { resolveConfigSetMode } from "./config-set-parser.js";
6869
import { formatCliJsonFailure } from "./failure-output.js";
6970
import { setCommandJsonMode } from "./program/json-mode.js";
71+
import { quoteCliArg } from "./quote-cli-arg.js";
7072

7173
export { parseConfigSetPath } from "./config-cli-path.js";
7274

@@ -171,21 +173,20 @@ export async function runConfigGet(opts: { path: string; json?: boolean; runtime
171173
if (!pluginMetadataSnapshot) {
172174
throw new Error("Config plugin metadata unavailable; refusing to display config values.");
173175
}
174-
const { uiHints } = buildRuntimeConfigSchemaFromRegistry(
176+
const { schema, uiHints } = buildRuntimeConfigSchemaFromRegistry(
175177
pluginMetadataSnapshot.manifestRegistry,
176178
);
177179
const res = getAtPath(redactConfigObject(snapshot.config, uiHints), parsedPath);
178180
if (!res.found) {
181+
const message = isConfigSchemaPath(schema, parsedPath)
182+
? `Config path is valid but unset: ${opts.path}. The runtime default applies until you set an authored value with ${formatCliCommand(`openclaw config set ${quoteCliArg(opts.path)} <value>`)}.`
183+
: `Unknown config path: ${opts.path}. Run ${formatCliCommand("openclaw config schema")} to inspect valid paths.`;
179184
if (opts.json) {
180-
writeRuntimeJson(runtime, formatCliJsonFailure(`Config path not found: ${opts.path}`));
185+
writeRuntimeJson(runtime, formatCliJsonFailure(message));
181186
runtime.exit(1);
182187
return;
183188
}
184-
runtime.error(
185-
danger(
186-
`Config path not found: ${opts.path}. Run ${formatCliCommand("openclaw config validate")} to inspect config shape.`,
187-
),
188-
);
189+
runtime.error(danger(message));
189190
runtime.exit(1);
190191
return;
191192
}

0 commit comments

Comments
 (0)