From 337a871a089bd33e2dd7d8d53e486deb705a6ea7 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Tue, 20 Jun 2023 09:33:26 -0400 Subject: [PATCH] fix(core): get the client env when calculating the task hash in the daemon (#17677) --- e2e/nx-run/src/cache.test.ts | 48 +++++++++++++++++++ packages/nx/src/daemon/client/client.ts | 1 + .../nx/src/daemon/server/handle-hash-tasks.ts | 4 ++ packages/nx/src/hasher/set-hash-env.ts | 19 ++++++++ packages/nx/src/hasher/task-hasher.ts | 4 +- 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 packages/nx/src/hasher/set-hash-env.ts diff --git a/e2e/nx-run/src/cache.test.ts b/e2e/nx-run/src/cache.test.ts index 372e6a03c94cd..d571676106d29 100644 --- a/e2e/nx-run/src/cache.test.ts +++ b/e2e/nx-run/src/cache.test.ts @@ -289,6 +289,54 @@ describe('cache', () => { ); }, 120000); + it('should support ENV as an input', () => { + const lib = uniq('lib'); + runCLI(`generate @nx/js:lib ${lib}`); + updateJson(`nx.json`, (c) => { + c.tasksRunnerOptions.default.options.cacheableOperations.push('echo'); + c.targetDefaults = { + echo: { + inputs: [ + { + env: 'NAME', + }, + ], + }, + }; + + return c; + }); + + updateJson(`libs/${lib}/project.json`, (c) => { + c.targets = { + echo: { + command: 'echo $NAME', + }, + }; + return c; + }); + + const firstRun = runCLI(`echo ${lib}`, { + env: { NAME: 'e2e' }, + }); + expect(firstRun).not.toContain('read the output from the cache'); + + const secondRun = runCLI(`echo ${lib}`, { + env: { NAME: 'e2e' }, + }); + expect(secondRun).toContain('read the output from the cache'); + + const thirdRun = runCLI(`echo ${lib}`, { + env: { NAME: 'change' }, + }); + expect(thirdRun).not.toContain('read the output from the cache'); + + const fourthRun = runCLI(`echo ${lib}`, { + env: { NAME: 'change' }, + }); + expect(fourthRun).toContain('read the output from the cache'); + }, 120000); + function expectCached( actualOutput: string, expectedCachedProjects: string[] diff --git a/packages/nx/src/daemon/client/client.ts b/packages/nx/src/daemon/client/client.ts index 54221ef98d73b..8481b83cd4f3f 100644 --- a/packages/nx/src/daemon/client/client.ts +++ b/packages/nx/src/daemon/client/client.ts @@ -128,6 +128,7 @@ export class DaemonClient { return this.sendToDaemonViaQueue({ type: 'HASH_TASKS', runnerOptions, + env: process.env, tasks, taskGraph, }); diff --git a/packages/nx/src/daemon/server/handle-hash-tasks.ts b/packages/nx/src/daemon/server/handle-hash-tasks.ts index 94ba025a903c8..edecebbbbdf61 100644 --- a/packages/nx/src/daemon/server/handle-hash-tasks.ts +++ b/packages/nx/src/daemon/server/handle-hash-tasks.ts @@ -3,6 +3,7 @@ import { getCachedSerializedProjectGraphPromise } from './project-graph-incremen import { InProcessTaskHasher } from '../../hasher/task-hasher'; import { readNxJson } from '../../config/configuration'; import { fileHasher } from '../../hasher/file-hasher'; +import { setHashEnv } from '../../hasher/set-hash-env'; /** * We use this not to recreated hasher for every hash operation @@ -14,9 +15,12 @@ let storedHasher: InProcessTaskHasher | null = null; export async function handleHashTasks(payload: { runnerOptions: any; + env: any; tasks: Task[]; taskGraph: TaskGraph; }) { + setHashEnv(payload.env); + const { projectGraph, allWorkspaceFiles, projectFileMap } = await getCachedSerializedProjectGraphPromise(); const nxJson = readNxJson(); diff --git a/packages/nx/src/hasher/set-hash-env.ts b/packages/nx/src/hasher/set-hash-env.ts new file mode 100644 index 0000000000000..513df4b79aa5e --- /dev/null +++ b/packages/nx/src/hasher/set-hash-env.ts @@ -0,0 +1,19 @@ +// if using without the daemon, the hashEnv is always going to be the process.env. +// When using the daemon, we'll need to set the hashEnv with `setHashEnv` + +let hashEnv = process.env; + +/** + * Set the environment to be used by the hasher + * @param env + */ +export function setHashEnv(env: any) { + hashEnv = env; +} + +/** + * Get the environment used by the hasher + */ +export function getHashEnv() { + return hashEnv; +} diff --git a/packages/nx/src/hasher/task-hasher.ts b/packages/nx/src/hasher/task-hasher.ts index 95a657494e222..05246b9ac559c 100644 --- a/packages/nx/src/hasher/task-hasher.ts +++ b/packages/nx/src/hasher/task-hasher.ts @@ -17,6 +17,7 @@ import { findMatchingProjects } from '../utils/find-matching-projects'; import { FileHasher, hashArray } from './file-hasher'; import { getOutputsForTargetAndConfiguration } from '../tasks-runner/utils'; import { join } from 'path'; +import { getHashEnv } from './set-hash-env'; type ExpandedSelfInput = | { fileset: string } @@ -695,7 +696,8 @@ class TaskHasherImpl { } private async hashEnv(envVarName: string): Promise { - const value = hashArray([process.env[envVarName] ?? '']); + let env = getHashEnv(); + const value = hashArray([env[envVarName] ?? '']); return { details: { [`env:${envVarName}`]: value }, value,