Skip to content

Commit

Permalink
fix(core): deprecate hashTask(task:Task) && hashTasks(task:Task[])
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Aug 1, 2023
1 parent 9e10a3e commit 8547043
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
25 changes: 19 additions & 6 deletions packages/nx/src/hasher/task-hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import { workspaceRoot } from '../utils/workspace-root';
import { join, relative } from 'path';
import { normalizePath } from '../utils/path';
import { findAllProjectNodeDependencies } from '../utils/project-graph-utils';
import { Simulate } from 'react-dom/test-utils';
import input = Simulate.input;

type ExpandedSelfInput =
| { fileset: string }
Expand Down Expand Up @@ -59,8 +61,19 @@ export interface Hash {
}

export interface TaskHasher {
hashTask(task: Task, taskGraph?: TaskGraph): Promise<Hash>;
hashTasks(tasks: Task[], taskGraph?: TaskGraph): Promise<Hash[]>;
/**
* @deprecated use hashTask(task:Task, taskGraph: TaskGraph)
* @param task
*/
hashTask(task: Task): Promise<Hash>;
hashTask(task: Task, taskGraph: TaskGraph): Promise<Hash>;

/**
* @deprecated use hashTasks(tasks:Task[], taskGraph: TaskGraph)
* @param tasks
*/
hashTasks(tasks: Task[]): Promise<Hash[]>;
hashTasks(tasks: Task[], taskGraph: TaskGraph): Promise<Hash[]>;
}

export type Hasher = TaskHasher;
Expand All @@ -71,11 +84,11 @@ export class DaemonBasedTaskHasher implements TaskHasher {
private readonly runnerOptions: any
) {}

async hashTasks(tasks: Task[], taskGraph: TaskGraph): Promise<Hash[]> {
async hashTasks(tasks: Task[], taskGraph?: TaskGraph): Promise<Hash[]> {
return this.daemonClient.hashTasks(this.runnerOptions, tasks, taskGraph);
}

async hashTask(task: Task, taskGraph: TaskGraph): Promise<Hash> {
async hashTask(task: Task, taskGraph?: TaskGraph): Promise<Hash> {
return (
await this.daemonClient.hashTasks(this.runnerOptions, [task], taskGraph)
)[0];
Expand Down Expand Up @@ -124,11 +137,11 @@ export class InProcessTaskHasher implements TaskHasher {
);
}

async hashTasks(tasks: Task[], taskGraph: TaskGraph): Promise<Hash[]> {
async hashTasks(tasks: Task[], taskGraph?: TaskGraph): Promise<Hash[]> {
return await Promise.all(tasks.map((t) => this.hashTask(t, taskGraph)));
}

async hashTask(task: Task, taskGraph: TaskGraph): Promise<Hash> {
async hashTask(task: Task, taskGraph?: TaskGraph): Promise<Hash> {
const res = await this.taskHasher.hashTask(task, taskGraph, [
task.target.project,
]);
Expand Down
31 changes: 29 additions & 2 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { Workspaces } from '../config/workspaces';
import {
DaemonBasedTaskHasher,
InProcessTaskHasher,
TaskHasher,
} from '../hasher/task-hasher';
import { hashTasksThatDoNotDependOnOutputsOfOtherTasks } from '../hasher/hash-task';
import { daemonClient } from '../daemon/client/client';
Expand Down Expand Up @@ -231,7 +232,7 @@ export async function invokeTasksRunner({

const { tasksRunner, runnerOptions } = getRunner(nxArgs, nxJson);

let hasher;
let hasher: TaskHasher;
if (daemonClient.enabled()) {
hasher = new DaemonBasedTaskHasher(daemonClient, runnerOptions);
} else {
Expand Down Expand Up @@ -272,7 +273,33 @@ export async function invokeTasksRunner({
nxJson,
nxArgs,
taskGraph,
hasher,
hasher: {
hashTask(task: Task, taskGraph_?: TaskGraph) {
if (!taskGraph_) {
output.warn({
title: `TaskGraph is now required as an argument to hashTasks`,
bodyLines: [
`The TaskGraph object can be retrieved from the context`,
],
});
taskGraph_ = taskGraph;
}
return hasher.hashTask(task, taskGraph_);
},
hashTasks(task: Task[], taskGraph_?: TaskGraph) {
if (!taskGraph_) {
output.warn({
title: `TaskGraph is now required as an argument to hashTasks`,
bodyLines: [
`The TaskGraph object can be retrieved from the context`,
],
});
taskGraph_ = taskGraph;
}

return hasher.hashTasks(task, taskGraph_);
},
},
daemon: daemonClient,
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CustomHasher } from '@nx/devkit';
* you can consume workspace details from the context.
*/
export const <%=propertyName%>Hasher: CustomHasher = async (task, context) => {
return context.hasher.hashTask(task);
return context.hasher.hashTask(task, context.taskGraph);
};

export default <%=propertyName%>Hasher;

0 comments on commit 8547043

Please sign in to comment.