Skip to content

Commit 2319dc3

Browse files
committed
Revert "cleanup(core): copy from cache only when needed"
This reverts commit 4dac0a2.
1 parent ab13bf4 commit 2319dc3

File tree

4 files changed

+19
-118
lines changed

4 files changed

+19
-118
lines changed

e2e/workspace/src/workspace.test.ts

+11-35
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
updateFile,
1616
workspaceConfigName,
1717
} from '@nrwl/e2e/utils';
18-
import { TaskCacheStatus } from '@nrwl/workspace/src/utilities/output';
1918

2019
describe('run-one', () => {
2120
let proj: string;
@@ -638,7 +637,7 @@ describe('cache', () => {
638637
});
639638
const outputWithBuildApp2Cached = runCLI(`affected:build ${files}`);
640639
expect(outputWithBuildApp2Cached).toContain('read the output from cache');
641-
expectMatchedOutput(outputWithBuildApp2Cached, [myapp2]);
640+
expectCached(outputWithBuildApp2Cached, [myapp2]);
642641

643642
// touch package.json
644643
// --------------------------------------------
@@ -652,17 +651,13 @@ describe('cache', () => {
652651

653652
// build individual project with caching
654653
const individualBuildWithCache = runCLI(`build ${myapp1}`);
655-
expect(individualBuildWithCache).toContain(
656-
TaskCacheStatus.MatchedExistingOutput
657-
);
654+
expect(individualBuildWithCache).toContain('from cache');
658655

659656
// skip caching when building individual projects
660657
const individualBuildWithSkippedCache = runCLI(
661658
`build ${myapp1} --skip-nx-cache`
662659
);
663-
expect(individualBuildWithSkippedCache).not.toContain(
664-
TaskCacheStatus.MatchedExistingOutput
665-
);
660+
expect(individualBuildWithSkippedCache).not.toContain('from cache');
666661

667662
// run lint with caching
668663
// --------------------------------------------
@@ -673,7 +668,7 @@ describe('cache', () => {
673668
expect(outputWithBothLintTasksCached).toContain(
674669
'read the output from cache'
675670
);
676-
expectMatchedOutput(outputWithBothLintTasksCached, [
671+
expectCached(outputWithBothLintTasksCached, [
677672
myapp1,
678673
myapp2,
679674
`${myapp1}-e2e`,
@@ -752,38 +747,19 @@ describe('cache', () => {
752747
actualOutput: string,
753748
expectedCachedProjects: string[]
754749
) {
755-
expectProjectMatchTaskCacheStatus(actualOutput, expectedCachedProjects);
756-
}
757-
758-
function expectMatchedOutput(
759-
actualOutput: string,
760-
expectedMatchedOutputProjects: string[]
761-
) {
762-
expectProjectMatchTaskCacheStatus(
763-
actualOutput,
764-
expectedMatchedOutputProjects,
765-
TaskCacheStatus.MatchedExistingOutput
766-
);
767-
}
768-
769-
function expectProjectMatchTaskCacheStatus(
770-
actualOutput: string,
771-
expectedProjects: string[],
772-
cacheStatus: TaskCacheStatus = TaskCacheStatus.RetrievedFromCache
773-
) {
774-
const matchingProjects = [];
750+
const cachedProjects = [];
775751
const lines = actualOutput.split('\n');
776-
lines.forEach((s) => {
752+
lines.forEach((s, i) => {
777753
if (s.startsWith(`> nx run`)) {
778754
const projectName = s.split(`> nx run `)[1].split(':')[0].trim();
779-
if (s.indexOf(cacheStatus) > -1) {
780-
matchingProjects.push(projectName);
755+
if (s.indexOf('from cache') > -1) {
756+
cachedProjects.push(projectName);
781757
}
782758
}
783759
});
784760

785-
matchingProjects.sort((a, b) => a.localeCompare(b));
786-
expectedProjects.sort((a, b) => a.localeCompare(b));
787-
expect(matchingProjects).toEqual(expectedProjects);
761+
cachedProjects.sort((a, b) => a.localeCompare(b));
762+
expectedCachedProjects.sort((a, b) => a.localeCompare(b));
763+
expect(cachedProjects).toEqual(expectedCachedProjects);
788764
}
789765
});

packages/workspace/src/tasks-runner/cache.ts

-46
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import * as fsExtra from 'fs-extra';
1212
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
1313
import { spawn } from 'child_process';
1414
import { cacheDirectory } from '../utilities/cache-directory';
15-
import { readJsonFile, writeJsonFile } from '../utilities/fileutils';
1615

1716
export type CachedResult = { terminalOutput: string; outputsPath: string };
1817
export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult };
@@ -39,7 +38,6 @@ export class Cache {
3938
root = appRootPath;
4039
cachePath = this.createCacheDir();
4140
terminalOutputsDir = this.createTerminalOutputsDir();
42-
nxOutputsPath = this.ensureNxOutputsFile();
4341
cacheConfig = new CacheConfig(this.options);
4442

4543
constructor(private readonly options: DefaultTasksRunnerOptions) {}
@@ -150,42 +148,6 @@ export class Cache {
150148
}
151149
}
152150

153-
removeOutputHashesFromNxOutputs(outputs: string[]): void {
154-
if (outputs.length === 0) {
155-
return;
156-
}
157-
158-
const nxOutputs = readJsonFile(this.nxOutputsPath);
159-
outputs.forEach((output) => {
160-
delete nxOutputs[output];
161-
});
162-
writeJsonFile(this.nxOutputsPath, nxOutputs);
163-
}
164-
165-
writeOutputHashesToNxOutputs(outputs: string[], hash: string): void {
166-
if (outputs.length === 0) {
167-
return;
168-
}
169-
170-
const nxOutputs = readJsonFile(this.nxOutputsPath);
171-
outputs.forEach((output) => {
172-
nxOutputs[output] = hash;
173-
});
174-
writeJsonFile(this.nxOutputsPath, nxOutputs);
175-
}
176-
177-
outputsMatchTask(task: Task, outputs: string[]): boolean {
178-
if (outputs.length === 0) {
179-
return true;
180-
}
181-
182-
const nxOutputs = readJsonFile(this.nxOutputsPath);
183-
return outputs.every(
184-
(output) =>
185-
existsSync(join(this.root, output)) && task.hash === nxOutputs[output]
186-
);
187-
}
188-
189151
private getFromLocalDir(task: Task) {
190152
const tdCommit = join(this.cachePath, `${task.hash}.commit`);
191153
const td = join(this.cachePath, task.hash);
@@ -213,12 +175,4 @@ export class Cache {
213175
fsExtra.ensureDirSync(path);
214176
return path;
215177
}
216-
217-
private ensureNxOutputsFile() {
218-
const path = join(this.cachePath, 'nx-outputs.json');
219-
if (!existsSync(path)) {
220-
writeJsonFile(path, {});
221-
}
222-
return path;
223-
}
224178
}

packages/workspace/src/tasks-runner/task-orchestrator.ts

+5-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as dotenv from 'dotenv';
44
import * as fs from 'fs';
55
import { ProjectGraph } from '../core/project-graph';
66
import { appRootPath } from '../utilities/app-root';
7-
import { output, TaskCacheStatus } from '../utilities/output';
7+
import { output } from '../utilities/output';
88
import { Cache, TaskWithCachedResult } from './cache';
99
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
1010
import { AffectedEventType, Task } from './tasks-runner';
@@ -115,28 +115,18 @@ export class TaskOrchestrator {
115115
tasks.forEach((t) => {
116116
this.options.lifeCycle.startTask(t.task);
117117

118-
const outputs = getOutputs(this.projectGraph.nodes, t.task);
119-
const outputsMatchCache = this.cache.outputsMatchTask(t.task, outputs);
120-
if (!outputsMatchCache) {
121-
this.cache.removeOutputHashesFromNxOutputs(outputs);
122-
this.cache.copyFilesFromCache(t.cachedResult, outputs);
123-
this.cache.writeOutputHashesToNxOutputs(outputs, t.task.hash);
124-
}
125-
126118
if (
127119
!this.initiatingProject ||
128120
this.initiatingProject === t.task.target.project
129121
) {
130122
const args = this.getCommandArgs(t.task);
131-
output.logCommand(
132-
`nx ${args.join(' ')}`,
133-
outputsMatchCache
134-
? TaskCacheStatus.MatchedExistingOutput
135-
: TaskCacheStatus.RetrievedFromCache
136-
);
123+
output.logCommand(`nx ${args.join(' ')}`, true);
137124
process.stdout.write(t.cachedResult.terminalOutput);
138125
}
139126

127+
const outputs = getOutputs(this.projectGraph.nodes, t.task);
128+
this.cache.copyFilesFromCache(t.cachedResult, outputs);
129+
140130
this.options.lifeCycle.endTask(t.task, 0);
141131
});
142132

@@ -185,7 +175,6 @@ export class TaskOrchestrator {
185175
if (forwardOutput) {
186176
output.logCommand(commandLine);
187177
}
188-
this.cache.removeOutputHashesFromNxOutputs(taskOutputs);
189178
const p = fork(this.getCommand(), args, {
190179
stdio: ['inherit', 'pipe', 'pipe', 'ipc'],
191180
env,
@@ -221,23 +210,17 @@ export class TaskOrchestrator {
221210
this.cache
222211
.put(task, outputPath, taskOutputs)
223212
.then(() => {
224-
this.cache.writeOutputHashesToNxOutputs(
225-
taskOutputs,
226-
task.hash
227-
);
228213
this.options.lifeCycle.endTask(task, code);
229214
res(code);
230215
})
231216
.catch((e) => {
232217
rej(e);
233218
});
234219
} else {
235-
this.cache.writeOutputHashesToNxOutputs(taskOutputs, task.hash);
236220
this.options.lifeCycle.endTask(task, code);
237221
res(code);
238222
}
239223
} else {
240-
this.cache.writeOutputHashesToNxOutputs(taskOutputs, task.hash);
241224
this.options.lifeCycle.endTask(task, code);
242225
res(code);
243226
}
@@ -268,7 +251,6 @@ export class TaskOrchestrator {
268251
if (forwardOutput) {
269252
output.logCommand(commandLine);
270253
}
271-
this.cache.removeOutputHashesFromNxOutputs(taskOutputs);
272254
const p = fork(this.getCommand(), args, {
273255
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
274256
env,
@@ -293,15 +275,13 @@ export class TaskOrchestrator {
293275
this.cache
294276
.put(task, outputPath, taskOutputs)
295277
.then(() => {
296-
this.cache.writeOutputHashesToNxOutputs(taskOutputs, task.hash);
297278
this.options.lifeCycle.endTask(task, code);
298279
res(code);
299280
})
300281
.catch((e) => {
301282
rej(e);
302283
});
303284
} else {
304-
this.cache.writeOutputHashesToNxOutputs(taskOutputs, task.hash);
305285
this.options.lifeCycle.endTask(task, code);
306286
res(code);
307287
}

packages/workspace/src/utilities/output.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ export interface CLISuccessMessageConfig {
2222
bodyLines?: string[];
2323
}
2424

25-
export enum TaskCacheStatus {
26-
NoCache = '[no cache]',
27-
MatchedExistingOutput = '[existing outputs match the cache, left as is]',
28-
RetrievedFromCache = '[retrieved from cache]',
29-
}
30-
3125
/**
3226
* Automatically disable styling applied by chalk if CI=true
3327
*/
@@ -183,16 +177,13 @@ class CLIOutput {
183177
this.addNewline();
184178
}
185179

186-
logCommand(
187-
message: string,
188-
cacheStatus: TaskCacheStatus = TaskCacheStatus.NoCache
189-
) {
180+
logCommand(message: string, isCached: boolean = false) {
190181
this.addNewline();
191182

192183
this.writeToStdOut(chalk.bold(`> ${message} `));
193184

194-
if (cacheStatus !== TaskCacheStatus.NoCache) {
195-
this.writeToStdOut(chalk.bold.grey(cacheStatus));
185+
if (isCached) {
186+
this.writeToStdOut(chalk.bold.grey(`[retrieved from cache]`));
196187
}
197188

198189
this.addNewline();

0 commit comments

Comments
 (0)