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
26 changes: 26 additions & 0 deletions apps/server/src/cli/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ it.layer(NodeServices.layer)("t3 update launcher", (it) => {
}).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
);

it.effect("repoints a recorded Windows launcher outside PATH", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped();
const baseDir = path.join(root, "home");
const runtimeDir = path.join(baseDir, "runtime");
const launcher = path.join(root, "custom bin", "t3.cmd");
const launchedAs = path.join(runtimeDir, "versions", "1.0.0", "t3.exe");
const targetEntryPath = path.join(runtimeDir, "versions", "2.0.0", "t3.exe");
yield* fs.makeDirectory(runtimeDir, { recursive: true });
yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
yield* fs.writeFileString(launcher, `@echo off\r\n"${launchedAs}" %*`);
yield* fs.writeFileString(path.join(runtimeDir, "launchers"), `${launcher}\r\n`);

const repointed = yield* repointLauncher({ launchedAs, baseDir, targetEntryPath });

assert.equal(Option.getOrUndefined(repointed), launcher);
assert.equal(yield* fs.readFileString(launcher), `@echo off\r\n"${targetEntryPath}" %*`);
}).pipe(
Effect.scoped,
Effect.provideService(HostProcess.Platform, "win32"),
Effect.provideService(HostProcess.Environment, { PATH: "" }),
),
);

it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
"finds the script that ran this executable and rewrites it, keeping aliases and arguments",
() =>
Expand Down
28 changes: 22 additions & 6 deletions apps/server/src/cli/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import * as BootService from "../cloud/bootService.ts";
import {
ensurePinnedRuntimeInstalled,
findPosixLauncher,
generatedWindowsShimTarget,
launcherOwnsVersionsDir,
isUnpublishedRelease,
pinnedRuntimePaths,
Expand Down Expand Up @@ -171,7 +172,7 @@ export const repointLauncher = Effect.fn("cli.update.repoint_launcher")(function
);

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

/** Windows executables see their own path, so find the installer shim by matching t3.cmd targets on PATH. */
/** Windows executables see their own path, so locate the installer shim by matching its target. */
export const findWindowsShim = Effect.fn("cli.update.find_windows_shim")(function* (
executablePath: string,
versionsDir?: string,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const environment = yield* HostProcess.Environment;
const candidates = [
...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []),
...(environment["PATH"] ?? environment["Path"] ?? "").split(";"),
].filter((entry) => entry.trim().length > 0);
for (const directory of candidates) {
const shimPath = path.join(directory, "t3.cmd");
]
.filter((entry) => entry.trim().length > 0)
.map((directory) => ({
shimPath: path.join(directory, "t3.cmd"),
parseTarget: windowsShimTarget,
}));
if (versionsDir !== undefined) {
const recorded = yield* fs
.readFileString(path.join(path.dirname(versionsDir), "launchers"))
.pipe(Effect.orElseSucceed(() => ""));
for (const shimPath of recorded.split(/\r?\n/)) {
if (path.isAbsolute(shimPath) && path.basename(shimPath).toLowerCase() === "t3.cmd") {
candidates.push({ shimPath, parseTarget: generatedWindowsShimTarget });
}
}
}
for (const { shimPath, parseTarget } of candidates) {
const contents = yield* fs.readFileString(shimPath).pipe(Effect.option);
if (Option.isNone(contents)) continue;
const target = windowsShimTarget(contents.value);
const target = parseTarget(contents.value);
if (
target !== undefined &&
path.resolve(target).toLowerCase() === path.resolve(executablePath).toLowerCase()
Expand Down
Loading