Skip to content
Open
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
11 changes: 9 additions & 2 deletions packages/desktop/src/main/workspace/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { app, safeStorage } from 'electron';
import { join } from 'path';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { readFileSync, writeFileSync, existsSync, renameSync } from 'fs';
import { homedir } from 'os';
import { randomUUID } from 'node:crypto';
import { CONFIG_FILE_NAME, DEFAULT_WORKSPACE_DIR } from '@clawwork/shared';
Expand Down Expand Up @@ -161,7 +161,14 @@ export function readConfig(): AppConfig | null {
const config = JSON.parse(raw) as AppConfig;
const migrated = migrateConfigIfNeeded(config);
return decryptGatewayCredentials(migrated);
} catch {
} catch (err) {
console.error('[config] failed to read:', err);
const corruptedPath = `${cfgPath}.corrupted-${new Date().toISOString().replace(/[:.]/g, '-')}`;
try {
renameSync(cfgPath, corruptedPath);
} catch {
// best-effort backup rename
}
return null;
}
Comment on lines +164 to 173
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation renames the configuration file as 'corrupted' for any error encountered during the read or migration process. This is quite aggressive and could lead to unnecessary data loss. For example, if an I/O error occurs during migration (like a full disk) or if there's a transient filesystem permission issue, the user's valid configuration will be moved aside, forcing them to reconfigure the app from scratch. It is safer to only perform the backup rename if the error is a SyntaxError, which specifically indicates that the file content is not valid JSON and thus truly corrupted.

  } catch (err) {
    console.error('[config] failed to read:', err);
    if (err instanceof SyntaxError) {
      const corruptedPath = cfgPath + '.corrupted-' + new Date().toISOString().replace(/[:.]/g, '-');
      try {
        renameSync(cfgPath, corruptedPath);
      } catch {
        // best-effort backup rename
      }
    }
    return null;
  }

}
Expand Down