TypeScript
fix(desktop): failed settings saves no longer leave temp files behind (#29635)
Alex Southwellb113eb020995
diff --git a/apps/desktop/src/settings/DesktopAppSettings.test.ts b/apps/desktop/src/settings/DesktopAppSettings.test.tsindex ab3e2e4cd07..7e4b5300830 100644--- a/apps/desktop/src/settings/DesktopAppSettings.test.ts+++ b/apps/desktop/src/settings/DesktopAppSettings.test.ts@@ -264,6 +264,29 @@ describe("DesktopSettings", () => {),);+ it.effect("removes its temporary file when replacing the settings file fails", () =>+ withSettings(+ Effect.gen(function* () {+ const environment = yield* DesktopEnvironment.DesktopEnvironment;+ const fileSystem = yield* FileSystem.FileSystem;+ const settings = yield* DesktopAppSettings.DesktopAppSettings;+ yield* fileSystem.makeDirectory(`${environment.desktopSettingsPath}/occupied`, {+ recursive: true,+ });++ const error = yield* settings.setServerExposureMode("network-accessible").pipe(Effect.flip);++ assert.equal(error.operation, "replace-settings-file");+ assert.deepEqual(+ (yield* fileSystem.readDirectory(environment.stateDir)).filter((name) =>+ name.endsWith(".tmp"),+ ),+ [],+ );+ }),+ ),+ );+it.effect("migrates legacy implicit update channels to the runtime default", () =>withSettings(Effect.gen(function* () {diff --git a/apps/desktop/src/settings/DesktopAppSettings.ts b/apps/desktop/src/settings/DesktopAppSettings.tsindex aa16502a165..6cd34322d98 100644--- a/apps/desktop/src/settings/DesktopAppSettings.ts+++ b/apps/desktop/src/settings/DesktopAppSettings.ts@@ -442,26 +442,28 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp}),),);- yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`).pipe(- Effect.mapError(- (cause) =>- new DesktopSettingsWriteError({- operation: "write-temporary-file",- path: tempPath,- cause,- }),- ),- );- yield* input.fileSystem.rename(tempPath, targetPath).pipe(- Effect.mapError(- (cause) =>- new DesktopSettingsWriteError({- operation: "replace-settings-file",- path: input.settingsPath,- cause,- }),- ),- );+ yield* Effect.gen(function* () {+ yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`).pipe(+ Effect.mapError(+ (cause) =>+ new DesktopSettingsWriteError({+ operation: "write-temporary-file",+ path: tempPath,+ cause,+ }),+ ),+ );+ yield* input.fileSystem.rename(tempPath, targetPath).pipe(+ Effect.mapError(+ (cause) =>+ new DesktopSettingsWriteError({+ operation: "replace-settings-file",+ path: input.settingsPath,+ cause,+ }),+ ),+ );+ }).pipe(Effect.ensuring(input.fileSystem.remove(tempPath, { force: true }).pipe(Effect.ignore)));});/** @public */diff --git a/apps/desktop/src/settings/DesktopClientSettings.test.ts b/apps/desktop/src/settings/DesktopClientSettings.test.tsindex 2d89754ac11..0dab1c8d83c 100644--- a/apps/desktop/src/settings/DesktopClientSettings.test.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.test.ts@@ -144,6 +144,29 @@ describe("DesktopClientSettings", () => {),);+ it.effect("removes its temporary file when replacing the settings file fails", () =>+ withClientSettings(+ Effect.gen(function* () {+ const environment = yield* DesktopEnvironment.DesktopEnvironment;+ const fileSystem = yield* FileSystem.FileSystem;+ const settings = yield* DesktopClientSettings.DesktopClientSettings;+ yield* fileSystem.makeDirectory(`${environment.clientSettingsPath}/occupied`, {+ recursive: true,+ });++ const error = yield* settings.set(clientSettings).pipe(Effect.flip);++ assert.equal(error.operation, "replace-settings-file");+ assert.deepEqual(+ (yield* fileSystem.readDirectory(environment.stateDir)).filter((name) =>+ name.endsWith(".tmp"),+ ),+ [],+ );+ }),+ ),+ );+it.effect.each([{ label: "permission", reason: "PermissionDenied" },{ label: "I/O", reason: "Unknown" },diff --git a/apps/desktop/src/settings/DesktopClientSettings.ts b/apps/desktop/src/settings/DesktopClientSettings.tsindex 52b61949462..b840bb7a058 100644--- a/apps/desktop/src/settings/DesktopClientSettings.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.ts@@ -162,26 +162,28 @@ const writeClientSettings = Effect.fnUntraced(function* (input: {}),),);- yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`).pipe(- Effect.mapError(- (cause) =>- new DesktopClientSettingsWriteError({- operation: "write-temporary-file",- path: tempPath,- cause,- }),- ),- );- yield* input.fileSystem.rename(tempPath, targetPath).pipe(- Effect.mapError(- (cause) =>- new DesktopClientSettingsWriteError({- operation: "replace-settings-file",- path: input.settingsPath,- cause,- }),- ),- );+ yield* Effect.gen(function* () {+ yield* input.fileSystem.writeFileString(tempPath, `${encoded}\n`).pipe(+ Effect.mapError(+ (cause) =>+ new DesktopClientSettingsWriteError({+ operation: "write-temporary-file",+ path: tempPath,+ cause,+ }),+ ),+ );+ yield* input.fileSystem.rename(tempPath, targetPath).pipe(+ Effect.mapError(+ (cause) =>+ new DesktopClientSettingsWriteError({+ operation: "replace-settings-file",+ path: input.settingsPath,+ cause,+ }),+ ),+ );+ }).pipe(Effect.ensuring(input.fileSystem.remove(tempPath, { force: true }).pipe(Effect.ignore)));});export const make = Effect.gen(function* () {