From b32062416a244d5b1109a6f1c40fa1136527834d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Jona=C5=A1?= Date: Thu, 19 Aug 2021 01:56:01 +0200 Subject: [PATCH] fix(core): skip caching output properties missing in options and arguments (#6743) * fix(core): add outputFile to cached outputs * fix(core): ignore output properties missing in options and console arguments --- e2e/linter/src/linter.test.ts | 25 +++++++++++++++++++ e2e/utils/index.ts | 5 ++++ e2e/workspace/src/run-commands.test.ts | 17 +++++++++++++ .../linter/src/executors/lint/lint.impl.ts | 1 - .../src/tasks-runner/task-orchestrator.ts | 6 ++--- packages/workspace/src/tasks-runner/utils.ts | 8 +++--- 6 files changed, 53 insertions(+), 9 deletions(-) diff --git a/e2e/linter/src/linter.test.ts b/e2e/linter/src/linter.test.ts index 19c9b59fa02d6..500112e9541a9 100644 --- a/e2e/linter/src/linter.test.ts +++ b/e2e/linter/src/linter.test.ts @@ -4,6 +4,7 @@ import { newProject, readFile, readJson, + removeFile, runCLI, uniq, updateFile, @@ -188,4 +189,28 @@ describe('Linter', () => { 'Unexpected console statement.' ); }, 1000000); + + it('linting should cache the output file if defined in outputs', () => { + newProject(); + const myapp = uniq('myapp'); + const outputFile = 'a/b/c/lint-output.json'; + + runCLI(`generate @nrwl/react:app ${myapp}`); + const workspaceJson = readJson(`workspace.json`); + workspaceJson.projects[myapp].targets.lint.outputs = [ + '{options.outputFile}', + ]; + updateFile('workspace.json', JSON.stringify(workspaceJson, null, 2)); + + expect(() => checkFilesExist(outputFile)).toThrow(); + runCLI(`lint ${myapp} --output-file="${outputFile}" --format=json`, { + silenceError: true, + }); + expect(() => checkFilesExist(outputFile)).not.toThrow(); + removeFile(outputFile); + runCLI(`lint ${myapp} --output-file="${outputFile}" --format=json`, { + silenceError: true, + }); + expect(() => checkFilesExist(outputFile)).not.toThrow(); + }, 1000000); }); diff --git a/e2e/utils/index.ts b/e2e/utils/index.ts index 85e4538ef92fc..db69bc3489eca 100644 --- a/e2e/utils/index.ts +++ b/e2e/utils/index.ts @@ -554,6 +554,11 @@ export function readFile(f: string) { return readFileSync(ff, 'utf-8'); } +export function removeFile(f: string) { + const ff = f.startsWith('/') ? f : tmpProjPath(f); + removeSync(ff); +} + export function rmDist() { removeSync(`${tmpProjPath()}/dist`); } diff --git a/e2e/workspace/src/run-commands.test.ts b/e2e/workspace/src/run-commands.test.ts index a126c1ec28c87..0a52916f58055 100644 --- a/e2e/workspace/src/run-commands.test.ts +++ b/e2e/workspace/src/run-commands.test.ts @@ -131,4 +131,21 @@ describe('Run Commands', () => { ); } }); + + it('run command should not break if output property is missing in options and arguments', () => { + const myapp = uniq('myapp'); + + runCLI(`generate @nrwl/web:app ${myapp}`); + const workspaceJson = readJson(`workspace.json`); + workspaceJson.projects[myapp].targets.lint.outputs = [ + '{options.outputFile}', + ]; + updateFile('workspace.json', JSON.stringify(workspaceJson, null, 2)); + + expect(() => + runCLI(`run ${myapp}:lint --format=json`, { + silenceError: true, + }) + ).not.toThrow(); + }, 1000000); }); diff --git a/packages/linter/src/executors/lint/lint.impl.ts b/packages/linter/src/executors/lint/lint.impl.ts index 7322118d87eed..047b49eeb6a24 100644 --- a/packages/linter/src/executors/lint/lint.impl.ts +++ b/packages/linter/src/executors/lint/lint.impl.ts @@ -60,7 +60,6 @@ export default async function run( createProgram(path.resolve(systemRoot, tsConfig)) ); - let i = 0; for (const program of allPrograms) { lintReports = [ ...lintReports, diff --git a/packages/workspace/src/tasks-runner/task-orchestrator.ts b/packages/workspace/src/tasks-runner/task-orchestrator.ts index 4e27df833d8f7..bcad766328fff 100644 --- a/packages/workspace/src/tasks-runner/task-orchestrator.ts +++ b/packages/workspace/src/tasks-runner/task-orchestrator.ts @@ -131,10 +131,8 @@ export class TaskOrchestrator { private async applyCachedResult(t: TaskWithCachedResult) { const outputs = getOutputs(this.projectGraph.nodes, t.task); - const shouldCopyOutputsFromCache = this.cache.shouldCopyOutputsFromCache( - t, - outputs - ); + const shouldCopyOutputsFromCache = + !!outputs.length && this.cache.shouldCopyOutputsFromCache(t, outputs); if (shouldCopyOutputsFromCache) { this.cache.copyFilesFromCache(t.task.hash, t.cachedResult, outputs); } diff --git a/packages/workspace/src/tasks-runner/utils.ts b/packages/workspace/src/tasks-runner/utils.ts index 1926bb5e048c6..79ddea4067c6b 100644 --- a/packages/workspace/src/tasks-runner/utils.ts +++ b/packages/workspace/src/tasks-runner/utils.ts @@ -100,9 +100,9 @@ export function getOutputsForTargetAndConfiguration( }; if (targets?.outputs) { - return targets.outputs.map((output: string) => - interpolateOutputs(output, options) - ); + return targets.outputs + .map((output: string) => interpolateOutputs(output, options)) + .filter((output) => !!output); } // Keep backwards compatibility in case `outputs` doesn't exist @@ -169,7 +169,7 @@ function interpolateOutputs(template: string, data: any): string { let path = match.slice(1, -1).trim().split('.').slice(1); for (let idx = 0; idx < path.length; idx++) { if (!value[path[idx]]) { - throw new Error(`Could not interpolate output {${match}}!`); + return; } value = value[path[idx]]; }