@@ -46,6 +46,7 @@ import {
4646 reduceDesktopUpdateStateOnInstallFailure ,
4747 reduceDesktopUpdateStateOnNoUpdate ,
4848 reduceDesktopUpdateStateOnUpdateAvailable ,
49+ resolveNotAppliedInstallVersion ,
4950} from "./updateMachine.ts" ;
5051
5152const AUTO_UPDATE_STARTUP_DELAY = "15 seconds" ;
@@ -72,6 +73,14 @@ const UpdateInfo = Schema.Struct({
7273const DownloadProgressInfo = Schema . Struct ( {
7374 percent : Schema . Number ,
7475} ) ;
76+ /** Saved in the runtime folder before an install quits the app, read on the next start. */
77+ export const DESKTOP_UPDATE_INSTALL_RECORD_FILE = "desktop-update-install.json" ;
78+ const DesktopUpdateInstallRecord = Schema . fromJsonString (
79+ Schema . Struct ( { fromVersion : Schema . String , targetVersion : Schema . String } ) ,
80+ ) ;
81+ const decodeInstallRecord = Schema . decodeUnknownEffect ( DesktopUpdateInstallRecord ) ;
82+ const encodeInstallRecord = Schema . encodeEffect ( DesktopUpdateInstallRecord ) ;
83+
7584const decodeAppUpdateYmlConfig = Schema . decodeUnknownEffect ( AppUpdateYmlConfig ) ;
7685const decodeUpdateInfo = Schema . decodeUnknownEffect ( UpdateInfo ) ;
7786const decodeDownloadProgressInfo = Schema . decodeUnknownEffect ( DownloadProgressInfo ) ;
@@ -347,6 +356,7 @@ export const make = Effect.gen(function* () {
347356 const finishedUpdateActions = yield * PubSub . unbounded < UpdateAction > ( ) ;
348357 const updaterConfiguredRef = yield * Ref . make ( false ) ;
349358 const lastLoggedDownloadMilestoneRef = yield * Ref . make ( - 1 ) ;
359+ const notAppliedVersionRef = yield * Ref . make ( Option . none < string > ( ) ) ;
350360 const updateStateRef = yield * Ref . make < DesktopUpdateState > (
351361 createInitialDesktopUpdateState (
352362 environment . appVersion ,
@@ -600,11 +610,57 @@ export const make = Effect.gen(function* () {
600610 . remove ( updateRestartMarkerPath , { force : true } )
601611 . pipe ( Effect . ignore ) ;
602612
613+ // Records the version an install hands to the OS installer, so the next
614+ // start can tell whether it applied. Best effort, like the restart marker.
615+ const installRecordPath = environment . path . join (
616+ updateRestartMarkerDir ,
617+ DESKTOP_UPDATE_INSTALL_RECORD_FILE ,
618+ ) ;
619+ const writeInstallRecord = ( targetVersion : string ) =>
620+ encodeInstallRecord ( { fromVersion : environment . appVersion , targetVersion } ) . pipe (
621+ Effect . flatMap ( ( text ) => fileSystem . writeFileString ( installRecordPath , text ) ) ,
622+ Effect . catch ( ( error ) =>
623+ logUpdaterWarning ( "Could not record the update install." , { errorTag : error . _tag } ) ,
624+ ) ,
625+ ) ;
626+ const removeInstallRecord = fileSystem
627+ . remove ( installRecordPath , { force : true } )
628+ . pipe ( Effect . ignore ) ;
629+
630+ // Reads the record of the last install. Returns the version it did not
631+ // apply, when the app came back on the version it installed from. That
632+ // record stays through later starts, until the version changes or a new
633+ // install replaces it, so no restart forgets the failed version.
634+ const readNotAppliedInstallVersion = fileSystem . readFileString ( installRecordPath ) . pipe (
635+ Effect . flatMap ( decodeInstallRecord ) ,
636+ Effect . option ,
637+ Effect . map ( ( record ) =>
638+ Option . flatMap ( record , ( value ) =>
639+ Option . fromNullishOr ( resolveNotAppliedInstallVersion ( value , environment . appVersion ) ) ,
640+ ) ,
641+ ) ,
642+ Effect . tap ( ( notAppliedVersion ) =>
643+ Option . isNone ( notAppliedVersion ) ? removeInstallRecord : Effect . void ,
644+ ) ,
645+ ) ;
646+
647+ const baseUpdateState = ( channel : DesktopUpdateChannel , enabled : boolean ) =>
648+ Ref . get ( notAppliedVersionRef ) . pipe (
649+ Effect . map ( ( notAppliedVersion ) => ( {
650+ ...createBaseUpdateState ( channel , enabled , environment ) ,
651+ ...Option . match ( notAppliedVersion , {
652+ onNone : ( ) => ( { } ) ,
653+ onSome : ( version ) => ( { notAppliedVersion : version } ) ,
654+ } ) ,
655+ } ) ) ,
656+ ) ;
657+
603658 const resetInstallAction = Effect . all (
604659 [
605660 finishUpdateAction ( "install" ) ,
606661 Ref . set ( desktopState . quitting , false ) ,
607662 removeUpdateRestartMarker ,
663+ removeInstallRecord ,
608664 ] ,
609665 { discard : true } ,
610666 ) ;
@@ -621,6 +677,7 @@ export const make = Effect.gen(function* () {
621677
622678 yield * Ref . set ( desktopState . quitting , false ) ;
623679 yield * removeUpdateRestartMarker ;
680+ yield * removeInstallRecord ;
624681 yield * Effect . gen ( function * ( ) {
625682 const instances = yield * pool . list ;
626683 const restartExit = yield * Effect . forEach ( instances , ( instance ) => instance . start , {
@@ -691,6 +748,8 @@ export const make = Effect.gen(function* () {
691748
692749 return yield * Effect . gen ( function * ( ) {
693750 yield * writeUpdateRestartMarker ;
751+ const { downloadedVersion } = yield * Ref . get ( updateStateRef ) ;
752+ if ( downloadedVersion !== null ) yield * writeInstallRecord ( downloadedVersion ) ;
694753 // Stop every backend in the pool, not just the primary. With
695754 // parallel WSL + Windows backends, leaving the WSL instance up
696755 // means quitAndInstall's app.quit() exits before the pool's
@@ -985,6 +1044,15 @@ export const make = Effect.gen(function* () {
9851044 const appUpdateYmlConfig = yield * readAppUpdateYml ;
9861045 yield * Ref . set ( appUpdateYmlConfigRef , appUpdateYmlConfig ) ;
9871046
1047+ const notAppliedVersion = yield * readNotAppliedInstallVersion ;
1048+ yield * Ref . set ( notAppliedVersionRef , notAppliedVersion ) ;
1049+ if ( Option . isSome ( notAppliedVersion ) ) {
1050+ yield * logUpdaterWarning ( "the last update install did not apply" , {
1051+ version : environment . appVersion ,
1052+ targetVersion : notAppliedVersion . value ,
1053+ } ) ;
1054+ }
1055+
9881056 if ( config . mockUpdates ) {
9891057 yield * electronUpdater . setFeedURL ( {
9901058 provider : "generic" ,
@@ -994,7 +1062,7 @@ export const make = Effect.gen(function* () {
9941062
9951063 const settings = yield * desktopSettings . get ;
9961064 const enabled = yield * shouldEnableAutoUpdates ;
997- yield * setState ( createBaseUpdateState ( settings . updateChannel , enabled , environment ) ) ;
1065+ yield * setState ( yield * baseUpdateState ( settings . updateChannel , enabled ) ) ;
9981066 if ( ! enabled ) {
9991067 return ;
10001068 }
@@ -1065,7 +1133,7 @@ export const make = Effect.gen(function* () {
10651133 ) ;
10661134
10671135 const enabled = yield * shouldEnableAutoUpdates ;
1068- yield * setState ( createBaseUpdateState ( nextChannel , enabled , environment ) ) ;
1136+ yield * setState ( yield * baseUpdateState ( nextChannel , enabled ) ) ;
10691137
10701138 if ( ! enabled || ! ( yield * Ref . get ( updaterConfiguredRef ) ) ) {
10711139 return yield * Ref . get ( updateStateRef ) ;
0 commit comments