MCPcopy
hub / github.com/codeaashu/claude-code / updateSettingsForSource

Function updateSettingsForSource

src/utils/settings/settings.ts:416–524  ·  view source on GitHub ↗
(
  source: EditableSettingSource,
  settings: SettingsJson,
)

Source from the content-addressed store, hash-verified

414 * the key is present with an explicit `undefined` value.
415 */
416export function updateSettingsForSource(
417 source: EditableSettingSource,
418 settings: SettingsJson,
419): { error: Error | null } {
420 if (
421 (source as unknown) === 'policySettings' ||
422 (source as unknown) === 'flagSettings'
423 ) {
424 return { error: null }
425 }
426
427 // Create the folder if needed
428 const filePath = getSettingsFilePathForSource(source)
429 if (!filePath) {
430 return { error: null }
431 }
432
433 try {
434 getFsImplementation().mkdirSync(dirname(filePath))
435
436 // Try to get existing settings with validation. Bypass the per-source
437 // cache — mergeWith below mutates its target (including nested refs),
438 // and mutating the cached object would leak unpersisted state if the
439 // write fails before resetSettingsCache().
440 let existingSettings = getSettingsForSourceUncached(source)
441
442 // If validation failed, check if file exists with a JSON syntax error
443 if (!existingSettings) {
444 let content: string | null = null
445 try {
446 content = readFileSync(filePath)
447 } catch (e) {
448 if (!isENOENT(e)) {
449 throw e
450 }
451 // File doesn't exist — fall through to merge with empty settings
452 }
453 if (content !== null) {
454 const rawData = safeParseJSON(content)
455 if (rawData === null) {
456 // JSON syntax error - return validation error instead of overwriting
457 // safeParseJSON will already log the error, so we'll just return the error here
458 return {
459 error: new Error(
460 `Invalid JSON syntax in settings file at ${filePath}`,
461 ),
462 }
463 }
464 if (rawData && typeof rawData === 'object') {
465 existingSettings = rawData as SettingsJson
466 logForDebugging(
467 `Using raw settings from ${filePath} due to validation failure`,
468 )
469 }
470 }
471 }
472
473 const updatedSettings = mergeWith(

Callers 15

callFunction · 0.85
AutoModeOptInDialogFunction · 0.85
MCPServerApprovalDialogFunction · 0.85
EffortCalloutFunction · 0.85
RemoteEnvironmentDialogFunction · 0.85
ModelPickerFunction · 0.85
ThemePickerFunction · 0.85
MemoryFileSelectorFunction · 0.85
onChangeFunction · 0.85
ConfigFunction · 0.85

Calls 14

getFsImplementationFunction · 0.85
isENOENTFunction · 0.85
logForDebuggingFunction · 0.85
markInternalWriteFunction · 0.85
jsonStringifyFunction · 0.85
resetSettingsCacheFunction · 0.85
getOriginalCwdFunction · 0.85

Tested by

no test coverage detected