fix(server): return null for unresolved optional commands (#18871)

This commit is contained in:
Yash Singh authored and GitHub committed 2026-10-12 20:04:34 -05:00
1 parent 8110edfddd
commit e2e95a39d3
9 files changed
+140 -52

No files matched your search

+3 -3
View File
@@ -76,15 +76,15 @@ export const resolveNodeExecutable = Effect.fn("nodeRuntime.resolveNodeExecutabl
const path = yield* Path.Path;
const platform = yield* HostProcess.Platform;
const env = environment ?? (yield* HostProcess.Environment);
const nodePath = yield* resolveCommandPath(platform === "win32" ? "node.exe" : "node", {
const resolvedNodePath = yield* resolveCommandPath(platform === "win32" ? "node.exe" : "node", {
// Batch wrappers require a shell; helper callers launch the runtime directly.
env: platform === "win32" ? { ...env, PATHEXT: ".EXE" } : env,
}).pipe(
// Refresh immediately after the user installs Node and retries setup.
Effect.provideService(CommandResolutionCache, new Map()),
Effect.map((commandPath) => path.resolve(commandPath)),
Effect.mapError((cause) => new NodeRuntimeUnavailableError({ feature, cause })),
);
if (resolvedNodePath === null) return yield* new NodeRuntimeUnavailableError({ feature });
const nodePath = path.resolve(resolvedNodePath);
// A launcher or symlink named node must not point back at the standalone app.
const resolvedPath = yield* fs
.realPath(nodePath)
+97 -11
View File
@@ -2,9 +2,12 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import { it as effectIt } from "@effect/vitest";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Result from "effect/Result";
import * as TestClock from "effect/testing/TestClock";
import * as Tracer from "effect/Tracer";
import { describe, expect, it, vi } from "vite-plus/test";
import {
@@ -41,6 +44,19 @@ const withWindowsEnvironmentMocks = <A, E, R>(
Effect.provideService(CommandAvailability, commandAvailable),
);
const recordCommandTrace = Effect.fnUntraced(function* <A, E, R>(effect: Effect.Effect<A, E, R>) {
const spans: Array<Tracer.NativeSpan> = [];
const tracer = Tracer.make({
span: (options) => {
const span = new Tracer.NativeSpan(options);
spans.push(span);
return span;
},
});
const result = yield* effect.pipe(Effect.withTracer(tracer), Effect.result);
return { result, spans };
});
describe("readPathFromLoginShell", () => {
it("uses a shell-agnostic printenv PATH probe", () => {
const execFile = vi.fn<
@@ -338,6 +354,65 @@ describe("resolveKnownWindowsCliDirs", () => {
});
effectIt.layer(NodeServices.layer)("isCommandAvailable", (it) => {
it.effect.each(["win32", "linux"] as const)(
"records missing optional commands as successful lookups on %s",
(platform) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-command-trace-" });
const env = { PATH: cwd, PATHEXT: ".CMD" };
const checks = [
isCommandAvailable("missing", { env }),
isCommandAvailable("missing", { env }),
isCommandAvailable("missing", { env: { ...env, PATH: "" } }),
isCommandAvailable(path.join(cwd, "missing.cmd"), { env }),
];
for (const check of checks) {
const { result, spans } = yield* recordCommandTrace(check);
expect(Result.getOrThrow(result)).toBe(false);
expect(spans.length).toBeGreaterThan(0);
for (const span of spans) {
expect(span.status._tag).toBe("Ended");
if (span.status._tag === "Ended") {
expect(Exit.isSuccess(span.status.exit), span.name).toBe(true);
}
}
}
}).pipe(
Effect.provideService(HostProcess.Platform, platform),
Effect.provideService(CommandResolutionCache, new Map()),
),
);
it.effect.each(["win32", "linux"] as const)(
"records available commands as successful lookups on %s",
(platform) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const cwd = yield* fs.makeTempDirectoryScoped({ prefix: "t3-command-trace-" });
const executable = path.join(cwd, platform === "win32" ? "available.cmd" : "available");
yield* fs.writeFileString(executable, "");
yield* fs.chmod(executable, 0o700);
const env = { PATH: cwd, PATHEXT: ".CMD" };
for (const command of ["available", executable]) {
const { result, spans } = yield* recordCommandTrace(isCommandAvailable(command, { env }));
expect(Result.getOrThrow(result)).toBe(true);
expect(spans.length).toBeGreaterThan(0);
for (const span of spans) {
expect(span.status._tag).toBe("Ended");
if (span.status._tag === "Ended") {
expect(Exit.isSuccess(span.status.exit), span.name).toBe(true);
}
}
}
}).pipe(
Effect.provideService(HostProcess.Platform, platform),
Effect.provideService(CommandResolutionCache, new Map()),
),
);
it.effect("returns false when PATH is empty", () =>
Effect.gen(function* () {
expect(
@@ -350,13 +425,29 @@ effectIt.layer(NodeServices.layer)("isCommandAvailable", (it) => {
});
effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
it.effect("fails when PATH is empty", () =>
it.effect("records a successful lookup when a command is missing", () =>
Effect.gen(function* () {
const { result, spans } = yield* recordCommandTrace(
resolveCommandPath("missing", { env: { PATH: "" } }),
);
expect(Result.getOrThrow(result)).toBeNull();
expect(spans.length).toBeGreaterThan(0);
for (const span of spans) {
expect(span.status._tag).toBe("Ended");
if (span.status._tag === "Ended") {
expect(Exit.isSuccess(span.status.exit), span.name).toBe(true);
}
}
}),
);
it.effect("returns null when PATH is empty", () =>
Effect.gen(function* () {
const result = yield* resolveCommandPath("definitely-not-installed", {
env: { PATH: "", PATHEXT: ".COM;.EXE;.BAT;.CMD" },
}).pipe(Effect.provideService(HostProcess.Platform, "win32"), Effect.result);
}).pipe(Effect.provideService(HostProcess.Platform, "win32"));
expect(result._tag).toBe("Failure");
expect(result).toBeNull();
}),
);
@@ -379,10 +470,9 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
}),
}),
),
Effect.result,
);
expect(result._tag).toBe("Failure");
expect(result).toBeNull();
return probed;
});
@@ -448,13 +538,9 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
const executable = path.join(cwd, "appeared.CMD");
const options = { env: { PATH: `${cwd};${cwd}`, PATHEXT: ".CMD" } };
expect((yield* resolveCommandPath("appeared", options).pipe(Effect.result))._tag).toBe(
"Failure",
);
expect(yield* resolveCommandPath("appeared", options)).toBeNull();
yield* fs.writeFileString(executable, "@echo off\n");
expect((yield* resolveCommandPath("appeared", options).pipe(Effect.result))._tag).toBe(
"Failure",
);
expect(yield* resolveCommandPath("appeared", options)).toBeNull();
expect(yield* resolveCommandPath(executable, options)).toBe(executable);
yield* TestClock.adjust("30 seconds");
expect(yield* resolveCommandPath("appeared", options)).toBe(executable);
+5 -17
View File
@@ -5,7 +5,6 @@ import * as NodeChildProcess from "node:child_process";
import * as NodeFS from "node:fs";
import * as Cache from "effect/Cache";
import * as Clock from "effect/Clock";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Option from "effect/Option";
@@ -43,11 +42,6 @@ export type CommandAvailabilityChecker = (
options?: CommandAvailabilityOptions,
) => Effect.Effect<boolean, never, FileSystem.FileSystem | Path.Path>;
export class CommandResolutionError extends Data.TaggedError("CommandResolutionError")<{
readonly command: string;
readonly reason: "not-found";
}> {}
const WINDOWS_SHELL_META_CHARS = /([()\][%!^"`<>&|;, *?])/g;
/**
@@ -612,7 +606,7 @@ const isExecutableFile = Effect.fnUntraced(function* (
const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlatform")(function* (
command: string,
options: CommandAvailabilityOptions & { readonly platform: NodeJS.Platform },
): Effect.fn.Return<string, CommandResolutionError, FileSystem.FileSystem | Path.Path> {
): Effect.fn.Return<string | null, never, FileSystem.FileSystem | Path.Path> {
const path = yield* Path.Path;
const platform = options.platform;
const env = options.env ?? process.env;
@@ -630,12 +624,12 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
return candidate;
}
}
return yield* new CommandResolutionError({ command, reason: "not-found" });
return null;
}
const pathValue = resolvePathEnvironmentVariable(env);
if (pathValue.length === 0) {
return yield* new CommandResolutionError({ command, reason: "not-found" });
return null;
}
const cacheKey = [platform, pathValue, windowsPathExtensions.join(";"), command].join(
@@ -645,9 +639,6 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
const nowNanos = yield* Clock.monotonicTimeNanos;
const cached = cache.get(cacheKey);
if (cached !== undefined && cached.expiresAtNanos > nowNanos) {
if (cached.resolvedPath === null) {
return yield* new CommandResolutionError({ command, reason: "not-found" });
}
return cached.resolvedPath;
}
@@ -680,7 +671,7 @@ const resolveCommandPathForPlatform = Effect.fn("shell.resolveCommandPathForPlat
}
}
cacheCommandResolution(cache, cacheKey, null, nowNanos);
return yield* new CommandResolutionError({ command, reason: "not-found" });
return null;
});
export const resolveCommandPath = Effect.fn("shell.resolveCommandPath")(function* (
@@ -800,10 +791,7 @@ export const isCommandAvailable = Effect.fn("shell.isCommandAvailable")(function
command: string,
options: CommandAvailabilityOptions = {},
) {
return yield* resolveCommandPath(command, options).pipe(
Effect.as(true),
Effect.catchTags({ CommandResolutionError: () => Effect.succeed(false) }),
);
return (yield* resolveCommandPath(command, options)) !== null;
});
export function resolveKnownWindowsCliDirs(env: NodeJS.ProcessEnv): ReadonlyArray<string> {