fix(js): post-process skipped tasks by the ts compiler in tsc batch implementation (#17938)

This commit is contained in:
Leosvel Pérez Espinosa 2023-07-10 14:30:25 +01:00 committed by GitHub
parent d905e36fb7
commit d10aeb49ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 26 deletions

View File

@ -187,6 +187,15 @@ describe('js e2e', () => {
`dist/libs/${lib}/src/lib/${lib}.js`,
`dist/libs/${lib}/src/lib/${lib}.d.ts`
);
// run a second time skipping the nx cache and with the outputs present
const secondBatchBuildOutput = runCLI(
`build ${parentLib} --skip-nx-cache`,
{ env: { NX_BATCH_MODE: 'true' } }
);
expect(secondBatchBuildOutput).toContain(
`Successfully ran target build for project ${parentLib} and 1 task it depends on`
);
}, 240_000);
it('should not create a `.babelrc` file when creating libs with js executors (--compiler=tsc)', () => {

View File

@ -76,17 +76,7 @@ export async function* tscBatchExecutor(
},
};
const typescriptCompilation = compileTypescriptSolution(
tsCompilationContext,
shouldWatch,
logger,
{
beforeProjectCompilationCallback: (tsConfig) => {
if (tsConfigTaskInfoMap[tsConfig]) {
tsConfigTaskInfoMap[tsConfig].startTime = Date.now();
}
},
afterProjectCompilationCallback: (tsConfig) => {
const processTaskPostCompilation = (tsConfig: string) => {
if (tsConfigTaskInfoMap[tsConfig]) {
const taskInfo = tsConfigTaskInfoMap[tsConfig];
taskInfo.assetsHandler.processAllAssetsOnceSync();
@ -98,7 +88,19 @@ export async function* tscBatchExecutor(
);
taskInfo.endTime = Date.now();
}
};
const typescriptCompilation = compileTypescriptSolution(
tsCompilationContext,
shouldWatch,
logger,
{
beforeProjectCompilationCallback: (tsConfig) => {
if (tsConfigTaskInfoMap[tsConfig]) {
tsConfigTaskInfoMap[tsConfig].startTime = Date.now();
}
},
afterProjectCompilationCallback: processTaskPostCompilation,
}
);
@ -136,23 +138,68 @@ export async function* tscBatchExecutor(
});
}
return yield* mapAsyncIterable(typescriptCompilation, async (iterator) => {
const { value, done } = await iterator.next();
const toBatchExecutorTaskResult = (
tsConfig: string,
success: boolean
): BatchExecutorTaskResult => ({
task: tsConfigTaskInfoMap[tsConfig].task,
result: {
success: success,
terminalOutput: tsConfigTaskInfoMap[tsConfig].terminalOutput,
startTime: tsConfigTaskInfoMap[tsConfig].startTime,
endTime: tsConfigTaskInfoMap[tsConfig].endTime,
},
});
let isCompilationDone = false;
const taskTsConfigsToReport = new Set(
Object.keys(taskGraph.tasks).map((t) => taskInMemoryTsConfigMap[t].path)
);
let tasksToReportIterator: IterableIterator<string>;
const processSkippedTasks = () => {
const { value: tsConfig, done } = tasksToReportIterator.next();
if (done) {
return { value, done: true };
return { value: undefined, done: true };
}
const taskResult: BatchExecutorTaskResult = {
task: tsConfigTaskInfoMap[value.tsConfig].task,
result: {
success: value.success,
terminalOutput: tsConfigTaskInfoMap[value.tsConfig].terminalOutput,
startTime: tsConfigTaskInfoMap[value.tsConfig].startTime,
endTime: tsConfigTaskInfoMap[value.tsConfig].endTime,
},
tsConfigTaskInfoMap[tsConfig].startTime = Date.now();
processTaskPostCompilation(tsConfig);
return { value: toBatchExecutorTaskResult(tsConfig, true), done: false };
};
return { value: taskResult, done: false };
return yield* mapAsyncIterable(typescriptCompilation, async (iterator) => {
if (isCompilationDone) {
return processSkippedTasks();
}
const { value, done } = await iterator.next();
if (done) {
if (taskTsConfigsToReport.size > 0) {
/**
* TS compilation is done but we still have tasks to report. This can
* happen if, for example, a project is identified as affected, but
* no file in the TS project is actually changed or if running a
* task with `--skip-nx-cache` and the outputs are already there. There
* can still be changes to assets or other files we need to process.
*
* Switch to handle the iterator for the tasks we still need to report.
*/
isCompilationDone = true;
tasksToReportIterator = taskTsConfigsToReport.values();
return processSkippedTasks();
}
return { value: undefined, done: true };
}
taskTsConfigsToReport.delete(value.tsConfig);
return {
value: toBatchExecutorTaskResult(value.tsConfig, value.success),
done: false,
};
});
}