( updater: (currentConfig: ProjectConfig) => ProjectConfig, )
| 1623 | } |
| 1624 | |
| 1625 | export function saveCurrentProjectConfig( |
| 1626 | updater: (currentConfig: ProjectConfig) => ProjectConfig, |
| 1627 | ): void { |
| 1628 | if (process.env.NODE_ENV === 'test') { |
| 1629 | const config = updater(TEST_PROJECT_CONFIG_FOR_TESTING) |
| 1630 | // Skip if no changes (same reference returned) |
| 1631 | if (config === TEST_PROJECT_CONFIG_FOR_TESTING) { |
| 1632 | return |
| 1633 | } |
| 1634 | Object.assign(TEST_PROJECT_CONFIG_FOR_TESTING, config) |
| 1635 | return |
| 1636 | } |
| 1637 | const absolutePath = getProjectPathForConfig() |
| 1638 | |
| 1639 | let written: GlobalConfig | null = null |
| 1640 | try { |
| 1641 | const didWrite = saveConfigWithLock( |
| 1642 | getGlobalClaudeFile(), |
| 1643 | createDefaultGlobalConfig, |
| 1644 | current => { |
| 1645 | const currentProjectConfig = |
| 1646 | current.projects?.[absolutePath] ?? DEFAULT_PROJECT_CONFIG |
| 1647 | const newProjectConfig = updater(currentProjectConfig) |
| 1648 | // Skip if no changes (same reference returned) |
| 1649 | if (newProjectConfig === currentProjectConfig) { |
| 1650 | return current |
| 1651 | } |
| 1652 | written = { |
| 1653 | ...current, |
| 1654 | projects: { |
| 1655 | ...current.projects, |
| 1656 | [absolutePath]: newProjectConfig, |
| 1657 | }, |
| 1658 | } |
| 1659 | return written |
| 1660 | }, |
| 1661 | ) |
| 1662 | if (didWrite && written) { |
| 1663 | writeThroughGlobalConfigCache(written) |
| 1664 | } |
| 1665 | } catch (error) { |
| 1666 | logForDebugging(`Failed to save config with lock: ${error}`, { |
| 1667 | level: 'error', |
| 1668 | }) |
| 1669 | |
| 1670 | // Same race window as saveGlobalConfig's fallback -- refuse to write |
| 1671 | // defaults over good cached config. See GH #3117. |
| 1672 | const config = getConfig(getGlobalClaudeFile(), createDefaultGlobalConfig) |
| 1673 | if (wouldLoseAuthState(config)) { |
| 1674 | logForDebugging( |
| 1675 | 'saveCurrentProjectConfig fallback: re-read config is missing auth that cache has; refusing to write. See GH #3117.', |
| 1676 | { level: 'error' }, |
| 1677 | ) |
| 1678 | logEvent('tengu_config_auth_loss_prevented', {}) |
| 1679 | return |
| 1680 | } |
| 1681 | const currentProjectConfig = |
| 1682 | config.projects?.[absolutePath] ?? DEFAULT_PROJECT_CONFIG |
no test coverage detected