Skip to content

Commit 6644126

Browse files
fix(cli): update Windows launchers outside PATH (pingdotgg#40309)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
1 parent 0854259 commit 6644126

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

‎apps/server/src/cli/update.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,32 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
229229
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
230230
);
231231

232+
it.effect("repoints a recorded Windows launcher outside PATH", () =>
233+
Effect.gen(function* () {
234+
const fs = yield* FileSystem.FileSystem;
235+
const path = yield* Path.Path;
236+
const root = yield* fs.makeTempDirectoryScoped();
237+
const baseDir = path.join(root, "home");
238+
const runtimeDir = path.join(baseDir, "runtime");
239+
const launcher = path.join(root, "custom bin", "t3.cmd");
240+
const launchedAs = path.join(runtimeDir, "versions", "1.0.0", "t3.exe");
241+
const targetEntryPath = path.join(runtimeDir, "versions", "2.0.0", "t3.exe");
242+
yield* fs.makeDirectory(runtimeDir, { recursive: true });
243+
yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
244+
yield* fs.writeFileString(launcher, `@echo off\r\n"${launchedAs}" %*`);
245+
yield* fs.writeFileString(path.join(runtimeDir, "launchers"), `${launcher}\r\n`);
246+
247+
const repointed = yield* repointLauncher({ launchedAs, baseDir, targetEntryPath });
248+
249+
assert.equal(Option.getOrUndefined(repointed), launcher);
250+
assert.equal(yield* fs.readFileString(launcher), `@echo off\r\n"${targetEntryPath}" %*`);
251+
}).pipe(
252+
Effect.scoped,
253+
Effect.provideService(HostProcess.Platform, "win32"),
254+
Effect.provideService(HostProcess.Environment, { PATH: "" }),
255+
),
256+
);
257+
232258
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
233259
"finds the script that ran this executable and rewrites it, keeping aliases and arguments",
234260
() =>

‎apps/server/src/cli/update.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as BootService from "../cloud/bootService.ts";
2525
import {
2626
ensurePinnedRuntimeInstalled,
2727
findPosixLauncher,
28+
generatedWindowsShimTarget,
2829
launcherOwnsVersionsDir,
2930
isUnpublishedRelease,
3031
pinnedRuntimePaths,
@@ -171,7 +172,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
171172
);
172173

173174
if (platform === "win32") {
174-
const shimPath = yield* findWindowsShim(input.launchedAs);
175+
const shimPath = yield* findWindowsShim(input.launchedAs, versionsDir);
175176
if (shimPath === undefined) return Option.none<string>();
176177
const current = yield* fs.readFileString(shimPath).pipe(Effect.option);
177178
const quoted = Option.isSome(current) ? windowsShimTarget(current.value) : undefined;
@@ -203,22 +204,37 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
203204
return Option.some(launcher);
204205
});
205206

206-
/** Windows executables see their own path, so find the installer shim by matching t3.cmd targets on PATH. */
207+
/** Windows executables see their own path, so locate the installer shim by matching its target. */
207208
export const findWindowsShim = Effect.fn("cli.update.find_windows_shim")(function* (
208209
executablePath: string,
210+
versionsDir?: string,
209211
) {
210212
const fs = yield* FileSystem.FileSystem;
211213
const path = yield* Path.Path;
212214
const environment = yield* HostProcess.Environment;
213215
const candidates = [
214216
...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []),
215217
...(environment["PATH"] ?? environment["Path"] ?? "").split(";"),
216-
].filter((entry) => entry.trim().length > 0);
217-
for (const directory of candidates) {
218-
const shimPath = path.join(directory, "t3.cmd");
218+
]
219+
.filter((entry) => entry.trim().length > 0)
220+
.map((directory) => ({
221+
shimPath: path.join(directory, "t3.cmd"),
222+
parseTarget: windowsShimTarget,
223+
}));
224+
if (versionsDir !== undefined) {
225+
const recorded = yield* fs
226+
.readFileString(path.join(path.dirname(versionsDir), "launchers"))
227+
.pipe(Effect.orElseSucceed(() => ""));
228+
for (const shimPath of recorded.split(/\r?\n/)) {
229+
if (path.isAbsolute(shimPath) && path.basename(shimPath).toLowerCase() === "t3.cmd") {
230+
candidates.push({ shimPath, parseTarget: generatedWindowsShimTarget });
231+
}
232+
}
233+
}
234+
for (const { shimPath, parseTarget } of candidates) {
219235
const contents = yield* fs.readFileString(shimPath).pipe(Effect.option);
220236
if (Option.isNone(contents)) continue;
221-
const target = windowsShimTarget(contents.value);
237+
const target = parseTarget(contents.value);
222238
if (
223239
target !== undefined &&
224240
path.resolve(target).toLowerCase() === path.resolve(executablePath).toLowerCase()

0 commit comments

Comments
 (0)