fix(#2803): config-get supports --default <value> fallback for missing keys (#2817)

* fix(#2803): honor --default flag in SDK config-get handler

The gsd-sdk query config-get handler ignored the --default <value> flag.
Missing keys always threw 'Key not found' (exit 1), making 8 workflow
sites that rely on config-get --default fall through to error paths.

The CJS path (gsd-tools.cjs) honored --default since #1893; this ports
that behavior to the SDK configGet handler.

Regression test: tests/bug-2803-config-get-default-flag.test.cjs

* fix(#2803): address CodeRabbit — require --default value, keep missing config.json as error, bash fence
This commit is contained in:
Tom Boucher
2026-04-28 20:21:48 -04:00
committed by GitHub
parent 7616309a32
commit b959b1844f
2 changed files with 149 additions and 2 deletions

View File

@@ -80,9 +80,22 @@ export function getAgentToModelMapForProfile(normalizedProfile: string): Record<
* @throws GSDError with Validation classification if key missing or not found
*/
export const configGet: QueryHandler = async (args, projectDir, workstream) => {
const keyPath = args[0];
// Support --default <value> flag (#2803): return this value (exit 0) when the
// key is absent, mirroring gsd-tools.cjs config-get behavior from #1893.
const defaultIdx = args.indexOf('--default');
let defaultValue: string | undefined;
let filteredArgs = args;
if (defaultIdx !== -1) {
if (defaultIdx + 1 >= args.length) {
throw new GSDError('Usage: config-get <key.path> [--default <value>]', ErrorClassification.Validation);
}
defaultValue = String(args[defaultIdx + 1]);
filteredArgs = [...args.slice(0, defaultIdx), ...args.slice(defaultIdx + 2)];
}
const keyPath = filteredArgs[0];
if (!keyPath) {
throw new GSDError('Usage: config-get <key.path>', ErrorClassification.Validation);
throw new GSDError('Usage: config-get <key.path> [--default <value>]', ErrorClassification.Validation);
}
const paths = planningPaths(projectDir, workstream);
@@ -106,11 +119,13 @@ export const configGet: QueryHandler = async (args, projectDir, workstream) => {
if (current === undefined || current === null || typeof current !== 'object') {
// UNIX convention (cf. `git config --get`): missing key exits 1, not 10.
// See issue #2544 — callers use `if ! gsd-sdk query config-get k; then` patterns.
if (defaultValue !== undefined) return { data: defaultValue };
throw new GSDError(`Key not found: ${keyPath}`, ErrorClassification.Execution);
}
current = (current as Record<string, unknown>)[key];
}
if (current === undefined) {
if (defaultValue !== undefined) return { data: defaultValue };
throw new GSDError(`Key not found: ${keyPath}`, ErrorClassification.Execution);
}