Skip to content

Commit

Permalink
fix(core): get the client env when calculating the task hash in the d…
Browse files Browse the repository at this point in the history
…aemon (#17677)
  • Loading branch information
Cammisuli authored Jun 20, 2023
1 parent 71409ea commit 337a871
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
48 changes: 48 additions & 0 deletions e2e/nx-run/src/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class DaemonClient {
return this.sendToDaemonViaQueue({
type: 'HASH_TASKS',
runnerOptions,
env: process.env,
tasks,
taskGraph,
});
Expand Down
4 changes: 4 additions & 0 deletions packages/nx/src/daemon/server/handle-hash-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
19 changes: 19 additions & 0 deletions packages/nx/src/hasher/set-hash-env.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 3 additions & 1 deletion packages/nx/src/hasher/task-hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -695,7 +696,8 @@ class TaskHasherImpl {
}

private async hashEnv(envVarName: string): Promise<PartialHash> {
const value = hashArray([process.env[envVarName] ?? '']);
let env = getHashEnv();
const value = hashArray([env[envVarName] ?? '']);
return {
details: { [`env:${envVarName}`]: value },
value,
Expand Down

1 comment on commit 337a871

@vercel
Copy link

@vercel vercel bot commented on 337a871 Jun 20, 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-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.