Skip to content

Commit 025942d

Browse files
fix(desktop): an unreadable settings file is no longer overwritten by a bounds save (#35890)
1 parent 8a38c63 commit 025942d

2 files changed

Lines changed: 83 additions & 13 deletions

File tree

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { assert, describe, it } from "@effect/vitest";
44
import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
66
import * as Layer from "effect/Layer";
7+
import * as PlatformError from "effect/PlatformError";
78
import * as Schema from "effect/Schema";
89
import * as Tracer from "effect/Tracer";
910

@@ -160,6 +161,49 @@ describe("DesktopSettings", () => {
160161
),
161162
);
162163

164+
it.effect("keeps saved preferences when a read fails before a bounds save", () =>
165+
withSettings(
166+
Effect.gen(function* () {
167+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
168+
const fileSystem = yield* FileSystem.FileSystem;
169+
yield* writeSettingsPatch({ serverExposureMode: "network-accessible" });
170+
const cause = PlatformError.systemError({
171+
_tag: "PermissionDenied",
172+
module: "FileSystem",
173+
method: "readFileString",
174+
pathOrDescriptor: environment.desktopSettingsPath,
175+
});
176+
let failRead = true;
177+
const settings = yield* DesktopAppSettings.make.pipe(
178+
Effect.provideService(
179+
FileSystem.FileSystem,
180+
FileSystem.FileSystem.of({
181+
...fileSystem,
182+
readFileString: (path) =>
183+
Effect.suspend(() => {
184+
if (failRead && path === environment.desktopSettingsPath) {
185+
failRead = false;
186+
return Effect.fail(cause);
187+
}
188+
return fileSystem.readFileString(path);
189+
}),
190+
}),
191+
),
192+
);
193+
194+
yield* Effect.exit(settings.load);
195+
yield* Effect.exit(
196+
settings.setMainWindowBounds({ x: 10, y: 20, width: 900, height: 600 }, false),
197+
);
198+
199+
const persisted = yield* decodeDesktopSettingsPatch(
200+
yield* fileSystem.readFileString(environment.desktopSettingsPath),
201+
);
202+
assert.equal(persisted.serverExposureMode, "network-accessible");
203+
}),
204+
),
205+
);
206+
163207
it.effect("loads lenient persisted desktop settings JSON", () =>
164208
withSettings(
165209
Effect.gen(function* () {

‎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)