fix(install): launch t3 by absolute path #13794
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/13794/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/server/src/cli/update.ts line 180OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -151,3 +167,14 @@151167 return Option.some(shimPath);152168 }153169170+ // Rename a new script over the old launcher, so a symlink is replaced, not171+ // written through.172+ const writeLauncher = (launcherPath: string) => {173+ const tempPath = `${launcherPath}.${process.pid}.tmp`;174+ return fs175+ .writeFileString(tempPath, posixLauncherScript(input.targetEntryPath), { mode: 0o755 })176+ .pipe(177+ Effect.andThen(fs.rename(tempPath, launcherPath)),178+ Effect.mapError(179+ () =>180+ new CliUpdateError({ reason: `Could not repoint the t3 launcher at ${launcherPath}.` }),
- apps/server/src/cli/update.ts line 282OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -225,0 +262,21 @@262+ * wrote.263+ */264+export const findPosixLauncher = Effect.fn("cli.update.find_posix_launcher")(function* (265+ executablePath: string,266+) {267+ const fs = yield* FileSystem.FileSystem;268+ const path = yield* Path.Path;269+ const environment = yield* HostProcessEnvironment;270+ const candidates = [271+ ...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []),272+ ...(environment["PATH"] ?? "").split(":"),273+ ].filter((entry) => entry.trim().length > 0);274+ for (const directory of candidates) {275+ const launcherPath = path.join(directory, "t3");276+ // A symlink here links an executable, and any other large file is not our277+ // script. Do not read either.278+ if (Option.isSome(yield* fs.readLink(launcherPath).pipe(Effect.option))) continue;279+ const info = yield* fs.stat(launcherPath).pipe(Effect.option);280+ if (Option.isNone(info) || info.value.type !== "File" || info.value.size > 4096n) continue;281+ const contents = yield* fs.readFileString(launcherPath).pipe(Effect.option);282+ const target = Option.isSome(contents) ? posixLauncherTarget(contents.value) : undefined;apps/server/src/cli/update.ts line 280OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -225,0 +260,21 @@260+ * executable only ever sees its own path. Walk PATH for a `t3` script whose261+ * target is the running executable; that is the launcher the install script262+ * wrote.263+ */264+export const findPosixLauncher = Effect.fn("cli.update.find_posix_launcher")(function* (265+ executablePath: string,266+) {267+ const fs = yield* FileSystem.FileSystem;268+ const path = yield* Path.Path;269+ const environment = yield* HostProcessEnvironment;270+ const candidates = [271+ ...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []),272+ ...(environment["PATH"] ?? "").split(":"),273+ ].filter((entry) => entry.trim().length > 0);274+ for (const directory of candidates) {275+ const launcherPath = path.join(directory, "t3");276+ // A symlink here links an executable, and any other large file is not our277+ // script. Do not read either.278+ if (Option.isSome(yield* fs.readLink(launcherPath).pipe(Effect.option))) continue;279+ const info = yield* fs.stat(launcherPath).pipe(Effect.option);280+ if (Option.isNone(info) || info.value.type !== "File" || info.value.size > 4096n) continue;apps/server/src/cli/update.ts lines 276 to 278OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -225,0 +258,21 @@258+/**259+ * On POSIX the `t3` script execs the executable by absolute path, so the260+ * executable only ever sees its own path. Walk PATH for a `t3` script whose261+ * target is the running executable; that is the launcher the install script262+ * wrote.263+ */264+export const findPosixLauncher = Effect.fn("cli.update.find_posix_launcher")(function* (265+ executablePath: string,266+) {267+ const fs = yield* FileSystem.FileSystem;268+ const path = yield* Path.Path;269+ const environment = yield* HostProcessEnvironment;270+ const candidates = [271+ ...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []),272+ ...(environment["PATH"] ?? "").split(":"),273+ ].filter((entry) => entry.trim().length > 0);274+ for (const directory of candidates) {275+ const launcherPath = path.join(directory, "t3");276+ // A symlink here links an executable, and any other large file is not our277+ // script. Do not read either.278+ if (Option.isSome(yield* fs.readLink(launcherPath).pipe(Effect.option))) continue;
- apps/server/src/cli/update.ts lines 235 to 239OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -223,3 +255,19 @@223255 return undefined;224256 });225257258+/**259+ * On POSIX the `t3` script execs the executable by absolute path, so the260+ * executable only ever sees its own path. Walk PATH for a `t3` script whose261+ * target is the running executable; that is the launcher the install script262+ * wrote.263+ */264+export const findPosixLauncher = Effect.fn("cli.update.find_posix_launcher")(function* (265+ executablePath: string,266+) {267+ const fs = yield* FileSystem.FileSystem;268+ const path = yield* Path.Path;269+ const environment = yield* HostProcessEnvironment;270+ const candidates = [271+ ...(environment["T3CODE_INSTALL_BIN_DIR"] ? [environment["T3CODE_INSTALL_BIN_DIR"]] : []),272+ ...(environment["PATH"] ?? "").split(":"),273+ ].filter((entry) => entry.trim().length > 0);
- apps/server/src/cli/update.ts lines 277 to 279OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -225,0 +259,21 @@259+ * executable only ever sees its own path. Look in install.sh's bin directory260+ * and on PATH for a `t3` that is exactly the script for the running261+ * executable; that is the launcher the install script or an update wrote.262+ * A wrapper someone wrote by hand does not match and is left alone.263+ */264+export const findPosixLauncher = Effect.fn("cli.update.find_posix_launcher")(function* (265+ executablePath: string,266+) {267+ const fs = yield* FileSystem.FileSystem;268+ const path = yield* Path.Path;269+ const environment = yield* HostProcessEnvironment;270+ const expected = posixLauncherScript(executablePath);271+ const expectedSize = BigInt(Buffer.byteLength(expected));272+ // install.sh's default, so `~/.local/bin/t3` run by full path is found273+ // when that folder is not on PATH.274+ const installBinDir =275+ environment["T3CODE_INSTALL_BIN_DIR"] ||276+ (environment["HOME"] ? path.join(environment["HOME"], ".local", "bin") : "");277+ const candidates = [installBinDir, ...(environment["PATH"] ?? "").split(":")].filter(278+ (entry) => entry.trim().length > 0,279+ );apps/server/src/cli/update.ts line 196OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -156,11 +187,10 @@156- const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value);157- if (!ownsTarget(resolvedTarget)) return Option.none<string>();158- const tempLink = `${input.launchedAs}.${process.pid}.tmp`;159- yield* fs.symlink(input.targetEntryPath, tempLink).pipe(160- Effect.andThen(fs.rename(tempLink, input.launchedAs)),161- Effect.mapError(162- () =>163- new CliUpdateError({ reason: `Could not repoint the t3 launcher at ${input.launchedAs}.` }),164- ),165- );166- return Option.some(input.launchedAs);187+ if (Option.isSome(linkTarget)) {188+ const resolvedTarget = path.resolve(path.dirname(input.launchedAs), linkTarget.value);189+ return ownsTarget(resolvedTarget)190+ ? yield* writeLauncher(input.launchedAs)191+ : Option.none<string>();192+ }193+ // The script runs the executable by absolute path, so the executable sees194+ // only itself. Look for the script, as with the Windows shim.195+ if (!ownsTarget(input.launchedAs)) return Option.none<string>();196+ const launcherPath = yield* findPosixLauncher(input.launchedAs);
juliusmarminge added labelsmacroscope-review
juliusmarminge assignedjuliusmarminge
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge added labelstakeover:in-progress
- @@ -516,0 +563,21 @@563+ ) {564+ return undefined;565+ }566+ const recorded = yield* fs567+ .readFileString(path.join(path.dirname(input.versionsDir), "launchers"))568+ .pipe(Effect.orElseSucceed(() => ""));569+ const candidates = [570+ // PATH first: it decides which t3 the user runs.571+ ...(environment["PATH"] ?? "").split(":").map((directory) => path.join(directory, "t3")),572+ path.join(573+ environment["T3CODE_INSTALL_BIN_DIR"] ||574+ path.join(yield* HostProcess.HomeDirectory, ".local", "bin"),575+ "t3",576+ ),577+ ...recorded.split(/\r?\n/),578+ ].filter((candidate) => path.isAbsolute(candidate));579+ for (const candidate of candidates) {580+ // The script itself, so an alias that links to it keeps working.581+ const script = yield* realPath(candidate);582+ if (script === undefined) continue;583+ const target = yield* readPosixLauncherTarget(script);scripts/install.sh line 278Outdated@@ -267,5 +267,12 @@267267268268 step "Setting up the t3 command..."269269 mkdir -p "$bin_dir"270-# Replace only this home's own t3 links: into its versions, or the desktop app's launcher.271-# Anything that is not a symlink (a script, a copied binary) belongs to someone else.270+# Exec by absolute path: through a symlink, argv0 is the link, and the Cursor SDK looks for its271+# helpers beside it. t3 update and t3 install write the same script.272+launcher_script() {273+ printf "#!/bin/sh\nexec '%s' \"\$@\"\n" "$(printf '%s' "$1" | sed "s/'/'\\\\''/g")"274+}275+# The executable an unedited launcher script at $1 runs; a user's own wrapper prints nothing.276+script_t3() {277+ [ -f "$1" ] && [ ! -L "$1" ] && [ "$(wc -c < "$1")" -le 4096 ] || return 0278+ quoted="$(sed -n "2s/^exec '\(.*\)' \"\\\$@\"\$/\1/p" "$1")"@@ -516,0 +558,21 @@558+ const realVersionsDir = yield* realPath(input.versionsDir);559+ if (560+ executable === undefined ||561+ realVersionsDir === undefined ||562+ !isVersionExecutable(path, realVersionsDir, executable)563+ ) {564+ return undefined;565+ }566+ const recorded = yield* fs567+ .readFileString(path.join(path.dirname(input.versionsDir), "launchers"))568+ .pipe(Effect.orElseSucceed(() => ""));569+ const candidates = [570+ // PATH first: it decides which t3 the user runs.571+ ...(environment["PATH"] ?? "").split(":").map((directory) => path.join(directory, "t3")),572+ path.join(573+ environment["T3CODE_INSTALL_BIN_DIR"] ||574+ path.join(yield* HostProcess.HomeDirectory, ".local", "bin"),575+ "t3",576+ ),577+ ...recorded.split(/\r?\n/),578+ ].filter((candidate) => path.isAbsolute(candidate));
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
- @@ -516,0 +555,21 @@555+ const realPath = (file: string) =>556+ fs.realPath(file).pipe(Effect.orElseSucceed(() => undefined));557+ const executable = yield* realPath(input.launchedAs);558+ const realVersionsDir = yield* realPath(input.versionsDir);559+ if (560+ executable === undefined ||561+ realVersionsDir === undefined ||562+ !isVersionExecutable(path, realVersionsDir, executable)563+ ) {564+ return undefined;565+ }566+ const recorded = yield* fs567+ .readFileString(path.join(path.dirname(input.versionsDir), "launchers"))568+ .pipe(Effect.orElseSucceed(() => ""));569+ const cwd = yield* HostProcess.WorkingDirectory;570+ const candidates = [571+ // PATH first, resolved like the shell does: it decides which t3 the user runs.572+ ...(environment["PATH"] ?? "")573+ .split(":")574+ .filter((directory) => directory.length > 0)575+ .map((directory) => path.resolve(cwd, directory, "t3")),@@ -189,8 +194,7 @@189194 () => Effect.succeedNone,190195 ),191196 );192- if (Option.isNone(linkTarget)) return;193- const relative = path.relative(194- versionsDir,195- path.resolve(path.dirname(launchedAs), linkTarget.value),196- );197+ // A legacy symlink, or the script install.sh and `t3 update` write.198+ const current = Option.isSome(linkTarget)199+ ? path.resolve(path.dirname(launchedAs), linkTarget.value)200+ : yield* readPosixLauncherTarget(launchedAs);
juliusmarminge added labelstakeover:ready
juliusmarminge removed labelstakeover:in-progress
juliusmarminge merged this7f7a1a4
juliusmarminge closed this
juliusmarminge removed labelstakeover:ready
Sign in to join this conversation.












macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed