@@ -13,6 +13,8 @@ import * as FileSystem from "effect/FileSystem";
1313import * as Layer from "effect/Layer" ;
1414import * as Option from "effect/Option" ;
1515import * as Path from "effect/Path" ;
16+ import * as PlatformError from "effect/PlatformError" ;
17+ import * as Ref from "effect/Ref" ;
1618import * as Schema from "effect/Schema" ;
1719import * 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.
382385function 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