Skip to content

Commit

Permalink
fix(core): hash the same environment as the tasks are run with (#19487)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Oct 7, 2023
1 parent 621a2d5 commit dde9106
Show file tree
Hide file tree
Showing 15 changed files with 691 additions and 488 deletions.
40 changes: 38 additions & 2 deletions docs/generated/devkit/TaskHasher.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

**`Deprecated`**

use hashTask(task:Task, taskGraph: TaskGraph)
use hashTask(task:Task, taskGraph: TaskGraph, env: NodeJS.ProcessEnv) instead. This will be removed in v18

#### Parameters

Expand All @@ -29,12 +29,30 @@ use hashTask(task:Task, taskGraph: TaskGraph)

**hashTask**(`task`, `taskGraph`): `Promise`<[`Hash`](../../devkit/documents/Hash)\>

**`Deprecated`**

use hashTask(task:Task, taskGraph: TaskGraph, env: NodeJS.ProcessEnv) instead. This will be removed in v18

#### Parameters

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

#### Returns

`Promise`<[`Hash`](../../devkit/documents/Hash)\>

**hashTask**(`task`, `taskGraph`, `env`): `Promise`<[`Hash`](../../devkit/documents/Hash)\>

#### Parameters

| Name | Type |
| :---------- | :---------------------------------------------- |
| `task` | [`Task`](../../devkit/documents/Task) |
| `taskGraph` | [`TaskGraph`](../../devkit/documents/TaskGraph) |
| `env` | `ProcessEnv` |

#### Returns

Expand All @@ -48,7 +66,7 @@ use hashTask(task:Task, taskGraph: TaskGraph)

**`Deprecated`**

use hashTasks(tasks:Task[], taskGraph: TaskGraph)
use hashTasks(tasks:Task[], taskGraph: TaskGraph, env: NodeJS.ProcessEnv) instead. This will be removed in v18

#### Parameters

Expand All @@ -62,12 +80,30 @@ use hashTasks(tasks:Task[], taskGraph: TaskGraph)

**hashTasks**(`tasks`, `taskGraph`): `Promise`<[`Hash`](../../devkit/documents/Hash)[]\>

**`Deprecated`**

use hashTasks(tasks:Task[], taskGraph: TaskGraph, env: NodeJS.ProcessEnv) instead. This will be removed in v18

#### Parameters

| Name | Type |
| :---------- | :---------------------------------------------- |
| `tasks` | [`Task`](../../devkit/documents/Task)[] |
| `taskGraph` | [`TaskGraph`](../../devkit/documents/TaskGraph) |

#### Returns

`Promise`<[`Hash`](../../devkit/documents/Hash)[]\>

**hashTasks**(`tasks`, `taskGraph`, `env`): `Promise`<[`Hash`](../../devkit/documents/Hash)[]\>

#### Parameters

| Name | Type |
| :---------- | :---------------------------------------------- |
| `tasks` | [`Task`](../../devkit/documents/Task)[] |
| `taskGraph` | [`TaskGraph`](../../devkit/documents/TaskGraph) |
| `env` | `ProcessEnv` |

#### Returns

Expand Down
7 changes: 6 additions & 1 deletion packages/linter/src/executors/eslint/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ export default async function run(
projectGraph: ProjectGraph;
taskGraph: TaskGraph;
projectsConfigurations: ProjectsConfigurations;
env: NodeJS.ProcessEnv;
}
): Promise<Hash> {
const res = await context.hasher.hashTask(task, context.taskGraph);
const res = await context.hasher.hashTask(
task,
context.taskGraph,
context.env
);
if (task.overrides['hasTypeAwareRules'] === true) {
return res;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/nx/src/command-line/affected/print-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { hashTask } from '../../hasher/hash-task';
import { getPackageManagerCommand } from '../../utils/package-manager';
import { printAffectedDeprecationMessage } from './command-object';
import { logger, NX_PREFIX } from '../../utils/logger';
import { getTaskSpecificEnv } from '../../tasks-runner/task-env';

/**
* @deprecated Use showProjectsHandler, generateGraph, or affected (without the print-affected mode) instead.
Expand Down Expand Up @@ -76,7 +77,16 @@ async function createTasks(
const tasks = Object.values(taskGraph.tasks);

await Promise.all(
tasks.map((t) => hashTask(hasher, projectGraph, taskGraph, t))
tasks.map((t) =>
hashTask(
hasher,
projectGraph,
taskGraph,
t,
// This loads dotenv files for the task
getTaskSpecificEnv(t)
)
)
);

return tasks.map((task) => ({
Expand Down
5 changes: 3 additions & 2 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ export class DaemonClient {
hashTasks(
runnerOptions: any,
tasks: Task[],
taskGraph: TaskGraph
taskGraph: TaskGraph,
env: NodeJS.ProcessEnv
): Promise<Hash[]> {
return this.sendToDaemonViaQueue({
type: 'HASH_TASKS',
runnerOptions,
env: process.env,
env,
tasks,
taskGraph,
});
Expand Down
5 changes: 1 addition & 4 deletions packages/nx/src/daemon/server/handle-hash-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Task, TaskGraph } from '../../config/task-graph';
import { getCachedSerializedProjectGraphPromise } from './project-graph-incremental-recomputation';
import { InProcessTaskHasher } from '../../hasher/task-hasher';
import { readNxJson } from '../../config/configuration';
import { setHashEnv } from '../../hasher/set-hash-env';

/**
* We use this not to recreated hasher for every hash operation
Expand All @@ -17,8 +16,6 @@ export async function handleHashTasks(payload: {
tasks: Task[];
taskGraph: TaskGraph;
}) {
setHashEnv(payload.env);

const { projectGraph, allWorkspaceFiles, fileMap } =
await getCachedSerializedProjectGraphPromise();
const nxJson = readNxJson();
Expand All @@ -34,7 +31,7 @@ export async function handleHashTasks(payload: {
);
}
const response = JSON.stringify(
await storedHasher.hashTasks(payload.tasks, payload.taskGraph)
await storedHasher.hashTasks(payload.tasks, payload.taskGraph, payload.env)
);
return {
response,
Expand Down
6 changes: 4 additions & 2 deletions packages/nx/src/hasher/hash-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export async function hashTask(
hasher: TaskHasher,
projectGraph: ProjectGraph,
taskGraph: TaskGraph,
task: Task
task: Task,
env: NodeJS.ProcessEnv
) {
const customHasher = await getCustomHasher(task, projectGraph);
const projectsConfigurations =
Expand All @@ -58,8 +59,9 @@ export async function hashTask(
workspaceConfig: projectsConfigurations, // to make the change non-breaking. Remove after v18
projectsConfigurations,
nxJsonConfiguration: readNxJson(),
env,
} as any)
: hasher.hashTask(task, taskGraph));
: hasher.hashTask(task, taskGraph, env));
task.hash = value;
task.hashDetails = details;
}
19 changes: 0 additions & 19 deletions packages/nx/src/hasher/set-hash-env.ts

This file was deleted.

Loading

1 comment on commit dde9106

@vercel
Copy link

@vercel vercel bot commented on dde9106 Oct 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.