( updater: (currentConfig: ProjectConfig) => ProjectConfig, )
| 1643 | } |
| 1644 | |
| 1645 | export function saveCurrentProjectConfig( |
| 1646 | updater: (currentConfig: ProjectConfig) => ProjectConfig, |
| 1647 | ): void { |
| 1648 | if (process.env.NODE_ENV === 'test') { |
| 1649 | const config = updater(TEST_PROJECT_CONFIG_FOR_TESTING) |
| 1650 | // Skip if no changes (same reference returned) |
| 1651 | if (config === TEST_PROJECT_CONFIG_FOR_TESTING) { |
| 1652 | return |
| 1653 | } |
| 1654 | Object.assign(TEST_PROJECT_CONFIG_FOR_TESTING, config) |
| 1655 | return |
| 1656 | } |
| 1657 | const absolutePath = getProjectPathForConfig() |
| 1658 | |
| 1659 | let written: GlobalConfig | null = null |
| 1660 | try { |
| 1661 | const didWrite = saveConfigWithLock( |
| 1662 | getGlobalClaudeFile(), |
| 1663 | createDefaultGlobalConfig, |
| 1664 | current => { |
| 1665 | const currentProjectConfig = |
| 1666 | current.projects?.[absolutePath] ?? DEFAULT_PROJECT_CONFIG |
| 1667 | const newProjectConfig = updater(currentProjectConfig) |
| 1668 | // Skip if no changes (same reference returned) |
| 1669 | if (newProjectConfig === currentProjectConfig) { |
| 1670 | return current |
| 1671 | } |
| 1672 | written = { |
| 1673 | ...current, |
| 1674 | projects: { |
| 1675 | ...current.projects, |
| 1676 | [absolutePath]: newProjectConfig, |
| 1677 | }, |
| 1678 | } |
| 1679 | return written |
| 1680 | }, |
| 1681 | ) |
| 1682 | if (didWrite && written) { |
| 1683 | writeThroughGlobalConfigCache(written) |
| 1684 | } |
| 1685 | } catch (error) { |
| 1686 | logForDebugging(`Failed to save config with lock: ${error}`, { |
| 1687 | level: 'error', |
| 1688 | }) |
| 1689 | |
| 1690 | // Same race window as saveGlobalConfig's fallback -- refuse to write |
| 1691 | // defaults over good cached config. See GH #3117. |
| 1692 | const config = getConfig(getGlobalClaudeFile(), createDefaultGlobalConfig) |
| 1693 | if (wouldLoseAuthState(config)) { |
| 1694 | logForDebugging( |
| 1695 | 'saveCurrentProjectConfig fallback: re-read config is missing auth that cache has; refusing to write. See GH #3117.', |
| 1696 | { level: 'error' }, |
| 1697 | ) |
| 1698 | logEvent('tengu_config_auth_loss_prevented', {}) |
| 1699 | return |
| 1700 | } |
| 1701 | const currentProjectConfig = |
| 1702 | config.projects?.[absolutePath] ?? DEFAULT_PROJECT_CONFIG |
no test coverage detected