Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions apps/desktop/src/settings/DesktopAppSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,34 @@ describe("DesktopSettings", () => {
),
);

it.effect("saves through a symlinked settings file without replacing the link", () =>
withSettings(
Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const fileSystem = yield* FileSystem.FileSystem;
const settings = yield* DesktopAppSettings.DesktopAppSettings;
const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-settings-dotfiles-",
});
const linkedSettingsPath = `${dotfiles}/desktop-settings.json`;
yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
yield* fileSystem.symlink(linkedSettingsPath, environment.desktopSettingsPath);

yield* settings.setServerExposureMode("network-accessible");

assert.equal(
yield* fileSystem.readLink(environment.desktopSettingsPath),
linkedSettingsPath,
);
const persisted = yield* decodeDesktopSettingsPatch(
yield* fileSystem.readFileString(linkedSettingsPath),
);
assert.equal(persisted.serverExposureMode, "network-accessible");
}),
),
);

it.effect("migrates legacy implicit update channels to the runtime default", () =>
withSettings(
Effect.gen(function* () {
Expand Down
11 changes: 8 additions & 3 deletions apps/desktop/src/settings/DesktopAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type DesktopUpdateChannel,
} from "@t3tools/contracts";
import { fromLenientJson } from "@t3tools/shared/schemaJson";
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
import * as Context from "effect/Context";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -421,8 +422,12 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp
readonly defaultSettings: DesktopSettings;
readonly suffix: string;
}): Effect.fn.Return<void, DesktopSettingsWriteError> {
const directory = input.path.dirname(input.settingsPath);
const tempPath = `${input.settingsPath}.${process.pid}.${input.suffix}.tmp`;
const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
Effect.provideService(FileSystem.FileSystem, input.fileSystem),
Effect.provideService(Path.Path, input.path),
);
const directory = input.path.dirname(targetPath);
const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;
const encoded = yield* encodeDesktopSettingsJson(
toDesktopSettingsDocument(input.settings, input.defaultSettings),
).pipe(
Expand Down Expand Up @@ -455,7 +460,7 @@ const writeSettings = Effect.fn("desktop.settings.writeSettings")(function* (inp
}),
),
);
yield* input.fileSystem.rename(tempPath, input.settingsPath).pipe(
yield* input.fileSystem.rename(tempPath, targetPath).pipe(
Effect.mapError(
(cause) =>
new DesktopSettingsWriteError({
Expand Down
28 changes: 28 additions & 0 deletions apps/desktop/src/settings/DesktopClientSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ describe("DesktopClientSettings", () => {
),
);

it.effect("saves through a symlinked client settings file without replacing the link", () =>
withClientSettings(
Effect.gen(function* () {
const environment = yield* DesktopEnvironment.DesktopEnvironment;
const fileSystem = yield* FileSystem.FileSystem;
const settings = yield* DesktopClientSettings.DesktopClientSettings;
const dotfiles = yield* fileSystem.makeTempDirectoryScoped({
prefix: "t3-desktop-client-settings-dotfiles-",
});
const linkedSettingsPath = `${dotfiles}/client-settings.json`;
yield* fileSystem.writeFileString(linkedSettingsPath, "{}\n");
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
yield* fileSystem.symlink(linkedSettingsPath, environment.clientSettingsPath);

yield* settings.set(clientSettings);

assert.equal(
yield* fileSystem.readLink(environment.clientSettingsPath),
linkedSettingsPath,
);
assert.deepEqual(
yield* decodeClientSettingsJson(yield* fileSystem.readFileString(linkedSettingsPath)),
clientSettings,
);
}),
),
);

for (const failure of [
{ label: "permission", reason: "PermissionDenied" },
{ label: "I/O", reason: "Unknown" },
Expand Down
11 changes: 8 additions & 3 deletions apps/desktop/src/settings/DesktopClientSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ClientSettingsSchema, type ClientSettings } from "@t3tools/contracts";
import { fromLenientJson } from "@t3tools/shared/schemaJson";
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
import * as Context from "effect/Context";
import * as Crypto from "effect/Crypto";
import * as Effect from "effect/Effect";
Expand Down Expand Up @@ -126,8 +127,12 @@ const writeClientSettings = Effect.fnUntraced(function* (input: {
readonly settings: ClientSettings;
readonly suffix: string;
}): Effect.fn.Return<void, DesktopClientSettingsWriteError> {
const directory = input.path.dirname(input.settingsPath);
const tempPath = `${input.settingsPath}.${process.pid}.${input.suffix}.tmp`;
const targetPath = yield* resolveSymlinkTarget(input.settingsPath).pipe(
Effect.provideService(FileSystem.FileSystem, input.fileSystem),
Effect.provideService(Path.Path, input.path),
);
const directory = input.path.dirname(targetPath);
const tempPath = `${targetPath}.${process.pid}.${input.suffix}.tmp`;
const encoded = yield* encodeClientSettingsJson(input.settings).pipe(
Effect.mapError(
(cause) =>
Expand Down Expand Up @@ -158,7 +163,7 @@ const writeClientSettings = Effect.fnUntraced(function* (input: {
}),
),
);
yield* input.fileSystem.rename(tempPath, input.settingsPath).pipe(
yield* input.fileSystem.rename(tempPath, targetPath).pipe(
Effect.mapError(
(cause) =>
new DesktopClientSettingsWriteError({
Expand Down
22 changes: 1 addition & 21 deletions apps/server/src/atomicWrite.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

const MAX_SYMLINK_HOPS = 40;

/**
* Follows a chain of symlinks to the file it finally names, which may not exist
* yet. Any path that is not a symlink resolves to itself.
*/
export const resolveSymlinkTarget = (filePath: string) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
let current = path.resolve(filePath);
for (let hop = 0; hop < MAX_SYMLINK_HOPS; hop++) {
const link = yield* fs.readLink(current).pipe(Effect.option);
if (link._tag === "None") {
return current;
}
current = path.resolve(path.dirname(current), link.value);
}
return current;
});
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";

/**
* Replaces a file's contents via a sibling temp file and rename. A symlinked
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/serverSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ import * as Semaphore from "effect/Semaphore";
import * as Scope from "effect/Scope";
import * as Stream from "effect/Stream";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { resolveSymlinkTarget, writeFileStringAtomically } from "./atomicWrite.ts";
import { writeFileStringAtomically } from "./atomicWrite.ts";
import { resolveSymlinkTarget } from "@t3tools/shared/symlink";
import * as ServerConfig from "./config.ts";
import { type DeepPartial, deepMerge } from "@t3tools/shared/Struct";
import { fromJsonStringPretty, fromLenientJson } from "@t3tools/shared/schemaJson";
Expand Down
2 changes: 1 addition & 1 deletion docs/fork/0027-symlinked-settings-stay-linked.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 0027: Symlinked settings stay linked

- PR: [TrogonStack/t3code#73](https://github.com/TrogonStack/t3code/pull/73)
- PRs: [TrogonStack/t3code#73](https://github.com/TrogonStack/t3code/pull/73), [TrogonStack/t3code#74](https://github.com/TrogonStack/t3code/pull/74)
- Status: active

## What you can do now
Expand Down
2 changes: 1 addition & 1 deletion docs/fork/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Each entry uses these sections:
- **0025** [A test run leaves no processes behind](./0025-a-test-run-leaves-no-processes-behind.md)
active, [#57](https://github.com/TrogonStack/t3code/pull/57)
- **0027** [Symlinked settings stay linked](./0027-symlinked-settings-stay-linked.md)
active, [#73](https://github.com/TrogonStack/t3code/pull/73)
active, [#73](https://github.com/TrogonStack/t3code/pull/73), [#74](https://github.com/TrogonStack/t3code/pull/74)
4 changes: 4 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@
"types": "./src/connectAuth.ts",
"import": "./src/connectAuth.ts"
},
"./symlink": {
"types": "./src/symlink.ts",
"import": "./src/symlink.ts"
},
"./path": {
"types": "./src/path.ts",
"import": "./src/path.ts"
Expand Down
25 changes: 25 additions & 0 deletions packages/shared/src/symlink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

const MAX_SYMLINK_HOPS = 40;

/**
* Follows a chain of symlinks to the file it finally names, which may not exist
* yet. Any path that is not a symlink resolves to itself. Atomic writers rename
* onto this path so a linked file keeps its link.
*/
export const resolveSymlinkTarget = (filePath: string) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
let current = path.resolve(filePath);
for (let hop = 0; hop < MAX_SYMLINK_HOPS; hop++) {
const link = yield* fs.readLink(current).pipe(Effect.option);
if (link._tag === "None") {
return current;
}
current = path.resolve(path.dirname(current), link.value);
}
return current;
});
Loading