Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
29 changes: 28 additions & 1 deletion packages/cli/src/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ export class LoadedSettings {
setNestedPropertySafe(settingsFile.settings, key, value);
setNestedPropertySafe(settingsFile.originalSettings, key, value);
this._merged = this.computeMergedSettings();
saveSettings(settingsFile);
saveSettingsKey(settingsFile.path, key, value);
}
}

Expand Down Expand Up @@ -790,3 +790,30 @@ export function saveSettings(settingsFile: SettingsFile): void {
throw error;
}
}

/**
* Saves only a single key-value pair to the settings file on disk.
* Unlike saveSettings(), this builds a minimal update object so that
* only the specified key is written — all other content in the file
* (including entries added externally while the app was running) is preserved.
*/
export function saveSettingsKey(
filePath: string,
key: string,
value: unknown,
): void {
try {
const dirPath = path.dirname(filePath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}

const update: Record<string, unknown> = {};
setNestedPropertySafe(update, key, value);
updateSettingsFilePreservingFormat(filePath, update);
} catch (error) {
debugLogger.error('Error saving settings key.');
debugLogger.error(error instanceof Error ? error.message : String(error));
throw error;
}
}
22 changes: 22 additions & 0 deletions packages/cli/src/utils/commentJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,28 @@ describe('commentJson', () => {
const unchangedContent = fs.readFileSync(testFilePath, 'utf-8');
expect(unchangedContent).toBe(corruptedContent);
});

it('should preserve keys not present in the update object', () => {
const originalContent = `{
"model": { "name": "old-model" },
"modelProviders": [
{ "name": "provider1" },
{ "name": "provider2" }
]
}`;

fs.writeFileSync(testFilePath, originalContent, 'utf-8');

// Only update model.name — modelProviders should be untouched
updateSettingsFilePreservingFormat(testFilePath, {
model: { name: 'new-model' },
});

const updatedContent = fs.readFileSync(testFilePath, 'utf-8');
expect(updatedContent).toContain('"name": "new-model"');
expect(updatedContent).toContain('"provider1"');
expect(updatedContent).toContain('"provider2"');
});
});
});

Expand Down
Loading