Skip to content

Commit ae6c388

Browse files
authored
fix: log warnings for malformed settings files instead of silent fallback (#486)
## Summary - `loadSettingsFile()` silently returned `{}` for **all** errors including malformed JSON, permission errors, and unexpected file content - Only `ENOENT` (file not found) should be silent since missing settings files are expected - Other errors now log a warning via the logger to help diagnose misconfigured settings (e.g. a typo in `settings.json` was previously completely invisible) ## Test plan - [x] `npm run lint` passes - [x] All settings tests pass - [x] Missing files still return `{}` silently (ENOENT path unchanged) - [x] Malformed JSON now produces a logged warning
1 parent 320f4d7 commit ae6c388

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

src/settings.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,26 @@ export interface ClaudeCodeSettings {
2525
}
2626

2727
/**
28-
* Reads and parses a JSON settings file, returning an empty object if not found or invalid
28+
* Reads and parses a JSON settings file, returning an empty object if not found or invalid.
29+
* Silently ignores missing files (ENOENT) but logs warnings for other errors
30+
* (malformed JSON, permission errors, etc.) to aid debugging.
2931
*/
30-
async function loadSettingsFile(filePath: string | null): Promise<ClaudeCodeSettings> {
32+
async function loadSettingsFile(
33+
filePath: string | null,
34+
logger?: { error: (...args: any[]) => void },
35+
): Promise<ClaudeCodeSettings> {
3136
if (!filePath) {
3237
return {};
3338
}
3439

3540
try {
3641
const content = await fs.promises.readFile(filePath, "utf-8");
3742
return JSON.parse(content) as ClaudeCodeSettings;
38-
} catch {
43+
} catch (error) {
44+
if (error instanceof Error && "code" in error && error.code === "ENOENT") {
45+
return {};
46+
}
47+
logger?.error(`Failed to load settings from ${filePath}:`, error);
3948
return {};
4049
}
4150
}
@@ -130,10 +139,10 @@ export class SettingsManager {
130139
*/
131140
private async loadAllSettings(): Promise<void> {
132141
const [userSettings, projectSettings, localSettings, enterpriseSettings] = await Promise.all([
133-
loadSettingsFile(this.getUserSettingsPath()),
134-
loadSettingsFile(this.getProjectSettingsPath()),
135-
loadSettingsFile(this.getLocalSettingsPath()),
136-
loadSettingsFile(getManagedSettingsPath()),
142+
loadSettingsFile(this.getUserSettingsPath(), this.logger),
143+
loadSettingsFile(this.getProjectSettingsPath(), this.logger),
144+
loadSettingsFile(this.getLocalSettingsPath(), this.logger),
145+
loadSettingsFile(getManagedSettingsPath(), this.logger),
137146
]);
138147

139148
this.userSettings = userSettings;

0 commit comments

Comments
 (0)