History

Fix packaged CLI node_modules resolution

Rhys Sullivan committed 7 months ago

Commit 1f9eeef1 parent c1292971 file · +34 −20Browse files

Unified · Split

packages/platform/control-plane/src/runtime/local/tools.ts+34−20
@@ -1,7 +1,6 @@
11 import { createHash } from "node:crypto";
2−import { createRequire } from "node:module";
3−import { basename, dirname, extname, join, relative, resolve, sep } from "node:path";
4−import { pathToFileURL } from "node:url";
2+import { dirname, extname, join, relative, resolve, sep } from "node:path";
3+import { fileURLToPath, pathToFileURL } from "node:url";
54 import { FileSystem } from "@effect/platform";
65 import {
76 createToolCatalogFromTools,
@@ -331,29 +330,44 @@ const transpileSourceFile = (input: { · 323 unmodified lines
331330 return rewriteRelativeImportSpecifiers(transpiled.outputText);
332331 });
333332
334−const resolveExecutorNodeModulesDirectory = (): string | null => {
335− const require = createRequire(import.meta.url);
336− let current = dirname(require.resolve("typescript"));
333+const resolveExecutorNodeModulesDirectory = (): Effect.Effect<
334+ string | null,
335+ LocalFileSystemError,
336+ FileSystem.FileSystem
337+> =>
338+ Effect.gen(function* () {
339+ const fs = yield* FileSystem.FileSystem;
340+ let current = dirname(fileURLToPath(import.meta.url));
337341
338− while (true) {
339− if (basename(current) === "node_modules") {
340− return current;
341− }
342+ while (true) {
343+ const candidate = join(current, "node_modules");
344+ const exists = yield* fs
345+ .exists(candidate)
346+ .pipe(
347+ Effect.mapError(
348+ mapFileSystemError(
349+ candidate,
350+ "check executor node_modules directory",
351+ ),
352+ ),
353+ );
354+ if (exists) {
355+ return candidate;
356+ }
342357
343− const parent = dirname(current);
344− if (parent === current) {
345− return null;
358+ const parent = dirname(current);
359+ if (parent === current) {
360+ return null;
361+ }
362+ current = parent;
346363 }
347− current = parent;
348− }
349−};
350−
351−const EXECUTOR_NODE_MODULES_DIRECTORY = resolveExecutorNodeModulesDirectory();
364+ });
352365
353366 const ensureArtifactNodeModulesLink = (artifactRoot: string) =>
354367 Effect.gen(function* () {
355368 const fs = yield* FileSystem.FileSystem;
356369 const linkPath = join(artifactRoot, "node_modules");
370+ const executorNodeModulesDirectory = yield* resolveExecutorNodeModulesDirectory();
357371 const exists = yield* fs
358372 .exists(linkPath)
359373 .pipe(
@@ -365,12 +379,12 @@ const ensureArtifactNodeModulesLink = (artifactRoot: string) => · 5 unmodified lines
365379 return;
366380 }
367381
368− if (EXECUTOR_NODE_MODULES_DIRECTORY === null) {
382+ if (executorNodeModulesDirectory === null) {
369383 return;
370384 }
371385
372386 yield* fs
373− .symlink(EXECUTOR_NODE_MODULES_DIRECTORY, linkPath)
387+ .symlink(executorNodeModulesDirectory, linkPath)
374388 .pipe(
375389 Effect.mapError(
376390 mapFileSystemError(linkPath, "create local tool node_modules link"),