Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): adds ability for outputs to be files and not just folders #3613

Merged
merged 1 commit into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions e2e/workspace/src/workspace.test.ts
Expand Up @@ -12,6 +12,7 @@ import {
uniq,
updateFile,
workspaceConfigName,
tmpProjPath,
} from '@nrwl/e2e/utils';

let originalCIValue: any;
Expand Down Expand Up @@ -689,6 +690,47 @@ forEachCli((cliName) => {
);
}, 120000);

it('should only cache specific files if build outputs is configured with specific files', async () => {
ensureProject();

const mylib1 = uniq('mylib1');
runCLI(`generate @nrwl/react:lib ${mylib1} --buildable`);

// Update outputs in workspace.json to just be a particular file
const workspaceJson = readJson(workspaceConfigName());

workspaceJson.projects[mylib1].architect['build-base'] = {
...workspaceJson.projects[mylib1].architect.build,
};
workspaceJson.projects[mylib1].architect.build = {
builder: '@nrwl/workspace:run-commands',
outputs: [`dist/libs/${mylib1}/${mylib1}.esm.js`],
options: {
commands: [
{
command: `npm run nx run ${mylib1}:build-base`,
},
],
parallel: false,
},
};
updateFile(workspaceConfigName(), JSON.stringify(workspaceJson));

// run build with caching
// --------------------------------------------
const outputThatPutsDataIntoCache = runCLI(`run ${mylib1}:build`);
// now the data is in cache
expect(outputThatPutsDataIntoCache).not.toContain('Cached Output:');

rmDist();

const outputWithBuildTasksCached = runCLI(`run ${mylib1}:build`);
expect(outputWithBuildTasksCached).toContain('Cached Output:');
expectCached(outputWithBuildTasksCached, [mylib1]);
// Ensure that only the specific file in outputs was copied to cache
expect(listFiles(`dist/libs/${mylib1}`)).toEqual([`${mylib1}.esm.js`]);
}, 120000);

function expectCached(
actualOutput: string,
expectedCachedProjects: string[]
Expand Down
45 changes: 29 additions & 16 deletions packages/workspace/src/tasks-runner/cache.ts
@@ -1,7 +1,13 @@
import { appRootPath } from '../utils/app-root';
import { Task } from './tasks-runner';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import {
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
lstatSync,
} from 'fs';
import { join, resolve } from 'path';
import * as fsExtra from 'fs-extra';
import { DefaultTasksRunnerOptions } from './default-tasks-runner';
import { spawn } from 'child_process';
Expand Down Expand Up @@ -80,7 +86,7 @@ export class Cache {
}
}

async put(task: Task, terminalOutputPath: string, folders: string[]) {
async put(task: Task, terminalOutputPath: string, outputs: string[]) {
const terminalOutput = readFileSync(terminalOutputPath).toString();
const td = join(this.cachePath, task.hash);
const tdCommit = join(this.cachePath, `${task.hash}.commit`);
Expand All @@ -97,12 +103,16 @@ export class Cache {
writeFileSync(join(td, 'terminalOutput'), terminalOutput);

mkdirSync(join(td, 'outputs'));
folders.forEach((f) => {
const srcDir = join(this.root, f);
if (existsSync(srcDir)) {
const cachedDir = join(td, 'outputs', f);
fsExtra.ensureDirSync(cachedDir);
fsExtra.copySync(srcDir, cachedDir);
outputs.forEach((f) => {
const src = join(this.root, f);
if (existsSync(src)) {
const cached = join(td, 'outputs', f);
// Ensure parent directory is created if src is a file
const isFile = lstatSync(src).isFile();
const directory = isFile ? resolve(cached, '..') : cached;
fsExtra.ensureDirSync(directory);

fsExtra.copySync(src, cached);
}
});
// we need this file to account for partial writes to the cache folder.
Expand All @@ -118,14 +128,17 @@ export class Cache {

copyFilesFromCache(cachedResult: CachedResult, outputs: string[]) {
outputs.forEach((f) => {
const cachedDir = join(cachedResult.outputsPath, f);
if (existsSync(cachedDir)) {
const srcDir = join(this.root, f);
if (existsSync(srcDir)) {
fsExtra.removeSync(srcDir);
const cached = join(cachedResult.outputsPath, f);
if (existsSync(cached)) {
const isFile = lstatSync(cached).isFile();
const src = join(this.root, f);
if (existsSync(src)) {
fsExtra.removeSync(src);
}
fsExtra.ensureDirSync(srcDir);
fsExtra.copySync(cachedDir, srcDir);
// Ensure parent directory is created if src is a file
const directory = isFile ? resolve(src, '..') : src;
fsExtra.ensureDirSync(directory);
fsExtra.copySync(cached, src);
}
});
}
Expand Down