Skip to content

Commit

Permalink
fix(core): skip caching output properties missing in options and argu…
Browse files Browse the repository at this point in the history
…ments (#6743)

* fix(core): add outputFile to cached outputs

* fix(core): ignore output properties missing in options and console arguments
  • Loading branch information
meeroslav authored Aug 18, 2021
1 parent 295b8d7 commit b320624
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
25 changes: 25 additions & 0 deletions e2e/linter/src/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
newProject,
readFile,
readJson,
removeFile,
runCLI,
uniq,
updateFile,
Expand Down Expand Up @@ -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);
});
5 changes: 5 additions & 0 deletions e2e/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand Down
17 changes: 17 additions & 0 deletions e2e/workspace/src/run-commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
1 change: 0 additions & 1 deletion packages/linter/src/executors/lint/lint.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export default async function run(
createProgram(path.resolve(systemRoot, tsConfig))
);

let i = 0;
for (const program of allPrograms) {
lintReports = [
...lintReports,
Expand Down
6 changes: 2 additions & 4 deletions packages/workspace/src/tasks-runner/task-orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/workspace/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]];
}
Expand Down

0 comments on commit b320624

Please sign in to comment.