Deprecating Custom Tasks Runner

The Nx core has been migrated to Rust. However, the Custom Tasks Runner API is not compatible with this rewrite because it allows modifications to the lifecycle of the Nx command execution, which could break important invariants that Nx depends on.

The custom task runners API was created many years ago and has not been supported for several years. It was developed before we introduced a formal method for extending Nx through plugins. The new API leverages the plugins API for better performance and functionality.

The preTasksExecution and postTasksExecution hooks

Custom Tasks Runner Version

Letโ€™s imagine that you have implemented a custom task runner as follows:

1function serializeTasksResults(taskResults: { [taskId: string]: TaskResult }) { 2 // ... 3} 4 5function validateEnv() { 6 // ... 7} 8 9export default async function customTasksRunner(tasks, options, context) { 10 if (process.env.QA_ENV) { 11 process.env.NX_SKIP_NX_CACHE = 'true'; 12 } 13 if (!validateEnv()) { 14 throw new Error('Env is not set up correctly'); 15 } 16 const allTaskResults = {}; 17 const lifeCycle = { 18 endTasks(taskResults) { 19 taskResults.forEach((tr) => { 20 allTaskResults[tr.task.id] = tr; 21 }); 22 }, 23 }; 24 const ret = await defaultTasksRunner( 25 tasks, 26 { ...options, lifeCycle }, 27 context 28 ); 29 if (options.reportAnalytics) { 30 await fetch(process.env.DATACAT_API, { 31 method: 'POST', 32 headers: { 33 'Content-Type': 'application/json', 34 }, 35 body: serializeTasksResults(allTaskResults), 36 }); 37 } 38 return ret; 39} 40

And let's imagine you configured it in nx.json as follows:

1{ 2 "tasksRunnerOptions": { 3 "default": { 4 "runner": "npm-package-with-custom-tasks-runner", 5 "options": { 6 "reportAnalytics": true 7 } 8 } 9 } 10} 11

New API

The new API includes preTasksExecution and postTasksExecution hooks that plugins can register. These hooks do not affect task execution and cannot violate any invariants.

The custom task runner above can be implemented as follows:

1function serializeTasksResults(taskResults: { [taskId: string]: TaskResult }) { 2 // task results contain timings and status information useful for gathering analytics 3} 4 5function validateEnv() { 6 // ... 7} 8 9// context contains workspaceRoot and nx.json configuration 10export async function preTasksExecution(options: any, context) { 11 if (process.env.QA_ENV) { 12 process.env.NX_SKIP_NX_CACHE = 'true'; 13 } 14 if (!validateEnv()) { 15 throw new Error('Env is not set up correctly'); 16 } 17} 18 19// context contains workspaceRoot, nx.json configuration, and task results 20export async function postTasksExecution(options: any, context) { 21 if (options.reportAnalytics) { 22 await fetch(process.env.DATACAT_API, { 23 method: 'POST', 24 headers: { 25 'Content-Type': 'application/json', 26 }, 27 body: serializeTasksResults(context.taskResults), 28 }); 29 } 30} 31

Because it's a regular plugin, it can be configured as follows in nx.json:

1{ 2 "plugins": [ 3 { 4 "plugin": "npm-package-with-the-plugin", 5 "options": { 6 "reportAnalytics": true 7 } 8 } 9 ] 10} 11

As with all plugin hooks, the preTasksExecution and postTasksExecution hooks must be exported so that they load properly when the "npm-package-with-the-plugin" is initialized.

Multiple Tasks Runners

You can implement multiple tasks runners using the new hooks.

Imagine you have the following nx.json:

1{ 2 "tasksRunnerOptions": { 3 "a": { 4 "runner": "package-a" 5 }, 6 "b": { 7 "runner": "package-b" 8 } 9 } 10} 11

You can replace it with two plugins:

1{ 2 "plugins": [ 3 { 4 "plugin": "package-a" 5 }, 6 { 7 "plugin": "package-b" 8 } 9 ] 10} 11

Simply add a condition to your hooks as follows:

1export async function preTasksExecution() { 2 if (process.env.RUNNER != 'a') return; 3} 4 5export async function postTasksExecution(options, tasksResults) { 6 if (process.env.RUNNER != 'a') return; 7} 8

You can then choose which hooks to use by setting the RUNNER env variable.

Passing Options

You can no longer augment options passed in to the default tasks runner, so instead you need to set env variables in the preTasksExecution hook.

Composing Plugins

Implementing hooks in plugins offers several advantages. It allows multiple plugins to be loaded simultaneously, enabling a clean separation of concerns.

Keeping State Across Command Invocations

By default, every plugin initiates a long-running process, allowing you to maintain state across command invocations, which can be very useful for advanced analytics.

Self-Hosted Remote Cache

The preTasksExecution and postTasksExecution hooks cannot modify the execution of tasks, making it impossible to use them for implementing a remote cache. Instead, you should install and activate one of the Nx Powerpack packages. Nx Powerpack does not make requests to external APIs, nor does it collect or send any data. Additionally, it offers more security and prevents cache poisoning, unlike DIY solutions.

We recognize that many organizations have been using DIY remote cache solutions. Therefore, we have made the migration to Nx Powerpack as streamlined as possible.

Get a License and Activate PowerpackUnlock all the features of the Nx CLI

Example Configuration Change

Enabling a Nx Powerpack plugin will configure it in nx.json. The specific modification depends on your repositoryโ€™s configuration. The following is one example, where a custom tasks runner configuration in nx.json will be removed:

1{ 2 "tasksRunnerOptions": { 3 "default": { 4 "runner": "@nx-aws-plugin/nx-aws-cache", 5 "options": { 6 "awsAccessKeyId": "key", 7 "awsSecretAccessKey": "secret", 8 "awsForcePathStyle": true, 9 "awsEndpoint": "http://custom", 10 "awsBucket": "my-bucket", 11 "awsRegion": "eu-central-1", 12 "encryptionFileKey": "key" 13 } 14 } 15 } 16} 17

And replaced with:

1{ 2 "s3": { 3 "accessKeyId": "key", 4 "secretAccessKey": "secret", 5 "forcePathStyle": true, 6 "endpoint": "http://custom", 7 "bucket": "my-bucket", 8 "region": "eu-central-1", 9 "encryptionFileKey": "key", 10 "localMode": "read-write" 11 } 12} 13

The API documentation for each plugin describes the available options.

Unhandled Custom Tasks Runner Use Cases?

If you have a use case that the new API doesn't handle, please open an issue.