@@ -388,66 +388,74 @@ const zodIpc = new ZodIpcConnection({
388388 return ;
389389 }
390390
391- try {
392- await runTimelineMetrics . measureMetric (
393- "trigger.dev/start" ,
394- "import" ,
395- {
396- entryPoint : taskManifest . entryPoint ,
397- file : taskManifest . filePath ,
398- } ,
399- async ( ) => {
400- const beforeImport = performance . now ( ) ;
401- resourceCatalog . setCurrentFileContext ( taskManifest . entryPoint , taskManifest . filePath ) ;
402-
403- // Load init file if it exists
404- if ( workerManifest . initEntryPoint ) {
405- try {
406- await import ( normalizeImportPath ( workerManifest . initEntryPoint ) ) ;
407- console . log ( `Loaded init file from ${ workerManifest . initEntryPoint } ` ) ;
408- } catch ( err ) {
409- console . error ( `Failed to load init file` , err ) ;
410- throw err ;
391+ // Import the task module
392+ let task = resourceCatalog . getTask ( execution . task . id ) ;
393+
394+ if ( ! task ) {
395+ try {
396+ await runTimelineMetrics . measureMetric (
397+ "trigger.dev/start" ,
398+ "import" ,
399+ {
400+ entryPoint : taskManifest . entryPoint ,
401+ file : taskManifest . filePath ,
402+ } ,
403+ async ( ) => {
404+ const beforeImport = performance . now ( ) ;
405+ resourceCatalog . setCurrentFileContext (
406+ taskManifest . entryPoint ,
407+ taskManifest . filePath
408+ ) ;
409+
410+ // Load init file if it exists
411+ if ( workerManifest . initEntryPoint ) {
412+ try {
413+ await import ( normalizeImportPath ( workerManifest . initEntryPoint ) ) ;
414+ console . log ( `Loaded init file from ${ workerManifest . initEntryPoint } ` ) ;
415+ } catch ( err ) {
416+ console . error ( `Failed to load init file` , err ) ;
417+ throw err ;
418+ }
411419 }
412- }
413420
414- await import ( normalizeImportPath ( taskManifest . entryPoint ) ) ;
415- resourceCatalog . clearCurrentFileContext ( ) ;
416- const durationMs = performance . now ( ) - beforeImport ;
421+ await import ( normalizeImportPath ( taskManifest . entryPoint ) ) ;
422+ resourceCatalog . clearCurrentFileContext ( ) ;
423+ const durationMs = performance . now ( ) - beforeImport ;
417424
418- console . log (
419- `Imported task ${ execution . task . id } [${ taskManifest . entryPoint } ] in ${ durationMs } ms`
420- ) ;
421- }
422- ) ;
423- } catch ( err ) {
424- console . error ( `Failed to import task ${ execution . task . id } ` , err ) ;
425+ console . log (
426+ `Imported task ${ execution . task . id } [${ taskManifest . entryPoint } ] in ${ durationMs } ms`
427+ ) ;
428+ }
429+ ) ;
430+ } catch ( err ) {
431+ console . error ( `Failed to import task ${ execution . task . id } ` , err ) ;
425432
426- await sender . send ( "TASK_RUN_COMPLETED" , {
427- execution,
428- result : {
429- ok : false ,
430- id : execution . run . id ,
431- error : {
432- type : "INTERNAL_ERROR" ,
433- code : TaskRunErrorCodes . COULD_NOT_IMPORT_TASK ,
434- message : err instanceof Error ? err . message : String ( err ) ,
435- stackTrace : err instanceof Error ? err . stack : undefined ,
436- } ,
437- usage : {
438- durationMs : 0 ,
433+ await sender . send ( "TASK_RUN_COMPLETED" , {
434+ execution,
435+ result : {
436+ ok : false ,
437+ id : execution . run . id ,
438+ error : {
439+ type : "INTERNAL_ERROR" ,
440+ code : TaskRunErrorCodes . COULD_NOT_IMPORT_TASK ,
441+ message : err instanceof Error ? err . message : String ( err ) ,
442+ stackTrace : err instanceof Error ? err . stack : undefined ,
443+ } ,
444+ usage : {
445+ durationMs : 0 ,
446+ } ,
447+ metadata : runMetadataManager . stopAndReturnLastFlush ( ) ,
439448 } ,
440- metadata : runMetadataManager . stopAndReturnLastFlush ( ) ,
441- } ,
442- } ) ;
449+ } ) ;
443450
444- return ;
445- }
451+ return ;
452+ }
446453
447- process . title = `trigger-dev-worker: ${ execution . task . id } ${ execution . run . id } ` ;
454+ process . title = `trigger-dev-worker: ${ execution . task . id } ${ execution . run . id } ` ;
448455
449- // Import the task module
450- const task = resourceCatalog . getTask ( execution . task . id ) ;
456+ // Now try and get the task again
457+ task = resourceCatalog . getTask ( execution . task . id ) ;
458+ }
451459
452460 if ( ! task ) {
453461 console . error ( `Could not find task ${ execution . task . id } ` ) ;
0 commit comments