Skip to content

Commit 7ad71f3

Browse files
committed
fix(fx): update Homebrew installs through their owning brew
1 parent eb8b52d commit 7ad71f3

3 files changed

Lines changed: 113 additions & 25 deletions

File tree

‎docs/user/providers-fx.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ Every fx instance on the same machine shares that machine's fx login. T3 Code do
2121

2222
## Update fx
2323

24-
When a newer fx release is out, the fx card in **Settings → Providers** shows it. **Update now** runs `fx upgrade` on the binary that instance uses, including one set in **Binary path**. You can also run `fx upgrade` on that machine yourself. T3 Code checks for new stable releases only; on fx's dev channel, run `fx upgrade` yourself to get newer dev builds. On macOS 14 or earlier, which newer fx releases do not support, T3 Code does not offer fx updates.
24+
When a newer fx release is out, the fx card in **Settings → Providers** shows it. For standalone installs, **Update now** runs the instance's selected executable with `upgrade`, including one set in **Binary path**. For Homebrew's `fx-agent` formula, it uses the owning Homebrew installation to run `brew upgrade fx-agent` and checks Homebrew's release version. If that ownership cannot be confirmed, update through your package manager yourself.
25+
26+
To update a standalone install manually, run `fx upgrade` on the server's machine. If **Binary path** is set, run that exact executable instead, for example `"/path/to/fx" upgrade`. Standalone version checks cover stable releases only; use the same manual command to get newer dev builds on fx's dev channel. On macOS 14 or earlier, which newer fx releases do not support, T3 Code does not offer fx updates.
2527

2628
## Models and effort
2729

‎packages/provider-fx/src/server/maintenance.test.ts‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { expect, it } from "@effect/vitest";
33
import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts";
44
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Path from "effect/Path";
7+
import * as Sink from "effect/Sink";
8+
import * as Stream from "effect/Stream";
9+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
10+
import * as HostProcess from "@t3tools/shared/HostProcess";
511
import * as HttpClient from "effect/http/HttpClient";
612
import * as HttpClientResponse from "effect/http/HttpClientResponse";
713

@@ -27,6 +33,83 @@ const serving = (response: Response) =>
2733
HttpClient.make((request) => Effect.succeed(HttpClientResponse.fromWeb(request, response)));
2834

2935
it.layer(NodeServices.layer)("fx maintenance", (it) => {
36+
it.effect("uses the owning Homebrew installation and otherwise stays manual-only", () =>
37+
Effect.gen(function* () {
38+
const fs = yield* FileSystem.FileSystem;
39+
const path = yield* Path.Path;
40+
const prefix = yield* fs
41+
.makeTempDirectoryScoped({ prefix: "t3-fx-brew-" })
42+
.pipe(Effect.flatMap((directory) => fs.realPath(directory)));
43+
const brewPath = path.join(prefix, "bin", "brew");
44+
yield* fs.makeDirectory(path.dirname(brewPath), { recursive: true });
45+
yield* fs.writeFileString(brewPath, "");
46+
let reportedPrefix = prefix;
47+
const spawner = ChildProcessSpawner.make((command) => {
48+
if (command._tag !== "StandardCommand") return Effect.die("Expected a brew probe");
49+
const output =
50+
command.args[0] === "--prefix"
51+
? reportedPrefix
52+
: JSON.stringify({ formulae: [{ versions: { stable: "0.0.14" } }] });
53+
return Effect.succeed(
54+
ChildProcessSpawner.makeHandle({
55+
pid: ChildProcessSpawner.ProcessId(1),
56+
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
57+
isRunning: Effect.succeed(false),
58+
kill: () => Effect.void,
59+
unref: Effect.succeed(Effect.void),
60+
stdin: Sink.drain,
61+
stdout: Stream.encodeText(Stream.make(output)),
62+
stderr: Stream.empty,
63+
all: Stream.empty,
64+
getInputFd: () => Sink.drain,
65+
getOutputFd: () => Stream.empty,
66+
}),
67+
);
68+
});
69+
const context = {
70+
binaryPath: path.join(prefix, "bin", "fx"),
71+
resolvedCommandPath: path.join(prefix, "bin", "fx"),
72+
realCommandPath: path.join(prefix, "Cellar", "fx-agent", "0.0.13", "bin", "fx"),
73+
platform: "darwin" as const,
74+
env: { PATH: "" },
75+
};
76+
const resolver = makeFxMaintenance(
77+
Effect.die("Homebrew must not use fx's release channel"),
78+
"24.0.0",
79+
);
80+
const capabilities = yield* resolver
81+
.resolve(context)
82+
.pipe(
83+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
84+
Effect.provideService(HostProcess.Platform, "darwin"),
85+
);
86+
expect(capabilities.update).toMatchObject({
87+
executable: brewPath,
88+
args: ["upgrade", "fx-agent"],
89+
lockKey: "homebrew",
90+
});
91+
expect(yield* capabilities.lookupLatestVersion!).toBe("0.0.14");
92+
93+
reportedPrefix = path.join(prefix, "another-installation");
94+
expect(
95+
(yield* resolver
96+
.resolve(context)
97+
.pipe(
98+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
99+
Effect.provideService(HostProcess.Platform, "darwin"),
100+
)).update,
101+
).toBeNull();
102+
yield* fs.remove(brewPath);
103+
expect((yield* resolver.resolve(context)).update).toBeNull();
104+
expect(
105+
(yield* resolver.resolve({
106+
...context,
107+
realCommandPath: path.join(prefix, "Cellar", "mise", "1.0.0", "bin", "fx"),
108+
})).update,
109+
).toBeNull();
110+
}).pipe(Effect.scoped),
111+
);
112+
30113
it.effect("offers `fx upgrade` on the instance's own binary once a newer release is out", () =>
31114
Effect.gen(function* () {
32115
const binaryPath = "/opt/tools/custom fx";

‎packages/provider-fx/src/server/maintenance.ts‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import * as HttpClient from "effect/http/HttpClient";
55
import * as HttpClientRequest from "effect/http/HttpClientRequest";
66

77
import {
8+
homebrewOwnershipFromCommandPath,
89
makeManualOnlyProviderMaintenanceCapabilities,
910
makeProviderMaintenanceCapabilities,
11+
resolveHomebrewProviderMaintenance,
1012
type ProviderMaintenanceCapabilitiesResolver,
1113
} from "@t3tools/provider-core/server/maintenanceResolver";
1214
import { FX_DRIVER_KIND } from "./acpSupport.ts";
@@ -26,32 +28,33 @@ export const fetchLatestFxVersion = Effect.fn("fetchLatestFxVersion")(function*
2628
);
2729
});
2830

29-
/**
30-
* `fx upgrade` replaces the executable it runs from, so the update runs the
31-
* instance's resolved binary, wherever `binaryPath` points. fx after 0.0.13
32-
* needs macOS 15 (Darwin 24) and `fx upgrade` does not check, so older Macs
33-
* get no update offer or latest-version check.
34-
*/
31+
// fx after 0.0.13 needs Darwin 24, but its updater does not check the host version.
3532
export const makeFxMaintenance = (
3633
lookupLatestVersion: Effect.Effect<string | null>,
3734
osRelease: string = NodeOS.release(),
3835
): ProviderMaintenanceCapabilitiesResolver => ({
39-
resolve: (context) =>
40-
Effect.succeed(
41-
context === null || (context.platform === "darwin" && Number.parseInt(osRelease, 10) < 24)
42-
? makeManualOnlyProviderMaintenanceCapabilities({
43-
provider: FX_DRIVER_KIND,
44-
packageName: null,
45-
})
46-
: makeProviderMaintenanceCapabilities({
47-
provider: FX_DRIVER_KIND,
48-
packageName: null,
49-
updateExecutable: context.resolvedCommandPath,
50-
updateArgs: ["upgrade"],
51-
updateLockKey: `fx:${context.realCommandPath}`,
52-
platform: context.platform,
53-
env: context.env,
54-
lookupLatestVersion,
55-
}),
56-
),
36+
resolve: Effect.fn("FxMaintenance.resolve")(function* (context) {
37+
const input = { provider: FX_DRIVER_KIND, packageName: null };
38+
const manualOnly = makeManualOnlyProviderMaintenanceCapabilities(input);
39+
if (
40+
context === null ||
41+
(context.platform === "darwin" && Number.parseInt(osRelease, 10) < 24)
42+
) {
43+
return manualOnly;
44+
}
45+
const homebrew = homebrewOwnershipFromCommandPath(context.realCommandPath);
46+
if (homebrew) {
47+
if (homebrew.kind !== "formula" || homebrew.name !== "fx-agent") return manualOnly;
48+
return (yield* resolveHomebrewProviderMaintenance(input, homebrew, context)) ?? manualOnly;
49+
}
50+
return makeProviderMaintenanceCapabilities({
51+
...input,
52+
updateExecutable: context.resolvedCommandPath,
53+
updateArgs: ["upgrade"],
54+
updateLockKey: `fx:${context.realCommandPath}`,
55+
platform: context.platform,
56+
env: context.env,
57+
lookupLatestVersion,
58+
});
59+
}),
5760
});

0 commit comments

Comments
 (0)