diff --git a/packages/cli/src/WorkflowHelpers.ts b/packages/cli/src/WorkflowHelpers.ts index 87e5fe3366..5107e261c8 100644 --- a/packages/cli/src/WorkflowHelpers.ts +++ b/packages/cli/src/WorkflowHelpers.ts @@ -261,15 +261,23 @@ export function getCredentialsDataWithParents(type: string): ICredentialsTypeDat * @param {IWorkflowCredentials} credentials The credentials which have to be able to be resolved * @returns {ICredentialsTypeData} */ -export function getCredentialsData(credentials: IWorkflowCredentials): ICredentialsTypeData { +export function getCredentialsDataByNodes(nodes: INode[]): ICredentialsTypeData { + const credentialTypeData: ICredentialsTypeData = {}; - for (const credentialType of Object.keys(credentials)) { - if (credentialTypeData[credentialType] !== undefined) { - continue; - } + for (const node of nodes) { + const credentialsUsedByThisNode = node.credentials; + if (credentialsUsedByThisNode) { + // const credentialTypesUsedByThisNode = Object.keys(credentialsUsedByThisNode!); + for (const credentialType of Object.keys(credentialsUsedByThisNode!)) { + if (credentialTypeData[credentialType] !== undefined) { + continue; + } - Object.assign(credentialTypeData, getCredentialsDataWithParents(credentialType)); + Object.assign(credentialTypeData, getCredentialsDataWithParents(credentialType)); + } + } + } return credentialTypeData; diff --git a/packages/cli/src/WorkflowRunner.ts b/packages/cli/src/WorkflowRunner.ts index 9a8c66430f..869b5c199d 100644 --- a/packages/cli/src/WorkflowRunner.ts +++ b/packages/cli/src/WorkflowRunner.ts @@ -425,14 +425,41 @@ export class WorkflowRunner { // Register the active execution const executionId = await this.activeExecutions.add(data, subprocess, restartExecutionId); - // Supply all nodeTypes and credentialTypes - const nodeTypeData = WorkflowHelpers.getAllNodeTypeData() as ITransferNodeTypes; - const credentialTypes = CredentialTypes(); + // Check if workflow contains a "executeWorkflow" Node as in this + // case we can not know which nodeTypes and credentialTypes will + // be needed and so have to load all of them in the workflowRunnerProcess + let loadAllNodeTypes = false; + for (const node of data.workflowData.nodes) { + if (node.type === 'n8n-nodes-base.executeWorkflow') { + loadAllNodeTypes = true; + break; + } + } + let nodeTypeData: ITransferNodeTypes; + let credentialTypeData: ICredentialsTypeData; + let credentialsOverwrites = this.credentialsOverwrites; + if (loadAllNodeTypes === true) { + // Supply all nodeTypes and credentialTypes + nodeTypeData = WorkflowHelpers.getAllNodeTypeData(); + const credentialTypes = CredentialTypes(); + credentialTypeData = credentialTypes.credentialTypes; + } else { + // Supply only nodeTypes, credentialTypes and overwrites that the workflow needs + nodeTypeData = WorkflowHelpers.getNodeTypeData(data.workflowData.nodes); + credentialTypeData = WorkflowHelpers.getCredentialsDataByNodes(data.workflowData.nodes); + + credentialsOverwrites = {}; + for (const credentialName of Object.keys(credentialTypeData)) { + if (this.credentialsOverwrites[credentialName] !== undefined) { + credentialsOverwrites[credentialName] = this.credentialsOverwrites[credentialName]; + } + } + } (data as unknown as IWorkflowExecutionDataProcessWithExecution).executionId = executionId; (data as unknown as IWorkflowExecutionDataProcessWithExecution).nodeTypeData = nodeTypeData; (data as unknown as IWorkflowExecutionDataProcessWithExecution).credentialsOverwrite = this.credentialsOverwrites; - (data as unknown as IWorkflowExecutionDataProcessWithExecution).credentialsTypeData = credentialTypes.credentialTypes; + (data as unknown as IWorkflowExecutionDataProcessWithExecution).credentialsTypeData = credentialTypeData; const workflowHooks = WorkflowExecuteAdditionalData.getWorkflowHooksMain(data, executionId);