fix(core): get the client env when calculating the task hash in the daemon (#17677)

This commit is contained in:
Jonathan Cammisuli 2023-06-20 09:33:26 -04:00 committed by GitHub
parent 71409eaed4
commit 337a871a08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 1 deletions

View File

@ -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[]

View File

@ -128,6 +128,7 @@ export class DaemonClient {
return this.sendToDaemonViaQueue({
type: 'HASH_TASKS',
runnerOptions,
env: process.env,
tasks,
taskGraph,
});

View File

@ -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();

View File

@ -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;
}

View File

@ -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<PartialHash> {
const value = hashArray([process.env[envVarName] ?? '']);
let env = getHashEnv();
const value = hashArray([env[envVarName] ?? '']);
return {
details: { [`env:${envVarName}`]: value },
value,