Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/infrastructure/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { execFileSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { toErrorMessage } from '../shared/errors.js';
import { ConfigError, toErrorMessage } from '../shared/errors.js';
import type { CodegraphConfig } from '../types.js';
import { debug, warn } from './logger.js';

Expand Down Expand Up @@ -179,6 +179,7 @@ export function loadConfig(cwd?: string): CodegraphConfig {
_configCache.set(cwd, structuredClone(result));
return result;
} catch (err: unknown) {
if (err instanceof ConfigError) throw err;
debug(`Failed to parse config ${filePath}: ${toErrorMessage(err)}`);
}
}
Expand Down Expand Up @@ -215,7 +216,16 @@ export function applyEnvOverrides(config: CodegraphConfig): CodegraphConfig {

export function resolveSecrets(config: CodegraphConfig): CodegraphConfig {
const cmd = config.llm.apiKeyCommand;
if (typeof cmd !== 'string' || cmd.trim() === '') return config;
if (cmd == null) return config;
if (typeof cmd !== 'string') {
const actual = Array.isArray(cmd) ? 'array' : typeof cmd;
throw new ConfigError(
`llm.apiKeyCommand must be a string (received ${actual}). ` +
'The command is split on whitespace and executed without a shell. ' +
'Example: "apiKeyCommand": "op read op://vault/openai/api-key"',
);
}
if (cmd.trim() === '') return config;

const parts = cmd.trim().split(/\s+/);
const [executable, ...args] = parts;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,12 @@ export interface CodegraphConfig {
model: string | null;
baseUrl: string | null;
apiKey: string | null;
/**
* Shell command that prints the API key to stdout. Must be a single string —
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Misleading "Shell command" phrasing in JSDoc

The doc opens with "Shell command that prints the API key to stdout," but the implementation explicitly executes without a shell (execFileSync with no shell option). This contradicts the very next sentence and may mislead users into writing shell-isms like $(...) or pipes that would silently fail.

Suggested change
* Shell command that prints the API key to stdout. Must be a single string
* Command that prints the API key to stdout. Must be a single string

Fix in Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in cc5cbf9 — removed the misleading "Shell" framing. The JSDoc now reads "Command that prints the API key to stdout" and explicitly calls out that shell features like $(...), pipes, globs, and variable expansion are not supported because execution goes through execFileSync with no shell.

* it is split on whitespace and executed via `execFileSync` with no shell.
* Example: `"op read op://vault/openai/api-key"`. Non-string values are
* rejected with a `ConfigError` at load time.
*/
apiKeyCommand: string | null;
};

Expand Down
20 changes: 16 additions & 4 deletions tests/unit/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
mergeConfig,
resolveSecrets,
} from '../../src/infrastructure/config.js';
import { ConfigError } from '../../src/shared/errors.js';

vi.mock('node:child_process', async (importOriginal) => {
const actual = await importOriginal();
Expand Down Expand Up @@ -430,13 +431,24 @@ describe('resolveSecrets', () => {
expect(config.llm.apiKey).toBe('existing');
});

it('skips when apiKeyCommand is not a string', () => {
const config = {
it('throws ConfigError when apiKeyCommand is not a string', () => {
const numberCfg = {
llm: { provider: null, model: null, baseUrl: null, apiKey: 'existing', apiKeyCommand: 42 },
};
resolveSecrets(config);
expect(() => resolveSecrets(numberCfg as never)).toThrow(ConfigError);
expect(() => resolveSecrets(numberCfg as never)).toThrow(/must be a string/);

const arrayCfg = {
llm: {
provider: null,
model: null,
baseUrl: null,
apiKey: 'existing',
apiKeyCommand: ['op', 'read', 'op://vault/key'],
},
};
expect(() => resolveSecrets(arrayCfg as never)).toThrow(/received array/);
expect(mockExecFile).not.toHaveBeenCalled();
expect(config.llm.apiKey).toBe('existing');
});

it('warns and preserves existing apiKey on command failure', () => {
Expand Down
Loading