Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeFS from "node:fs";
3import * as NodePath from "node:path";
4
5import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
6import { SpawnExecutableResolution } from "@t3tools/shared/shell";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9
10/**
11 * Windows launcher-script extensions that Node cannot spawn without a shell
12 * (`spawn EINVAL` since Node 20.12) and that the Claude Agent SDK therefore
13 * cannot use as `pathToClaudeCodeExecutable`.
14 */
15const WINDOWS_SHIM_EXTENSIONS: ReadonlySet<string> = new Set([".cmd", ".bat", ".ps1"]);
16
17/**
18 * Entry points of the npm `@anthropic-ai/claude-code` package relative to the
19 * global `node_modules` directory that sits next to the npm launcher shim.
20 * Newer package versions ship a native `bin/claude.exe`; older versions only
21 * ship `cli.js`, which the SDK runs with a JavaScript runtime.
22 */
23const NPM_PACKAGE_ENTRY_CANDIDATES = [
24 ["node_modules", "@anthropic-ai", "claude-code", "bin", "claude.exe"],
25 ["node_modules", "@anthropic-ai", "claude-code", "cli.js"],
26] as const;
27
28export type ExecutableFileCheck = (filePath: string) => boolean;
29
30function isExistingFile(filePath: string): boolean {
31 try {
32 return NodeFS.statSync(filePath).isFile();
33 } catch {
34 return false;
35 }
36}
37
38/** Injectable file-existence check so tests can run against a fake filesystem. */
39export const ClaudeExecutableFileCheck = Context.Reference<ExecutableFileCheck>(
40 "server/provider/Drivers/ClaudeExecutableFileCheck",
41 {
42 defaultValue: () => isExistingFile,
43 },
44);
45
46/**
47 * Resolves the configured Claude binary path into a value the Claude Agent
48 * SDK can spawn directly via `pathToClaudeCodeExecutable`.
49 *
50 * The SDK spawns the given path without a shell and without Windows PATH /
51 * PATHEXT resolution, so a bare command name like `claude` fails with
52 * "native binary not found" and an npm `claude.cmd` shim fails with
53 * `spawn EINVAL`. CLI probes avoid this via `resolveSpawnCommand`, which can
54 * fall back to `shell: true`; the SDK offers no such escape hatch.
55 *
56 * On Windows this resolves the command against PATH/PATHEXT and, when the
57 * result is an npm launcher shim, follows it to the real package entry
58 * (`bin/claude.exe`, or `cli.js` for older package versions). On other
59 * platforms the configured value is returned unchanged.
60 */
61export const resolveClaudeSdkExecutablePath = Effect.fn("resolveClaudeSdkExecutablePath")(
62 function* (binaryPath: string, environment: NodeJS.ProcessEnv): Effect.fn.Return<string> {
63 const platform = yield* HostProcessPlatform;
64 if (platform !== "win32") {
65 return binaryPath;
66 }
67
68 const resolveExecutable = yield* SpawnExecutableResolution;
69 const isFile = yield* ClaudeExecutableFileCheck;
70 const resolved = resolveExecutable(binaryPath, platform, environment) ?? binaryPath;
71 const extension = NodePath.win32.extname(resolved).toLowerCase();
72 if (!WINDOWS_SHIM_EXTENSIONS.has(extension)) {
73 return resolved;
74 }
75
76 const shimDirectory = NodePath.win32.dirname(resolved);
77 for (const entrySegments of NPM_PACKAGE_ENTRY_CANDIDATES) {
78 const candidate = NodePath.win32.join(shimDirectory, ...entrySegments);
79 if (isFile(candidate)) {
80 return candidate;
81 }
82 }
83
84 yield* Effect.logWarning(
85 "Claude launcher shim resolved but no known package entry was found next to it; the Claude Agent SDK cannot spawn launcher scripts directly.",
86 { binaryPath, resolvedShimPath: resolved },
87 );
88 return binaryPath;
89 },
90);