Skip to content

Commit 3bfbc37

Browse files
fix(desktop): the t3 command warns instead of installing behind another t3 (pingdotgg#17351)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 66298aa commit 3bfbc37

4 files changed

Lines changed: 57 additions & 15 deletions

File tree

‎apps/desktop/src/app/DesktopCliCommand.test.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
2-
import { expect, it } from "@effect/vitest";
2+
import { afterEach, beforeEach, expect, it } from "@effect/vitest";
33
import * as Effect from "effect/Effect";
44
import * as FileSystem from "effect/FileSystem";
55
import * as Option from "effect/Option";
@@ -85,6 +85,15 @@ const fakePowerShell = (initial: string) => {
8585
return { registry, spawner };
8686
};
8787

88+
// The machine's own PATH may already hold a t3; each test sets the PATH it means.
89+
const machinePath = process.env.PATH;
90+
beforeEach(() => {
91+
process.env.PATH = "";
92+
});
93+
afterEach(() => {
94+
process.env.PATH = machinePath;
95+
});
96+
8897
it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
8998
it.effect("links the launcher onto PATH and removes only that link", () =>
9099
Effect.gen(function* () {
@@ -165,24 +174,31 @@ it.layer(NodeServices.layer)("DesktopCliCommand", (it) => {
165174
}).pipe(Effect.scoped),
166175
);
167176

168-
it.effect("reports when another t3 earlier on PATH would run instead", () =>
177+
it.effect("refuses to install behind another t3 that runs first", () =>
169178
Effect.gen(function* () {
170179
const fs = yield* FileSystem.FileSystem;
171180
const path = yield* Path.Path;
172181
const home = yield* fs.makeTempDirectoryScoped();
173182
const shadow = path.join(home, "shadow");
174183
yield* fs.makeDirectory(shadow);
175184
yield* fs.writeFileString(path.join(shadow, "t3"), "#!/bin/sh\n", { mode: 0o755 });
176-
const previous = process.env.PATH;
177185
process.env.PATH = [shadow, path.join(home, ".local", "bin")].join(":");
178-
yield* Effect.addFinalizer(() => Effect.sync(() => (process.env.PATH = previous)));
179186

180187
const command = yield* commandIn({ home });
188+
const theirs = path.join(shadow, "t3");
189+
expect((yield* command.state).shadowedBy).toBe(theirs);
190+
// A link behind it would never run, so nothing is created.
191+
const error = yield* Effect.flip(command.install);
192+
expect(error.message).toContain(theirs);
193+
expect(yield* fs.exists(path.join(home, ".local", "bin", "t3"))).toBe(false);
194+
195+
yield* fs.remove(theirs);
181196
const installed = yield* command.install;
182-
expect(installed.installedPath).toBe(path.join(home, ".local", "bin", "t3"));
183-
expect(installed.onPath).toBe(false);
184-
yield* fs.remove(path.join(shadow, "t3"));
185-
expect((yield* command.state).onPath).toBe(true);
197+
expect(installed).toMatchObject({
198+
onPath: true,
199+
installedPath: path.join(home, ".local", "bin", "t3"),
200+
});
201+
expect(installed.shadowedBy).toBeUndefined();
186202
}).pipe(Effect.scoped),
187203
);
188204

‎apps/desktop/src/app/DesktopCliCommand.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ export const make = Effect.gen(function* () {
146146
return Option.none<string>();
147147
});
148148

149+
/** The `t3` a new shell runs when it is not this app's, on Unix. */
150+
const foreignFirstOnPath = Effect.gen(function* () {
151+
if (windows) return Option.none<string>();
152+
const first = yield* firstOnPath;
153+
if (Option.isNone(first) || (yield* isOurLink(first.value))) return Option.none<string>();
154+
return first;
155+
});
156+
149157
/** Where this app's command is installed now, if anywhere. */
150158
const installedAt = Effect.gen(function* () {
151159
if (windows) {
@@ -167,12 +175,16 @@ export const make = Effect.gen(function* () {
167175
return { supported: false, installedPath: null, onPath: false } as const;
168176
}
169177
const installed = yield* installedAt;
170-
if (Option.isNone(installed)) return { supported: true, installedPath: null, onPath: false };
178+
const shadowedBy = yield* foreignFirstOnPath;
179+
const shadow = Option.isSome(shadowedBy) ? { shadowedBy: shadowedBy.value } : {};
180+
if (Option.isNone(installed)) {
181+
return { supported: true, installedPath: null, onPath: false, ...shadow };
182+
}
171183
// On Windows only terminals opened after the change see it. On Unix the
172184
// first `t3` on PATH must be ours; a `t3` earlier on PATH would shadow it.
173185
const first = yield* firstOnPath;
174186
const onPath = windows || (Option.isSome(first) && (yield* isOurLink(first.value)));
175-
return { supported: true, installedPath: installed.value, onPath };
187+
return { supported: true, installedPath: installed.value, onPath, ...shadow };
176188
}).pipe(Effect.orElseSucceed(() => ({ supported: false, installedPath: null, onPath: false })));
177189

178190
/** Writes the launcher if the app has not yet, e.g. when no local backend runs. */
@@ -209,6 +221,13 @@ export const make = Effect.gen(function* () {
209221
.remove(existing.value)
210222
.pipe(Effect.mapError(() => fail(`Could not replace ${existing.value}.`)));
211223
}
224+
// A link behind another `t3` never runs, so installing one would only hide the problem.
225+
const shadowedBy = yield* foreignFirstOnPath;
226+
if (Option.isSome(shadowedBy)) {
227+
return yield* fail(
228+
`Another t3 at ${shadowedBy.value} runs first in a new terminal. Remove it, or run the launcher directly at ${launcher}.`,
229+
);
230+
}
212231
const onPath = pathEntries(process.env.PATH, ":");
213232
const candidates = unixCandidates(environment.homeDirectory, environment.platform);
214233
// Prefer a folder already on PATH that the user can write to without admin rights.

‎apps/web/src/components/settings/CliCommandSettingsRow.tsx‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ export function CliCommandSettingsRow() {
5151

5252
if (!bridge || !state?.supported) return null;
5353
const installed = state.installedPath !== null;
54-
const description = !installed
55-
? "Run T3 Code's CLI as `t3` from any terminal."
56-
: state.onPath
57-
? `Installed at ${state.installedPath}. Open a new terminal to use it.`
58-
: `Installed at ${state.installedPath}, which is not on your PATH yet. Add its folder to your PATH to run \`t3\`.`;
54+
const description = state.shadowedBy
55+
? `Another t3 at ${state.shadowedBy} runs first in a new terminal. Remove it to use T3 Code's.`
56+
: !installed
57+
? "Run T3 Code's CLI as `t3` from any terminal."
58+
: state.onPath
59+
? `Installed at ${state.installedPath}. Open a new terminal to use it.`
60+
: `Installed at ${state.installedPath}, which is not on your PATH yet. Add its folder to your PATH to run \`t3\`.`;
5961

6062
return (
6163
<SettingsRow

‎packages/contracts/src/ipc.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,11 @@ export const DesktopCliCommandStateSchema = Schema.Struct({
325325
installedPath: Schema.NullOr(Schema.String),
326326
/** Whether a new terminal finds it; false when the folder is not on PATH yet. */
327327
onPath: Schema.Boolean,
328+
/**
329+
* Another `t3` a new terminal runs instead, earlier on PATH. Install refuses
330+
* while it is there, since a link behind it would never run.
331+
*/
332+
shadowedBy: Schema.optionalKey(Schema.String),
328333
});
329334
export type DesktopCliCommandState = typeof DesktopCliCommandStateSchema.Type;
330335

0 commit comments

Comments
 (0)