Skip to content

Commit 7ab2a1d

Browse files
committed
fix(settings): symlink cycles fail instead of replacing a link, and a repointed link stays watched
Signed-off-by: Yordis Prieto <[email protected]>
1 parent a6dd266 commit 7ab2a1d

6 files changed

Lines changed: 109 additions & 13 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const settingsChange = (settings: DesktopSettings, changed: boolean): DesktopSet
131131

132132
const DesktopSettingsWriteOperation = Schema.Literals([
133133
"create-temporary-file-name",
134+
"resolve-symlink",
134135
"encode-document",
135136
"create-directory",
136137
"write-temporary-file",
@@ -425,6 +426,14 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp
425426
const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
426427
Effect.provideService(FileSystem.FileSystem, input.fileSystem),
427428
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+
),
428437
);
429438
const directory = input.path.dirname(targetPath);
430439
const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export class DesktopClientSettingsReadError extends Schema.TaggedError<DesktopCl
4242

4343
const DesktopClientSettingsWriteOperation = Schema.Literals([
4444
"create-temporary-file-name",
45+
"resolve-symlink",
4546
"encode-document",
4647
"create-directory",
4748
"write-temporary-file",
@@ -130,6 +131,14 @@ const writeClientSettings = Effect.fnUntraced(function* (input: {
130131
const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
131132
Effect.provideService(FileSystem.FileSystem, input.fileSystem),
132133
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+
),
133142
);
134143
const directory = input.path.dirname(targetPath);
135144
const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;

‎apps/server/src/atomicWrite.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
45
import * as FileSystem from "effect/FileSystem";
56
import * as Path from "effect/Path";
67

@@ -43,6 +44,26 @@ it.layer(NodeServices.layer)("writeFileStringAtomically", (it) => {
4344
}),
4445
);
4546

47+
it.effect("fails on a symlink cycle without replacing either link", () =>
48+
Effect.gen(function* () {
49+
const fs = yield* FileSystem.FileSystem;
50+
const path = yield* Path.Path;
51+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-atomic-write-" });
52+
const first = path.join(root, "first.json");
53+
const second = path.join(root, "second.json");
54+
yield* fs.symlink(second, first);
55+
yield* fs.symlink(first, second);
56+
57+
const result = yield* Effect.exit(
58+
writeFileStringAtomically({ filePath: first, contents: "after" }),
59+
);
60+
61+
assert.isTrue(Exit.isFailure(result));
62+
assert.strictEqual(yield* fs.readLink(first), second);
63+
assert.strictEqual(yield* fs.readLink(second), first);
64+
}),
65+
);
66+
4667
it.effect("creates a missing file and its directory", () =>
4768
Effect.gen(function* () {
4869
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/serverSettings.test.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,41 @@ it.layer(NodeServices.layer)("server settings", (it) => {
181181
).pipe(TestClock.withLive, Effect.provide(makeServerSettingsLayer())),
182182
);
183183

184+
it.effect("follows a settings link that is repointed to another directory", () =>
185+
Effect.scoped(
186+
Effect.gen(function* () {
187+
const config = yield* ServerConfig.ServerConfig;
188+
const fs = yield* FileSystem.FileSystem;
189+
const path = yield* Path.Path;
190+
const service = yield* ServerSettingsModule.ServerSettingsService;
191+
const dotfiles = yield* fs.makeTempDirectoryScoped({ prefix: "t3-settings-dotfiles-" });
192+
const firstSettingsPath = path.join(dotfiles, "first", "settings.json");
193+
const secondSettingsPath = path.join(dotfiles, "second", "settings.json");
194+
yield* fs.makeDirectory(path.dirname(firstSettingsPath), { recursive: true });
195+
yield* fs.makeDirectory(path.dirname(secondSettingsPath), { recursive: true });
196+
yield* fs.writeFileString(firstSettingsPath, `{ "responseStreamingMode": "turn" }`);
197+
yield* fs.writeFileString(secondSettingsPath, `{ "responseStreamingMode": "paragraph" }`);
198+
yield* fs.remove(config.settingsPath, { force: true });
199+
yield* fs.symlink(firstSettingsPath, config.settingsPath);
200+
yield* service.start;
201+
202+
const repointChanges = yield* service.subscribeChanges;
203+
yield* fs.remove(config.settingsPath);
204+
yield* fs.symlink(secondSettingsPath, config.settingsPath);
205+
const repointed = yield* repointChanges.pipe(Stream.runHead, Effect.timeout("2 seconds"));
206+
assert.equal(Option.getOrUndefined(repointed)?.responseStreamingMode, "paragraph");
207+
208+
const editChanges = yield* service.subscribeChanges;
209+
yield* writeFileStringAtomically({
210+
filePath: secondSettingsPath,
211+
contents: `{ "responseStreamingMode": "turn" }`,
212+
});
213+
const edited = yield* editChanges.pipe(Stream.runHead, Effect.timeout("2 seconds"));
214+
assert.equal(Option.getOrUndefined(edited)?.responseStreamingMode, "turn");
215+
}),
216+
).pipe(TestClock.withLive, Effect.provide(makeServerSettingsLayer())),
217+
);
218+
184219
it.effect("reloads when a dangling settings link gets its destination", () =>
185220
Effect.scoped(
186221
Effect.gen(function* () {

‎apps/server/src/serverSettings.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,20 +1215,34 @@ const make = Effect.gen(function* () {
12151215
const revalidateAndEmitSafely = revalidateAndEmit.pipe(Effect.ignoreCause({ log: true }));
12161216

12171217
// A symlinked settings file is rewritten in its destination's directory,
1218-
// which a watch on the link's directory never sees.
1219-
const linkTargetPath = yield* resolveSymlinkTarget(settingsPath).pipe(
1220-
Effect.provideService(FileSystem.FileSystem, fs),
1221-
Effect.provideService(Path.Path, pathService),
1222-
);
1223-
const isLinked = linkTargetPath !== pathService.resolve(settingsPath);
1224-
if (isLinked) {
1218+
// which a watch on the link's directory never sees. The link is resolved
1219+
// again whenever it changes, so repointing it moves the watch along.
1220+
const watchLinkTarget = Effect.gen(function* () {
1221+
const linkTargetPath = yield* resolveSymlinkTarget(settingsPath).pipe(
1222+
Effect.provideService(FileSystem.FileSystem, fs),
1223+
Effect.provideService(Path.Path, pathService),
1224+
);
1225+
if (linkTargetPath === pathService.resolve(settingsPath)) {
1226+
return Option.none<string>();
1227+
}
12251228
yield* fs
12261229
.makeDirectory(pathService.dirname(linkTargetPath), { recursive: true })
12271230
.pipe(Effect.ignore({ log: true }));
1228-
}
1229-
const linkTargetEvents = isLinked
1230-
? watchFileChanges(linkTargetPath).pipe(Stream.ignore({ log: true }))
1231-
: Stream.empty;
1231+
return Option.some(linkTargetPath);
1232+
}).pipe(Effect.orElseSucceed(() => Option.none<string>()));
1233+
1234+
const initialLinkTarget = yield* watchLinkTarget;
1235+
const linkTargetEvents = Stream.make(initialLinkTarget).pipe(
1236+
Stream.concat(watchFileChanges(settingsPath).pipe(Stream.mapEffect(() => watchLinkTarget))),
1237+
Stream.changes,
1238+
Stream.switchMap(
1239+
Option.match({
1240+
onNone: () => Stream.empty,
1241+
onSome: (linkTargetPath) =>
1242+
watchFileChanges(linkTargetPath).pipe(Stream.ignore({ log: true })),
1243+
}),
1244+
),
1245+
);
12321246

12331247
// Debounce watch events so the file is fully written before we read it.
12341248
// Editors emit multiple events per save (truncate, write, rename) and

‎packages/shared/src/symlink.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import * as Effect from "effect/Effect";
22
import * as FileSystem from "effect/FileSystem";
33
import * as Path from "effect/Path";
4+
import * as PlatformError from "effect/PlatformError";
45

56
const MAX_SYMLINK_HOPS = 40;
67

78
/**
89
* Follows a chain of symlinks to the file it finally names, which may not exist
910
* yet. Any path that is not a symlink resolves to itself. Atomic writers rename
10-
* onto this path so a linked file keeps its link.
11+
* onto this path so a linked file keeps its link. Fails on a cycle or an overly
12+
* long chain rather than handing back a link that a rename would replace.
1113
*/
1214
export const resolveSymlinkTarget = (filePath: string) =>
1315
Effect.gen(function* () {
@@ -21,5 +23,11 @@ export const resolveSymlinkTarget = (filePath: string) =>
2123
}
2224
current = path.resolve(path.dirname(current), link.value);
2325
}
24-
return current;
26+
return yield* PlatformError.systemError({
27+
_tag: "Unknown",
28+
module: "FileSystem",
29+
method: "readLink",
30+
description: "Too many levels of symbolic links",
31+
pathOrDescriptor: filePath,
32+
});
2533
});

0 commit comments

Comments
 (0)