diff --git a/.changeset/beige-horses-juggle.md b/.changeset/beige-horses-juggle.md new file mode 100644 index 0000000000..9c2f5c40e2 --- /dev/null +++ b/.changeset/beige-horses-juggle.md @@ -0,0 +1,5 @@ +--- +"trigger.dev": patch +--- + +Added experimental_devProcessCwdInBuildDir config option to opt-in to new process.cwd behavior when executing tasks in the dev CLI. Currently process.cwd maps to the "root" of your trigger.dev project (the directory that contains your trigger.config.ts file). Setting experimental_devProcessCwdInBuildDir to true changes process.cwd to instead be the temporary build directory inside of the .trigger directory. diff --git a/packages/cli-v3/src/dev/devSupervisor.ts b/packages/cli-v3/src/dev/devSupervisor.ts index c55b906cc3..e86ea4eb45 100644 --- a/packages/cli-v3/src/dev/devSupervisor.ts +++ b/packages/cli-v3/src/dev/devSupervisor.ts @@ -353,6 +353,16 @@ class DevSupervisor implements WorkerRuntime { continue; } + logger.debug("[DevSupervisor] dequeueRuns. Creating run controller", { + run: message.run.friendlyId, + worker, + config: this.options.config, + }); + + const cwd = this.options.config.experimental_devProcessCwdInBuildDir + ? worker.build.outputPath + : undefined; + //new run runController = new DevRunController({ runFriendlyId: message.run.friendlyId, @@ -360,6 +370,7 @@ class DevSupervisor implements WorkerRuntime { httpClient: this.options.client, logLevel: this.options.args.logLevel, taskRunProcessPool: this.taskRunProcessPool, + cwd, onFinished: () => { logger.debug("[DevSupervisor] Run finished", { runId: message.run.friendlyId }); diff --git a/packages/cli-v3/src/dev/taskRunProcessPool.ts b/packages/cli-v3/src/dev/taskRunProcessPool.ts index d7491bf17d..1d0640e52d 100644 --- a/packages/cli-v3/src/dev/taskRunProcessPool.ts +++ b/packages/cli-v3/src/dev/taskRunProcessPool.ts @@ -47,7 +47,8 @@ export class TaskRunProcessPool { workerManifest: WorkerManifest, serverWorker: ServerBackgroundWorker, machineResources: MachinePresetResources, - env?: Record + env?: Record, + cwd?: string ): Promise<{ taskRunProcess: TaskRunProcess; isReused: boolean }> { const version = serverWorker.version || "unknown"; @@ -97,6 +98,7 @@ export class TaskRunProcessPool { version, availableCount, busyCount, + workerManifest, }); const newProcess = new TaskRunProcess({ @@ -107,7 +109,7 @@ export class TaskRunProcessPool { }, serverWorker, machineResources, - cwd: this.options.cwd, + cwd: cwd ?? this.options.cwd, }).initialize(); // Add to busy processes for this version diff --git a/packages/cli-v3/src/entryPoints/dev-run-controller.ts b/packages/cli-v3/src/entryPoints/dev-run-controller.ts index c52192783e..50d9a61719 100644 --- a/packages/cli-v3/src/entryPoints/dev-run-controller.ts +++ b/packages/cli-v3/src/entryPoints/dev-run-controller.ts @@ -31,6 +31,7 @@ type DevRunControllerOptions = { onSubscribeToRunNotifications: (run: Run, snapshot: Snapshot) => void; onUnsubscribeFromRunNotifications: (run: Run, snapshot: Snapshot) => void; onFinished: () => void; + cwd?: string; }; type Run = { @@ -50,6 +51,7 @@ export class DevRunController { private readonly heartbeatIntervalSeconds: number; private readonly snapshotPoller: IntervalService; private readonly snapshotPollIntervalSeconds: number; + private readonly cwd?: string; private state: | { @@ -77,7 +79,7 @@ export class DevRunController { this.worker = opts.worker; this.heartbeatIntervalSeconds = opts.heartbeatIntervalSeconds || 20; this.snapshotPollIntervalSeconds = 5; - + this.cwd = opts.cwd; this.httpClient = opts.httpClient; this.snapshotPoller = new IntervalService({ @@ -607,6 +609,10 @@ export class DevRunController { this.snapshotPoller.start(); + logger.debug("getProcess", { + build: this.opts.worker.build, + }); + // Get process from pool instead of creating new one const { taskRunProcess, isReused } = await this.opts.taskRunProcessPool.getProcess( this.opts.worker.manifest, @@ -621,7 +627,8 @@ export class DevRunController { TRIGGER_WORKER_MANIFEST_PATH: join(this.opts.worker.build.outputPath, "index.json"), RUN_WORKER_SHOW_LOGS: this.opts.logLevel === "debug" ? "true" : "false", TRIGGER_WORKER_VERSION: this.opts.worker.serverWorker?.version, - } + }, + this.cwd ); this.taskRunProcess = taskRunProcess; diff --git a/packages/core/src/v3/config.ts b/packages/core/src/v3/config.ts index 061b40fc40..838a464dcf 100644 --- a/packages/core/src/v3/config.ts +++ b/packages/core/src/v3/config.ts @@ -259,6 +259,14 @@ export type TriggerConfig = { devMaxPoolSize?: number; }; + /** + * @default false + * @description When running the dev CLI, set the current working directory to the build directory. + * + * Currently, the process.cwd() is set to the root of the project. + */ + experimental_devProcessCwdInBuildDir?: boolean; + /** * @deprecated Use `dirs` instead */