Skip to content

Commit b8ce030

Browse files
fix(desktop): an unreadable settings file is no longer overwritten by a bounds save
readSettings treated every read error as a missing file, so a permission or I/O error loaded defaults and the startup window bounds save replaced the file, erasing saved preferences. Only NotFound now means defaults. Any other read error is logged, defaults are used for the session, and settings changes stay in memory instead of overwriting the file. Fixes #35873 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 647fbaf commit b8ce030

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

‎apps/desktop/src/settings/DesktopAppSettings.ts‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import * as FileSystem from "effect/FileSystem";
1313
import * as Layer from "effect/Layer";
1414
import * as Option from "effect/Option";
1515
import * as Path from "effect/Path";
16+
import * as PlatformError from "effect/PlatformError";
17+
import * as Ref from "effect/Ref";
1618
import * as Schema from "effect/Schema";
1719
import * as SynchronizedRef from "effect/SynchronizedRef";
1820

@@ -379,15 +381,20 @@ function applyWslWindowsFallback(settings: DesktopSettings): DesktopSettings {
379381
return setWslOnly(setWslBackendEnabled(settings, false), false);
380382
}
381383

384+
// Fails only when the file may exist but could not be read; a missing file means defaults.
382385
function readSettings(
383386
fileSystem: FileSystem.FileSystem,
384387
settingsPath: string,
385388
appVersion: string,
386-
): Effect.Effect<DesktopSettings> {
389+
): Effect.Effect<DesktopSettings, PlatformError.PlatformError> {
387390
const defaultSettings = resolveDefaultDesktopSettings(appVersion);
388391

389392
return fileSystem.readFileString(settingsPath).pipe(
390-
Effect.option,
393+
Effect.asSome,
394+
Effect.catchIf(
395+
(cause) => cause.reason._tag === "NotFound",
396+
() => Effect.succeed(Option.none<string>()),
397+
),
391398
Effect.flatMap(
392399
Option.match({
393400
onNone: () => Effect.succeed(defaultSettings),
@@ -476,6 +483,8 @@ export const make = Effect.gen(function* () {
476483
const path = yield* Path.Path;
477484
const random = yield* SecureRandom.SecureRandom;
478485
const settingsRef = yield* SynchronizedRef.make(environment.defaultDesktopSettings);
486+
// Writing over a settings file we could not read would erase the preferences it holds.
487+
const persistenceEnabled = yield* Ref.make(true);
479488

480489
const updateInMemory = (update: (settings: DesktopSettings) => DesktopSettings) =>
481490
SynchronizedRef.modify(settingsRef, (settings) => {
@@ -492,17 +501,23 @@ export const make = Effect.gen(function* () {
492501
return Effect.succeed([settingsChange(settings, false), settings] as const);
493502
}
494503

495-
return random.uuidV4().pipe(
496-
Effect.map((uuid) => uuid.replace(/-/g, "")),
497-
Effect.flatMap((suffix) =>
498-
writeSettings({
499-
fileSystem,
500-
path,
501-
settingsPath: environment.desktopSettingsPath,
502-
settings: nextSettings,
503-
defaultSettings: environment.defaultDesktopSettings,
504-
suffix,
505-
}),
504+
return Ref.get(persistenceEnabled).pipe(
505+
Effect.flatMap((enabled) =>
506+
enabled
507+
? random.uuidV4().pipe(
508+
Effect.map((uuid) => uuid.replace(/-/g, "")),
509+
Effect.flatMap((suffix) =>
510+
writeSettings({
511+
fileSystem,
512+
path,
513+
settingsPath: environment.desktopSettingsPath,
514+
settings: nextSettings,
515+
defaultSettings: environment.defaultDesktopSettings,
516+
suffix,
517+
}),
518+
),
519+
)
520+
: Effect.void,
506521
),
507522
Effect.as([settingsChange(nextSettings, true), nextSettings] as const),
508523
);
@@ -515,6 +530,17 @@ export const make = Effect.gen(function* () {
515530
fileSystem,
516531
environment.desktopSettingsPath,
517532
environment.appVersion,
533+
).pipe(
534+
Effect.catch((cause) =>
535+
Effect.logWarning(
536+
"Could not read desktop settings; using defaults without saving changes.",
537+
cause,
538+
).pipe(
539+
Effect.annotateLogs({ settingsPath: environment.desktopSettingsPath }),
540+
Effect.andThen(Ref.set(persistenceEnabled, false)),
541+
Effect.as(resolveDefaultDesktopSettings(environment.appVersion)),
542+
),
543+
),
518544
);
519545
return yield* SynchronizedRef.setAndGet(settingsRef, settings);
520546
}).pipe(Effect.withSpan("desktop.settings.load")),

0 commit comments

Comments
 (0)