Skip to content

Open source CapCut alternative.

TypeScript

fix(site): bundled tools resolve on every Cut API surface via one widened PATH [rebuild]

David Le520d5d545bf4
diff --git a/site/src/cut/engine/serve.ts b/site/src/cut/engine/serve.ts
index ef3ed88e..775b1c8e 100644
--- a/site/src/cut/engine/serve.ts
+++ b/site/src/cut/engine/serve.ts
@@ -3,8 +3,8 @@ import { Readable } from "node:stream";
import { allowedOrigin, corsHeaders, preflightHeaders } from "../server/cors";
import { matchCutRoute } from "../server/http/routes";
+import { ensureToolPath, resolveOnPath } from "../server/tool-path";
import { enginePort } from "./config";
-import { resolveOnPath, widenPath } from "./paths";
// Throws (and exits with a clear message) on a bad DONKEY_CUT_PORT rather than
// binding a random port the client would never find.
@@ -73,7 +73,7 @@ function exitWithParent() {
async function start() {
exitWithParent();
- await widenPath();
+ await ensureToolPath();
// The Agent SDK can't resolve its built-in CLI from inside a compiled
// binary; point it at the user's own Claude Code install (their login is
diff --git a/site/src/cut/server/http/next.ts b/site/src/cut/server/http/next.ts
index 9b863f6b..dee4b3dd 100644
--- a/site/src/cut/server/http/next.ts
+++ b/site/src/cut/server/http/next.ts
@@ -1,4 +1,5 @@
import { hostedApiBlock } from "../local-only";
+import { ensureToolPath } from "../tool-path";
import { matchCutRoute } from "./routes";
/**
@@ -12,6 +13,10 @@ export async function cutCatchAll(req: Request): Promise<Response> {
const blocked = hostedApiBlock();
if (blocked) return blocked;
+ // The dev server spawns tools (yt-dlp, ffmpeg, …) in-process, so it needs
+ // the same widened PATH the packaged engine builds at startup.
+ await ensureToolPath();
+
const { pathname } = new URL(req.url);
const match = matchCutRoute(req.method, pathname);
if (!match) return new Response("Not found.", { status: 404 });
diff --git a/site/src/cut/engine/paths.ts b/site/src/cut/server/tool-path.ts
similarity index 58%
rename from site/src/cut/engine/paths.ts
rename to site/src/cut/server/tool-path.ts
index b26d1b7d..6325017c 100644
--- a/site/src/cut/engine/paths.ts
+++ b/site/src/cut/server/tool-path.ts
@@ -2,7 +2,7 @@ import { execFile } from "node:child_process";
import os from "node:os";
import path from "node:path";
-import { findOnPath } from "../server/util";
+import { exists, findOnPath } from "./util";
/** Directories a GUI-spawned process misses but user CLIs commonly live in. */
const COMMON_BIN_DIRS = [
@@ -30,14 +30,15 @@ function loginShellPath(): Promise<string | null> {
});
}
-/**
- * The engine is spawned by an app, not a terminal, so it inherits a bare
- * PATH. Rebuild it: tools shipped beside the engine binary first (they
- * version with the app), then the app's bundled tools (bundled tool always
- * wins), then the user's login-shell PATH, common install dirs, and whatever
- * was already there.
- */
-export async function widenPath(): Promise<void> {
+/** The repo's vendor/donkey-tools when running from a source checkout (the
+ * Next dev server and `engine:dev` both run with cwd = site/). The packaged
+ * app has no such directory, so this resolves to nothing there. */
+async function devVendorToolsDir(): Promise<string | null> {
+ const dir = path.resolve(process.cwd(), "..", "vendor", "donkey-tools");
+ return (await exists(dir)) ? dir : null;
+}
+
+async function widenPath(): Promise<void> {
const parts: string[] = [];
const push = (p?: string | null) => {
if (!p) return;
@@ -47,11 +48,27 @@ export async function widenPath(): Promise<void> {
};
push(path.dirname(process.execPath));
push(process.env.DONKEY_CUT_TOOLS_DIR);
+ push(await devVendorToolsDir());
push(await loginShellPath());
for (const dir of COMMON_BIN_DIRS) push(dir);
push(process.env.PATH);
process.env.PATH = parts.join(":");
}
+let widened: Promise<void> | null = null;
+
+/**
+ * Rebuild PATH so bundled tools (yt-dlp, ffmpeg, …) resolve identically on
+ * every Cut API surface: tools shipped beside the engine binary first (they
+ * version with the app), then the app's bundled tools (bundled tool always
+ * wins), the repo's vendor tools in dev, the user's login-shell PATH, common
+ * install dirs, and whatever was already there. Memoized — the engine runs it
+ * at startup, the Next dev server on the first API request.
+ */
+export function ensureToolPath(): Promise<void> {
+ widened ??= widenPath();
+ return widened;
+}
+
/** First executable named `name` on the (widened) PATH, or null. */
export const resolveOnPath = findOnPath;
diff --git a/site/src/cut/server/urlImport.ts b/site/src/cut/server/urlImport.ts
index ee30daeb..1890be8a 100644
--- a/site/src/cut/server/urlImport.ts
+++ b/site/src/cut/server/urlImport.ts
@@ -8,7 +8,7 @@ import { mediaDir, mediaPath as projectMediaPath, readProject } from "./projects
import { uniqueName } from "./util";
// Download a media URL (TikTok, YouTube, Instagram, …) with the bundled
-// yt-dlp. The engine finds yt-dlp on its widened PATH exactly like ffmpeg.
+// yt-dlp, resolved from the widened PATH (tool-path.ts) exactly like ffmpeg.
const MEDIA_EXT = /\.(mp4|mov|m4v|webm|mkv|mp3|m4a|aac|wav|ogg|flac)$/i;