Skip to content

Commit 7a142d3

Browse files
authored
fix: Warn instead of throwing when getting workflowBundle with workflowsPath and bundlerOptions (#833)
NOTE: We now prefer taking the bundle over workflowsPath which is the correct behavior and what users should expect. Also warning that workflow interceptors are ignored when using a bundle.
1 parent 73ebc35 commit 7a142d3

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

packages/worker/src/worker-options.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ export interface WorkerOptions {
114114
*
115115
* See https://docs.temporal.io/typescript/production-deploy#pre-build-code for more information.
116116
*
117-
* When using this option, any Workflow interceptors provided in {@link interceptors} are not used. Instead, provide
118-
* them via {@link BundleOptions.workflowInterceptorModules} when calling {@link bundleWorkflowCode}.
117+
* When using this option, {@link workflowsPath}, {@link bundlerOptions} and any Workflow interceptors modules
118+
* provided in * {@link interceptors} are not used. To use workflow interceptors, pass them via
119+
* {@link BundleOptions.workflowInterceptorModules} when calling {@link bundleWorkflowCode}.
119120
*/
120121
workflowBundle?: WorkflowBundleOption;
121122

packages/worker/src/worker.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
optionalTsToMs,
3434
SearchAttributes,
3535
tsToMs,
36-
ValueError,
3736
} from '@temporalio/internal-workflow-common';
3837
import { coresdk, temporal } from '@temporalio/proto';
3938
import { DeterminismViolationError, SinkCall, WorkflowInfo } from '@temporalio/workflow';
@@ -64,7 +63,7 @@ import pkg from './pkg';
6463
import { History, Runtime } from './runtime';
6564
import { closeableGroupBy, mapWithState, mergeMapWithState } from './rxutils';
6665
import { childSpan, getTracer, instrument } from './tracing';
67-
import { byteArrayToBuffer, convertToParentWorkflowType, toMB } from './utils';
66+
import { byteArrayToBuffer, convertToParentWorkflowType } from './utils';
6867
import {
6968
addDefaultWorkerOptions,
7069
CompiledWorkerOptions,
@@ -535,13 +534,29 @@ export class Worker {
535534
compiledOptions: CompiledWorkerOptions,
536535
logger: Logger
537536
): Promise<WorkflowBundleWithSourceMapAndFilename | undefined> {
538-
if (compiledOptions.workflowsPath) {
539-
if (compiledOptions.workflowBundle) {
540-
throw new ValueError(
541-
'You cannot set both WorkerOptions.workflowsPath and .workflowBundle: only one can be used'
537+
if (compiledOptions.workflowBundle) {
538+
if (compiledOptions.workflowsPath) {
539+
logger.warn('Ignoring WorkerOptions.workflowsPath because WorkerOptions.workflowBundle is set');
540+
}
541+
if (compiledOptions.bundlerOptions) {
542+
logger.warn('Ignoring WorkerOptions.bundlerOptions because WorkerOptions.workflowBundle is set');
543+
}
544+
if (compiledOptions.interceptors?.workflowModules) {
545+
logger.warn(
546+
'Ignoring WorkerOptions.interceptors.workflowModules because WorkerOptions.workflowBundle is set.\n' +
547+
'To use workflow interceptors with a workflowBundle, pass them in the call to bundleWorkflowCode.'
542548
);
543549
}
544550

551+
if (isCodeBundleOption(compiledOptions.workflowBundle)) {
552+
return parseWorkflowCode(compiledOptions.workflowBundle.code);
553+
} else if (isPathBundleOption(compiledOptions.workflowBundle)) {
554+
const code = await fs.readFile(compiledOptions.workflowBundle.codePath, 'utf8');
555+
return parseWorkflowCode(code, compiledOptions.workflowBundle.codePath);
556+
} else {
557+
throw new TypeError('Invalid WorkflowOptions.workflowBundle');
558+
}
559+
} else if (compiledOptions.workflowsPath) {
545560
const bundler = new WorkflowCodeBundler({
546561
logger,
547562
workflowsPath: compiledOptions.workflowsPath,
@@ -551,21 +566,7 @@ export class Worker {
551566
webpackConfigHook: compiledOptions.bundlerOptions?.webpackConfigHook,
552567
});
553568
const bundle = await bundler.createBundle();
554-
logger.info('Workflow bundle created', { size: `${toMB(bundle.code.length)}MB` });
555569
return parseWorkflowCode(bundle.code);
556-
} else if (compiledOptions.workflowBundle) {
557-
if (compiledOptions.bundlerOptions) {
558-
throw new ValueError('You cannot set both WorkerOptions.workflowBundle and .bundlerOptions');
559-
}
560-
561-
if (isCodeBundleOption(compiledOptions.workflowBundle)) {
562-
return parseWorkflowCode(compiledOptions.workflowBundle.code);
563-
} else if (isPathBundleOption(compiledOptions.workflowBundle)) {
564-
const code = await fs.readFile(compiledOptions.workflowBundle.codePath, 'utf8');
565-
return parseWorkflowCode(code, compiledOptions.workflowBundle.codePath);
566-
} else {
567-
throw new TypeError('Invalid WorkflowOptions.workflowBundle');
568-
}
569570
} else {
570571
return undefined;
571572
}

packages/worker/src/workflow/bundler.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as unionfs from 'unionfs';
77
import util from 'util';
88
import webpack from 'webpack';
99
import { DefaultLogger, Logger } from '../logger';
10+
import { toMB } from '../utils';
1011

1112
export const allowedBuiltinModules = ['assert'];
1213
export const disallowedBuiltinModules = builtinModules.filter((module) => !allowedBuiltinModules.includes(module));
@@ -104,11 +105,14 @@ export class WorkflowCodeBundler {
104105

105106
this.genEntrypoint(vol, entrypointPath);
106107
const bundleFilePath = await this.bundle(ufs, memoryFs, entrypointPath, distDir);
108+
const code = memoryFs.readFileSync(bundleFilePath, 'utf8') as string;
109+
110+
this.logger.info('Workflow bundle created', { size: `${toMB(code.length)}MB` });
107111

108112
// Cast because the type definitions are inaccurate
109113
return {
110-
sourceMap: 'deprecated: this is no longer in use',
111-
code: memoryFs.readFileSync(bundleFilePath, 'utf8') as string,
114+
sourceMap: 'deprecated: this is no longer in use\n',
115+
code,
112116
};
113117
}
114118

0 commit comments

Comments
 (0)