diff --git a/apps/server/src/cli/install.test.ts b/apps/server/src/cli/install.test.ts index f7d8b1df86d76..2578d58a042c1 100644 --- a/apps/server/src/cli/install.test.ts +++ b/apps/server/src/cli/install.test.ts @@ -70,15 +70,36 @@ it.layer(NodeServices.layer)("t3 install", (it) => { const onPath = yield* provide(installCli(), { PATH: `/usr/bin:${path.join(root, "bin")}/` }); assert.isTrue(onPath.onPath); // Another t3 earlier on PATH still runs first, so `t3` would not run this one. + const binDir = path.join(root, "bin"); const otherBin = path.join(root, "other-bin"); yield* fs.makeDirectory(otherBin); yield* fs.writeFileString(path.join(otherBin, "t3"), "", { mode: 0o755 }); - const shadowed = yield* provide(installCli(), { - PATH: `${otherBin}:${path.join(root, "bin")}`, - }); - assert.isFalse(shadowed.onPath); - assert.equal(shadowed.shadowedBy, path.join(otherBin, "t3")); - assert.isUndefined(shadowed.pathCommand); + const installIn = (cwd: string, PATH: string) => + provide(installCli().pipe(Effect.provideService(HostProcess.WorkingDirectory, cwd)), { + PATH, + }); + const assertShadowed = (result: Effect.Success>) => { + assert.isFalse(result.onPath); + assert.equal(result.shadowedBy, path.join(otherBin, "t3")); + assert.isUndefined(result.pathCommand); + }; + assertShadowed(yield* provide(installCli(), { PATH: `${otherBin}:${binDir}` })); + // Relative and empty entries resolve against the working directory. + assertShadowed(yield* installIn(root, `other-bin:${binDir}`)); + assertShadowed(yield* installIn(otherBin, `:${binDir}`)); + // An empty PATH is not the working directory. + const offPath = yield* installIn(otherBin, ""); + assert.isUndefined(offPath.shadowedBy); + assert.isDefined(offPath.pathCommand); + // A symlink to binDir, or a relative entry to it that wins first, is this install. + yield* fs.remove(alias); + yield* fs.symlink(binDir, alias); + for (const pathEnv of [`${alias}:${otherBin}`, `bin:other-bin:${binDir}`]) { + const installedOnPath = yield* installIn(root, pathEnv); + assert.isTrue(installedOnPath.onPath); + assert.isUndefined(installedOnPath.shadowedBy); + } + yield* fs.remove(alias); yield* fs.remove(path.join(otherBin, "t3")); yield* fs.makeDirectory(path.join(otherBin, "t3")); const directory = yield* provide(installCli(), { diff --git a/apps/server/src/cli/install.ts b/apps/server/src/cli/install.ts index b789930b2cfbb..abd32cf0d844d 100644 --- a/apps/server/src/cli/install.ts +++ b/apps/server/src/cli/install.ts @@ -146,28 +146,37 @@ export const installCli = Effect.fn("cli.install.install")(function* () { const normalize = (value: string) => windows ? path.resolve(value).toLowerCase() : path.resolve(value); const same = (entry: string) => entry !== "" && normalize(entry) === normalize(binDir); - const entries = (environment.PATH ?? environment.Path ?? "").split(windows ? ";" : ":"); - const onPath = entries.some(same); + const rawPath = environment.PATH ?? environment.Path ?? ""; + const entries = rawPath === "" ? [] : rawPath.split(windows ? ";" : ":"); + let cwd = yield* Effect.catchDefect(HostProcess.WorkingDirectory, () => Effect.undefined); // A t3 earlier on PATH runs instead, and an added folder goes last. Runner caches (npx's own // .bin) are not installs. const firstOther = Effect.gen(function* () { for (const entry of entries) { - if (same(entry)) return undefined; - if (!path.isAbsolute(entry) || detectCliRunner(`${entry}/`) !== null) continue; + const absolute = path.isAbsolute(entry); + if ((windows || absolute) && same(entry)) return null; + const dir = absolute ? entry : windows || !cwd ? undefined : path.resolve(cwd, entry); + if (dir === undefined || detectCliRunner(`${dir}/`) !== null) continue; for (const name of windows ? ["t3.cmd", "t3.exe"] : ["t3"]) { - const candidate = path.join(entry, name); - if (yield* resolveCommandPath(candidate, { env: environment })) return candidate; + const candidate = path.join(dir, name); + if (!(yield* resolveCommandPath(candidate, { env: environment }))) continue; + if (windows) return candidate; + const resolved = yield* fs.realPath(candidate).pipe(Effect.orElseSucceed(() => candidate)); + if (normalize(resolved) !== normalize(recorded)) return candidate; + if (absolute) return null; + cwd = undefined; // Ours already won in cwd; later relative entries lose. } } return undefined; }); - const shadowedBy = yield* firstOther; + // null: this launcher runs first; a path: another t3 does; undefined: no t3 on PATH. + const found = yield* firstOther; return { link, - onPath: onPath && shadowedBy === undefined, - shadowedBy, + onPath: found === null, + shadowedBy: found ?? undefined, pathCommand: - onPath || shadowedBy !== undefined + found !== undefined ? undefined : windows ? // PowerShell; it covers this window only, since changing the saved PATH would flatten