Skip to content

Commit

Permalink
feat(core): add the outputs of a task onto the task
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Oct 11, 2023
1 parent 89ad0b1 commit 5b9c775
Show file tree
Hide file tree
Showing 14 changed files with 165 additions and 855 deletions.
9 changes: 9 additions & 0 deletions docs/generated/devkit/Task.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A representation of the invocation of an Executor
- [hash](../../devkit/documents/Task#hash): string
- [hashDetails](../../devkit/documents/Task#hashdetails): Object
- [id](../../devkit/documents/Task#id): string
- [outputs](../../devkit/documents/Task#outputs): string[]
- [overrides](../../devkit/documents/Task#overrides): any
- [projectRoot](../../devkit/documents/Task#projectroot): string
- [startTime](../../devkit/documents/Task#starttime): number
Expand Down Expand Up @@ -67,6 +68,14 @@ Unique ID

---

### outputs

**outputs**: `string`[]

The outputs the task may produce

---

### overrides

**overrides**: `any`
Expand Down
26 changes: 22 additions & 4 deletions docs/generated/devkit/getOutputsForTargetAndConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@

**getOutputsForTargetAndConfiguration**(`task`, `node`): `string`[]

**`Deprecated`**

Pass the target and overrides instead. This will be removed in v18.

#### Parameters

| Name | Type |
| :----- | :-------------------------------------------------------------------------- |
| `task` | [`Task`](../../devkit/documents/Task) |
| `node` | [`ProjectGraphProjectNode`](../../devkit/documents/ProjectGraphProjectNode) |

#### Returns

`string`[]

**getOutputsForTargetAndConfiguration**(`target`, `overrides`, `node`): `string`[]

Returns the list of outputs that will be cached.

#### Parameters

| Name | Type | Description |
| :----- | :-------------------------------------------------------------------------- | :-------------------------------------------------------- |
| `task` | `Pick`<[`Task`](../../devkit/documents/Task), `"overrides"` \| `"target"`\> | target + overrides |
| `node` | [`ProjectGraphProjectNode`](../../devkit/documents/ProjectGraphProjectNode) | ProjectGraphProjectNode object that the task runs against |
| Name | Type |
| :---------- | :----------------------------------------------------------------------------------------------------------------- |
| `target` | [`Task`](../../devkit/documents/Task) \| { `configuration?`: `string` ; `project`: `string` ; `target`: `string` } |
| `overrides` | `any` |
| `node` | [`ProjectGraphProjectNode`](../../devkit/documents/ProjectGraphProjectNode) |

#### Returns

Expand Down
8 changes: 3 additions & 5 deletions packages/angular/src/generators/move/lib/update-ng-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ export function updateNgPackage(tree: Tree, schema: MoveImplOptions): void {
);
const outputs = getOutputsForTargetAndConfiguration(
{
target: {
project: schema.newProjectName,
target: 'build',
},
overrides: {},
project: schema.newProjectName,
target: 'build',
},
{},
{
name: schema.newProjectName,
type: 'lib',
Expand Down
36 changes: 17 additions & 19 deletions packages/js/src/utils/buildable-libs-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,11 @@ export function calculateProjectDependencies(
: dep,
outputs: getOutputsForTargetAndConfiguration(
{
overrides: {},
target: {
project: projectName,
target: targetName,
configuration: configurationName,
},
project: projectName,
target: targetName,
configuration: configurationName,
},
{},
depNode
),
node: depNode,
Expand Down Expand Up @@ -260,7 +258,11 @@ export function calculateDependenciesFromTaskGraph(
return null;
}

let outputs = getOutputsForTargetAndConfiguration(depTask, depProjectNode);
let outputs = getOutputsForTargetAndConfiguration(
depTask.target,
depTask.overrides,
depProjectNode
);

if (outputs.length === 0) {
nonBuildableDependencies.push(depTask.target.project);
Expand Down Expand Up @@ -558,13 +560,11 @@ export function updateBuildableProjectPackageJsonDependencies(
) {
const outputs = getOutputsForTargetAndConfiguration(
{
overrides: {},
target: {
project: projectName,
target: targetName,
configuration: configurationName,
},
project: projectName,
target: targetName,
configuration: configurationName,
},
{},
node
);

Expand Down Expand Up @@ -598,13 +598,11 @@ export function updateBuildableProjectPackageJsonDependencies(
if (entry.node.type === 'lib') {
const outputs = getOutputsForTargetAndConfiguration(
{
overrides: {},
target: {
project: projectName,
target: targetName,
configuration: configurationName,
},
project: projectName,
target: targetName,
configuration: configurationName,
},
{},
entry.node
);

Expand Down
10 changes: 4 additions & 6 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,11 @@ function addMissingDependencies(
) {
const outputs = getOutputsForTargetAndConfiguration(
{
overrides: {},
target: {
project: projectName,
target: targetName,
configuration: configurationName,
},
project: projectName,
target: targetName,
configuration: configurationName,
},
{},
entry.node
);

Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/affected/print-affected.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCommandAsString, getOutputs } from '../../tasks-runner/utils';
import { getCommandAsString } from '../../tasks-runner/utils';
import * as yargs from 'yargs';
import type { NxArgs } from '../../utils/command-line-utils';
import {
Expand Down Expand Up @@ -95,7 +95,7 @@ async function createTasks(
target: task.target,
hash: task.hash,
command: getCommandAsString(execCommand, task),
outputs: getOutputs(projectGraph.nodes, task),
outputs: task.outputs,
}));
}

Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/config/task-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export interface Task {
* Overrides for the configured options of the target
*/
overrides: any;

/**
* The outputs the task may produce
*/
outputs: string[];
/**
* Root of the project the task belongs to
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/hasher/task-hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,8 @@ class TaskHasherImpl {
for (const d of taskGraph.dependencies[task.id]) {
const childTask = taskGraph.tasks[d];
const outputs = getOutputsForTargetAndConfiguration(
childTask,
childTask.target,
childTask.overrides,
this.projectGraph.nodes[childTask.target.project]
);
const { getFilesForOutputs } =
Expand Down
15 changes: 13 additions & 2 deletions packages/nx/src/tasks-runner/create-task-graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProjectGraph, ProjectGraphProjectNode } from '../config/project-graph';
import { getDependencyConfigs, interpolate } from './utils';
import { getDependencyConfigs, getOutputs, interpolate } from './utils';
import {
projectHasTarget,
projectHasTargetAndConfiguration,
Expand Down Expand Up @@ -329,11 +329,22 @@ export class ProcessTasks {
configuration: resolvedConfiguration,
};

const interpolatedOverrides = interpolateOverrides(
overrides,
project.name,
project.data
);

return {
id,
target: qualifiedTarget,
projectRoot: project.data.root,
overrides: interpolateOverrides(overrides, project.name, project.data),
overrides: interpolatedOverrides,
outputs: getOutputs(
this.projectGraph.nodes,
qualifiedTarget,
interpolatedOverrides
),
// TODO(v18): Remove cast here after typing is moved back onto TargetConfiguration
cache: (project.data.targets[target] as any).cache,
};
Expand Down
10 changes: 3 additions & 7 deletions packages/nx/src/tasks-runner/task-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { TaskStatus } from './tasks-runner';
import {
calculateReverseDeps,
getExecutorForTask,
getOutputs,
isCacheableTask,
removeTasksFromTaskGraph,
shouldStreamOutput,
Expand Down Expand Up @@ -147,7 +146,7 @@ export class TaskOrchestrator {
const cachedResult = await this.cache.get(task);
if (!cachedResult || cachedResult.code !== 0) return null;

const outputs = getOutputs(this.projectGraph.nodes, task);
const outputs = task.outputs;
const shouldCopyOutputsFromCache =
!!outputs.length &&
(await this.shouldCopyOutputsFromCache(outputs, task.hash));
Expand Down Expand Up @@ -422,7 +421,7 @@ export class TaskOrchestrator {
result.status === 'success'
? 0
: 1,
outputs: getOutputs(this.projectGraph.nodes, result.task),
outputs: result.task.outputs,
}))
.filter(({ task, code }) => this.shouldCacheTaskResult(task, code))
.filter(({ terminalOutput, outputs }) => terminalOutput || outputs)
Expand Down Expand Up @@ -551,10 +550,7 @@ export class TaskOrchestrator {

private async recordOutputsHash(task: Task) {
if (this.daemon?.enabled()) {
return this.daemon.recordOutputsHash(
getOutputs(this.projectGraph.nodes, task),
task.hash
);
return this.daemon.recordOutputsHash(task.outputs, task.hash);
}
}

Expand Down
Loading

0 comments on commit 5b9c775

Please sign in to comment.