Skip to content

Commit 00eb8f6

Browse files
authored
fix(settings): symlinked settings files stay linked when saved (#15009)
Signed-off-by: Yordis Prieto <[email protected]>
1 parent 8d84666 commit 00eb8f6

10 files changed

Lines changed: 453 additions & 21 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,34 @@ describe("DesktopSettings", () => {
357357
),
358358
);
359359

360+
it.effect("saves through a symlinked settings file without replacing the link", () =>
361+
withSettings(
362+
Effect.gen(function* () {
363+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
364+
const fileSystem = yield* FileSystem.FileSystem;
365+
const settings = yield* DesktopAppSettings.DesktopAppSettings;
366+
const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
367+
prefix: "t3-desktop-settings-dotfiles-",
368+
});
369+
const linkedSettingsPath = `${dotfiles}/desktop-settings.json`;
370+
yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
371+
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
372+
yield* fileSystem.symlink(linkedSettingsPath, environment.desktopSettingsPath);
373+
374+
yield* settings.setServerExposureMode("network-accessible");
375+
376+
assert.equal(
377+
yield* fileSystem.readLink(environment.desktopSettingsPath),
378+
linkedSettingsPath,
379+
);
380+
const persisted = yield* decodeDesktopSettingsPatch(
381+
yield* fileSystem.readFileString(linkedSettingsPath),
382+
);
383+
assert.equal(persisted.serverExposureMode, "network-accessible");
384+
}),
385+
),
386+
);
387+
360388
it.effect("migrates legacy implicit update channels to the runtime default", () =>
361389
withSettings(
362390
Effect.gen(function* () {

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type DesktopUpdateChannel,
66
} from "@t3tools/contracts";
77
import { fromLenientJson } from "@t3tools/shared/schemaJson";
8+
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
89
import * as Context from "effect/Context";
910
import * as Crypto from "effect/Crypto";
1011
import * as Effect from "effect/Effect";
@@ -130,6 +131,7 @@ const settingsChange = (settings: DesktopSettings, changed: boolean): DesktopSet
130131

131132
const DesktopSettingsWriteOperation = Schema.Literals([
132133
"create-temporary-file-name",
134+
"resolve-symlink",
133135
"encode-document",
134136
"create-directory",
135137
"write-temporary-file",
@@ -421,8 +423,20 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp
421423
readonly defaultSettings: DesktopSettings;
422424
readonly suffix: string;
423425
}): Effect.fn.Return<void, DesktopSettingsWriteError> {
424-
const directory = input.path.dirname(input.settingsPath);
425-
const tempPath = `${input.settingsPath}.${process.pid}.${input.suffix}.tmp`;
426+
const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
427+
Effect.provideService(FileSystem.FileSystem, input.fileSystem),
428+
Effect.provideService(Path.Path, input.path),
429+
Effect.mapError(
430+
(cause) =>
431+
new DesktopSettingsWriteError({
432+
operation: "resolve-symlink",
433+
path: input.settingsPath,
434+
cause,
435+
}),
436+
),
437+
);
438+
const directory = input.path.dirname(targetPath);
439+
const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;
426440
const encoded = yield* encodeDesktopSettingsJson(
427441
toDesktopSettingsDocument(input.settings, input.defaultSettings),
428442
).pipe(
@@ -455,7 +469,7 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp
455469
}),
456470
),
457471
);
458-
yield* input.fileSystem.rename(tempPath, input.settingsPath).pipe(
472+
yield* input.fileSystem.rename(tempPath, targetPath).pipe(
459473
Effect.mapError(
460474
(cause) =>
461475
new DesktopSettingsWriteError({

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ describe("DesktopClientSettings", () => {
149149
),
150150
);
151151

152+
it.effect("saves through a symlinked client settings file without replacing the link", () =>
153+
withClientSettings(
154+
Effect.gen(function* () {
155+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
156+
const fileSystem = yield* FileSystem.FileSystem;
157+
const settings = yield* DesktopClientSettings.DesktopClientSettings;
158+
const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
159+
prefix: "t3-desktop-client-settings-dotfiles-",
160+
});
161+
const linkedSettingsPath = `${dotfiles}/client-settings.json`;
162+
yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
163+
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
164+
yield* fileSystem.symlink(linkedSettingsPath, environment.clientSettingsPath);
165+
166+
yield* settings.set(clientSettings);
167+
168+
assert.equal(
169+
yield* fileSystem.readLink(environment.clientSettingsPath),
170+
linkedSettingsPath,
171+
);
172+
assert.deepEqual(
173+
yield* decodeClientSettingsJson(yield* fileSystem.readFileString(linkedSettingsPath)),
174+
clientSettings,
175+
);
176+
}),
177+
),
178+
);
179+
152180
it.effect.each([
153181
{ label: "permission", reason: "PermissionDenied" },
154182
{ label: "I/O", reason: "Unknown" },

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ClientSettingsSchema, type ClientSettings } from "@t3tools/contracts";
22
import { fromLenientJson } from "@t3tools/shared/schemaJson";
3+
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
34
import * as Context from "effect/Context";
45
import * as Crypto from "effect/Crypto";
56
import * as Effect from "effect/Effect";
@@ -41,6 +42,7 @@ export class DesktopClientSettingsReadError extends Schema.TaggedError<DesktopCl
4142

4243
const DesktopClientSettingsWriteOperation = Schema.Literals([
4344
"create-temporary-file-name",
45+
"resolve-symlink",
4446
"encode-document",
4547
"create-directory",
4648
"write-temporary-file",
@@ -126,8 +128,20 @@ const writeClientSettings = Effect.fnUntraced(function* (input: {
126128
readonly settings: ClientSettings;
127129
readonly suffix: string;
128130
}): Effect.fn.Return<void, DesktopClientSettingsWriteError> {
129-
const directory = input.path.dirname(input.settingsPath);
130-
const tempPath = `${input.settingsPath}.${process.pid}.${input.suffix}.tmp`;
131+
const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
132+
Effect.provideService(FileSystem.FileSystem, input.fileSystem),
133+
Effect.provideService(Path.Path, input.path),
134+
Effect.mapError(
135+
(cause) =>
136+
new DesktopClientSettingsWriteError({
137+
operation: "resolve-symlink",
138+
path: input.settingsPath,
139+
cause,
140+
}),
141+
),
142+
);
143+
const directory = input.path.dirname(targetPath);
144+
const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;
131145
const encoded = yield* encodeClientSettingsJson(input.settings).pipe(
132146
Effect.mapError(
133147
(cause) =>
@@ -158,7 +172,7 @@ const writeClientSettings = Effect.fnUntraced(function* (input: {
158172
}),
159173
),
160174
);
161-
yield* input.fileSystem.rename(tempPath, input.settingsPath).pipe(
175+
yield* input.fileSystem.rename(tempPath, targetPath).pipe(
162176
Effect.mapError(
163177
(cause) =>
164178
new DesktopClientSettingsWriteError({
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
2+
import { assert, it } from "@effect/vitest";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
7+
import * as Path from "effect/Path";
8+
import * as PlatformError from "effect/PlatformError";
9+
10+
import { writeFileStringAtomically } from "./atomicWrite.ts";
11+
12+
it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
13+
it.effect("keeps a symlinked file linked and rewrites its destination", () =>
14+
Effect.gen(function* () {
15+
const fs = yield* FileSystem.FileSystem;
16+
const path = yield* Path.Path;
17+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
18+
const destination = path.join(root, "dotfiles", "settings.json");
19+
const link = path.join(root, "home", "settings.json");
20+
yield* fs.makeDirectory(path.dirname(destination), { recursive: true });
21+
yield* fs.makeDirectory(path.dirname(link), { recursive: true });
22+
yield* fs.writeFileString(destination, "before");
23+
yield* fs.symlink(destination, link);
24+
25+
yield* writeFileStringAtomically({ filePath: link, contents: "after" });
26+
27+
assert.strictEqual(yield* fs.readLink(link), destination);
28+
assert.strictEqual(yield* fs.readFileString(destination), "after");
29+
}),
30+
);
31+
32+
it.effect("keeps a dangling symlink linked and creates its destination", () =>
33+
Effect.gen(function* () {
34+
const fs = yield* FileSystem.FileSystem;
35+
const path = yield* Path.Path;
36+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
37+
const destination = path.join(root, "dotfiles", "settings.json");
38+
const link = path.join(root, "home", "settings.json");
39+
yield* fs.makeDirectory(path.dirname(link), { recursive: true });
40+
yield* fs.symlink(destination, link);
41+
42+
yield* writeFileStringAtomically({ filePath: link, contents: "fresh" });
43+
44+
assert.strictEqual(yield* fs.readLink(link), destination);
45+
assert.strictEqual(yield* fs.readFileString(destination), "fresh");
46+
}),
47+
);
48+
49+
it.effect("fails on a symlink cycle without replacing either link", () =>
50+
Effect.gen(function* () {
51+
const fs = yield* FileSystem.FileSystem;
52+
const path = yield* Path.Path;
53+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
54+
const first = path.join(root, "first.json");
55+
const second = path.join(root, "second.json");
56+
yield* fs.symlink(second, first);
57+
yield* fs.symlink(first, second);
58+
59+
const result = yield* Effect.exit(
60+
writeFileStringAtomically({ filePath: first, contents: "after" }),
61+
);
62+
63+
assert.isTrue(Exit.isFailure(result));
64+
assert.strictEqual(yield* fs.readLink(first), second);
65+
assert.strictEqual(yield* fs.readLink(second), first);
66+
}),
67+
);
68+
69+
it.effect("resolves a relative link through a symlinked parent directory", () =>
70+
Effect.gen(function* () {
71+
const fs = yield* FileSystem.FileSystem;
72+
const path = yield* Path.Path;
73+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
74+
const destination = path.join(root, "dotfiles", "config", "settings.json");
75+
const linkedState = path.join(root, "dotfiles", "state");
76+
const home = path.join(root, "home");
77+
const link = path.join(home, "state", "settings.json");
78+
yield* fs.makeDirectory(path.dirname(destination), { recursive: true });
79+
yield* fs.makeDirectory(linkedState, { recursive: true });
80+
yield* fs.makeDirectory(home, { recursive: true });
81+
yield* fs.symlink(linkedState, path.join(home, "state"));
82+
yield* fs.writeFileString(destination, "before");
83+
yield* fs.symlink("../config/settings.json", link);
84+
85+
yield* writeFileStringAtomically({ filePath: link, contents: "after" });
86+
87+
assert.strictEqual(yield* fs.readLink(link), "../config/settings.json");
88+
assert.strictEqual(yield* fs.readFileString(destination), "after");
89+
}),
90+
);
91+
92+
it.effect("creates a missing file and its directory", () =>
93+
Effect.gen(function* () {
94+
const fs = yield* FileSystem.FileSystem;
95+
const path = yield* Path.Path;
96+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
97+
const filePath = path.join(root, "nested", "settings.json");
98+
99+
yield* writeFileStringAtomically({ filePath, contents: "fresh" });
100+
101+
assert.strictEqual(yield* fs.readFileString(filePath), "fresh");
102+
}),
103+
);
104+
});
105+
106+
it.effect("surfaces an unreadable link instead of writing over it", () =>
107+
Effect.gen(function* () {
108+
const readLinkFailure = PlatformError.systemError({
109+
_tag: "Unknown",
110+
module: "FileSystem",
111+
method: "readLink",
112+
pathOrDescriptor: "/home/settings.json",
113+
});
114+
115+
const result = yield* Effect.exit(
116+
writeFileStringAtomically({ filePath: "/home/settings.json", contents: "after" }),
117+
);
118+
119+
assert.deepStrictEqual(result, Exit.fail(readLinkFailure));
120+
}).pipe(
121+
Effect.provide(
122+
Layer.mergeAll(
123+
Path.layer,
124+
FileSystem.layerNoop({
125+
readLink: () =>
126+
Effect.fail(
127+
PlatformError.systemError({
128+
_tag: "Unknown",
129+
module: "FileSystem",
130+
method: "readLink",
131+
pathOrDescriptor: "/home/settings.json",
132+
}),
133+
),
134+
rename: () => Effect.die("an unreadable link must not be replaced"),
135+
}),
136+
),
137+
),
138+
),
139+
);

‎apps/server/src/atomicWrite.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import * as Effect from "effect/Effect";
22
import * as FileSystem from "effect/FileSystem";
33
import * as Path from "effect/Path";
4+
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
45

6+
/**
7+
* Replaces a file's contents via a sibling temp file and rename. A symlinked
8+
* target is resolved first so the link survives and its destination is
9+
* rewritten, since renaming over the link itself would swap it for a regular file.
10+
*/
511
export const writeFileStringAtomically = (input: {
612
readonly filePath: string;
713
readonly contents: string;
@@ -10,16 +16,17 @@ export const writeFileStringAtomically = (input: {
1016
Effect.gen(function* () {
1117
const fs = yield* FileSystem.FileSystem;
1218
const path = yield* Path.Path;
13-
const targetDirectory = path.dirname(input.filePath);
19+
const targetPath = yield* resolveSymlinkTarget(input.filePath);
20+
const targetDirectory = path.dirname(targetPath);
1421

1522
yield* fs.makeDirectory(targetDirectory, { recursive: true });
1623
const tempDirectory = yield* fs.makeTempDirectoryScoped({
1724
directory: targetDirectory,
18-
prefix: `${path.basename(input.filePath)}.`,
25+
prefix: `${path.basename(targetPath)}.`,
1926
});
2027
const tempPath = path.join(tempDirectory, "contents.tmp");
2128

2229
yield* fs.writeFileString(tempPath, input.contents);
23-
yield* fs.rename(tempPath, input.filePath);
30+
yield* fs.rename(tempPath, targetPath);
2431
}),
2532
);

0 commit comments

Comments
 (0)