Skip to content

Commit 10f2e9d

Browse files
fix: handle invalid source map columns in Bun runtime
1 parent d35bf04 commit 10f2e9d

5 files changed

Lines changed: 75 additions & 26 deletions

File tree

packages/cli-v3/src/entryPoints/dev-index-worker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "@trigger.dev/core/v3/workers";
1515
import { sendMessageInCatalog, ZodSchemaParsedError } from "@trigger.dev/core/v3/zodMessageHandler";
1616
import { readFile } from "node:fs/promises";
17-
import { installSourceMapSupport } from "../utilities/sourceMaps.js";
17+
import { installSourceMapSupport } from "../utilities/installSourceMapSupport.js";
1818
import { registerResources } from "../indexing/registerResources.js";
1919
import { env } from "std-env";
2020
import { normalizeImportPath } from "../utilities/normalizeImportPath.js";

packages/cli-v3/src/entryPoints/dev-run-worker.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
SharedRuntimeManager,
4848
OtelTaskLogger,
4949
populateEnv,
50+
NO_FILE_CONTEXT,
5051
StandardLifecycleHooksManager,
5152
StandardLocalsManager,
5253
StandardMetadataManager,
@@ -67,7 +68,7 @@ import {
6768
import { ZodIpcConnection } from "@trigger.dev/core/v3/zodIpc";
6869
import { readFile } from "node:fs/promises";
6970
import { setInterval, setTimeout } from "node:timers/promises";
70-
import { installSourceMapSupport } from "../utilities/sourceMaps.js";
71+
import { installSourceMapSupport } from "../utilities/installSourceMapSupport.js";
7172
import { env } from "std-env";
7273
import { normalizeImportPath } from "../utilities/normalizeImportPath.js";
7374
import { VERSION } from "../version.js";
@@ -125,8 +126,9 @@ process.on("uncaughtException", function (error, origin) {
125126
}
126127
});
127128

128-
process.title = `trigger-dev-run-worker (${getEnvVar("TRIGGER_WORKER_VERSION") ?? "unknown version"
129-
})`;
129+
process.title = `trigger-dev-run-worker (${
130+
getEnvVar("TRIGGER_WORKER_VERSION") ?? "unknown version"
131+
})`;
130132

131133
const heartbeatIntervalMs = getEnvVar("HEARTBEAT_INTERVAL_MS");
132134

@@ -171,7 +173,7 @@ const standardRealtimeStreamsManager = new StandardRealtimeStreamsManager(
171173
apiClientManager.clientOrThrow(),
172174
getEnvVar("TRIGGER_STREAM_URL", getEnvVar("TRIGGER_API_URL")) ?? "https://api.trigger.dev",
173175
(getEnvVar("TRIGGER_STREAMS_DEBUG") === "1" || getEnvVar("TRIGGER_STREAMS_DEBUG") === "true") ??
174-
false
176+
false
175177
);
176178
realtimeStreams.setGlobalManager(standardRealtimeStreamsManager);
177179

@@ -322,12 +324,12 @@ async function doBootstrap() {
322324

323325
let bootstrapCache:
324326
| {
325-
tracer: TriggerTracer;
326-
tracingSDK: TracingSDK;
327-
consoleInterceptor: ConsoleInterceptor;
328-
config: TriggerConfig;
329-
workerManifest: WorkerManifest;
330-
}
327+
tracer: TriggerTracer;
328+
tracingSDK: TracingSDK;
329+
consoleInterceptor: ConsoleInterceptor;
330+
config: TriggerConfig;
331+
workerManifest: WorkerManifest;
332+
}
331333
| undefined;
332334

333335
async function bootstrap() {
@@ -496,8 +498,8 @@ const zodIpc = new ZodIpcConnection({
496498
async () => {
497499
const beforeImport = performance.now();
498500
resourceCatalog.setCurrentFileContext(
499-
taskManifest.entryPoint,
500-
taskManifest.filePath
501+
taskManifest.filePath,
502+
taskManifest.entryPoint
501503
);
502504

503505
// Load init file if it exists
@@ -605,6 +607,12 @@ const zodIpc = new ZodIpcConnection({
605607

606608
const signal = AbortSignal.any([_cancelController.signal, timeoutController.signal]);
607609

610+
// Sentinel context so `task()` calls firing during run / lifecycle
611+
// hooks (e.g. via `await import(...)` of a module containing a task
612+
// definition) register normally instead of being silently dropped.
613+
// Cleared in the surrounding finally below.
614+
resourceCatalog.setCurrentFileContext(NO_FILE_CONTEXT, NO_FILE_CONTEXT);
615+
608616
const { result } = await executor.execute(execution, ctx, signal);
609617

610618
if (_isRunning && !_isCancelled) {
@@ -623,6 +631,7 @@ const zodIpc = new ZodIpcConnection({
623631
}
624632
} finally {
625633
standardHeartbeatsManager.stopHeartbeat();
634+
resourceCatalog.clearCurrentFileContext();
626635

627636
_execution = undefined;
628637
_isRunning = false;

packages/cli-v3/src/entryPoints/managed-index-worker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "@trigger.dev/core/v3/workers";
1515
import { sendMessageInCatalog, ZodSchemaParsedError } from "@trigger.dev/core/v3/zodMessageHandler";
1616
import { readFile } from "node:fs/promises";
17-
import { installSourceMapSupport } from "../utilities/sourceMaps.js";
17+
import { installSourceMapSupport } from "../utilities/installSourceMapSupport.js";
1818
import { registerResources } from "../indexing/registerResources.js";
1919
import { env } from "std-env";
2020
import { normalizeImportPath } from "../utilities/normalizeImportPath.js";
@@ -190,8 +190,8 @@ await sendMessageInCatalog(
190190
typeof processKeepAlive === "object"
191191
? processKeepAlive
192192
: typeof processKeepAlive === "boolean"
193-
? { enabled: processKeepAlive }
194-
: undefined,
193+
? { enabled: processKeepAlive }
194+
: undefined,
195195
timings,
196196
},
197197
importErrors,

packages/cli-v3/src/entryPoints/managed-run-worker.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import {
4747
OtelTaskLogger,
4848
populateEnv,
4949
ProdUsageManager,
50+
NO_FILE_CONTEXT,
5051
StandardLifecycleHooksManager,
5152
StandardLocalsManager,
5253
StandardMetadataManager,
@@ -67,7 +68,7 @@ import {
6768
import { ZodIpcConnection } from "@trigger.dev/core/v3/zodIpc";
6869
import { readFile } from "node:fs/promises";
6970
import { setInterval, setTimeout } from "node:timers/promises";
70-
import { installSourceMapSupport } from "../utilities/sourceMaps.js";
71+
import { installSourceMapSupport } from "../utilities/installSourceMapSupport.js";
7172
import { env } from "std-env";
7273
import { normalizeImportPath } from "../utilities/normalizeImportPath.js";
7374
import { VERSION } from "../version.js";
@@ -145,7 +146,7 @@ const standardRealtimeStreamsManager = new StandardRealtimeStreamsManager(
145146
apiClientManager.clientOrThrow(),
146147
getEnvVar("TRIGGER_STREAM_URL", getEnvVar("TRIGGER_API_URL")) ?? "https://api.trigger.dev",
147148
(getEnvVar("TRIGGER_STREAMS_DEBUG") === "1" || getEnvVar("TRIGGER_STREAMS_DEBUG") === "true") ??
148-
false
149+
false
149150
);
150151
realtimeStreams.setGlobalManager(standardRealtimeStreamsManager);
151152

@@ -298,12 +299,12 @@ async function doBootstrap() {
298299

299300
let bootstrapCache:
300301
| {
301-
tracer: TriggerTracer;
302-
tracingSDK: TracingSDK;
303-
consoleInterceptor: ConsoleInterceptor;
304-
config: TriggerConfig;
305-
workerManifest: WorkerManifest;
306-
}
302+
tracer: TriggerTracer;
303+
tracingSDK: TracingSDK;
304+
consoleInterceptor: ConsoleInterceptor;
305+
config: TriggerConfig;
306+
workerManifest: WorkerManifest;
307+
}
307308
| undefined;
308309

309310
async function bootstrap() {
@@ -486,8 +487,8 @@ const zodIpc = new ZodIpcConnection({
486487
async () => {
487488
const beforeImport = performance.now();
488489
resourceCatalog.setCurrentFileContext(
489-
taskManifest.entryPoint,
490-
taskManifest.filePath
490+
taskManifest.filePath,
491+
taskManifest.entryPoint
491492
);
492493

493494
// Load init file if it exists
@@ -591,6 +592,12 @@ const zodIpc = new ZodIpcConnection({
591592

592593
const signal = AbortSignal.any([_cancelController.signal, timeoutController.signal]);
593594

595+
// Sentinel context so `task()` calls firing during run / lifecycle
596+
// hooks (e.g. via `await import(...)` of a module containing a task
597+
// definition) register normally instead of being silently dropped.
598+
// Cleared in the surrounding finally below.
599+
resourceCatalog.setCurrentFileContext(NO_FILE_CONTEXT, NO_FILE_CONTEXT);
600+
594601
const { result } = await executor.execute(execution, ctx, signal);
595602

596603
if (_isRunning && !_isCancelled) {
@@ -609,6 +616,7 @@ const zodIpc = new ZodIpcConnection({
609616
}
610617
} finally {
611618
standardHeartbeatsManager.stopHeartbeat();
619+
resourceCatalog.clearCurrentFileContext();
612620

613621
_execution = undefined;
614622
_isRunning = false;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sourceMapSupport from "source-map-support";
2+
3+
/**
4+
* Installs source-map-support with a workaround for Bun's source map handling.
5+
*
6+
* Bun's runtime can produce source maps with column values of -1, which causes
7+
* source-map@0.6.1 (used by source-map-support) to throw:
8+
* "Column must be greater than or equal to 0, got -1"
9+
*
10+
* This wraps the prepareStackTrace hook so that if source map processing fails,
11+
* it falls back to default stack trace formatting instead of crashing.
12+
*
13+
* See: https://github.com/oven-sh/bun/issues/8087
14+
*/
15+
export function installSourceMapSupport() {
16+
sourceMapSupport.install({
17+
handleUncaughtExceptions: false,
18+
environment: "node",
19+
hookRequire: false,
20+
});
21+
22+
const _prepareStackTrace = (Error as any).prepareStackTrace;
23+
if (_prepareStackTrace) {
24+
(Error as any).prepareStackTrace = (error: Error, stackTraces: NodeJS.CallSite[]) => {
25+
try {
26+
return _prepareStackTrace(error, stackTraces);
27+
} catch {
28+
return error + "\n" + stackTraces.map((s) => " at " + s).join("\n");
29+
}
30+
};
31+
}
32+
}

0 commit comments

Comments
 (0)