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

fix(webpack): WebpackNxBuildCoordinationPlugin cancels inflight builds on new changes detected #18883

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { output } from 'nx/src/utils/output';

export class WebpackNxBuildCoordinationPlugin {
private currentlyRunning: 'none' | 'nx-build' | 'webpack-build' = 'none';
private buildCmdProcess: ReturnType<typeof exec> | null = null;

constructor(private readonly buildCmd: string, skipInitialBuild?: boolean) {
if (!skipInitialBuild) {
Expand All @@ -30,19 +31,7 @@ export class WebpackNxBuildCoordinationPlugin {
}

async startWatchingBuildableLibs() {
const unregisterFileWatcher = await createFileWatcher(
() => this.buildChangedProjects(),
() => {
output.error({
title: 'Watch connection closed',
bodyLines: [
'The daemon has closed the connection to this watch process.',
'Please restart your watch command.',
],
});
process.exit(1);
}
);
const unregisterFileWatcher = await this.createFileWatcher();

process.on('exit', () => {
unregisterFileWatcher();
Expand All @@ -54,46 +43,54 @@ export class WebpackNxBuildCoordinationPlugin {
await sleep(50);
}
this.currentlyRunning = 'nx-build';
return new Promise<void>((res) => {
try {
const cp = exec(this.buildCmd);
try {
return await new Promise<void>((res) => {
this.buildCmdProcess = exec(this.buildCmd);

cp.stdout.pipe(process.stdout);
cp.stderr.pipe(process.stderr);
cp.on('exit', () => {
this.buildCmdProcess.stdout.pipe(process.stdout);
this.buildCmdProcess.stderr.pipe(process.stderr);
this.buildCmdProcess.on('exit', () => {
res();
});
cp.on('error', () => {
this.buildCmdProcess.on('error', () => {
res();
});
// eslint-disable-next-line no-empty
} catch (e) {
res();
} finally {
this.currentlyRunning = 'none';
});
} finally {
this.currentlyRunning = 'none';
this.buildCmdProcess = null;
}
}

private createFileWatcher() {
const runner = new BatchFunctionRunner(() => this.buildChangedProjects());
return daemonClient.registerFileWatcher(
{
watchProjects: 'all',
},
(err, { changedProjects, changedFiles }) => {
if (err === 'closed') {
output.error({
title: 'Watch connection closed',
bodyLines: [
'The daemon has closed the connection to this watch process.',
'Please restart your watch command.',
],
});
process.exit(1);
}

if (this.buildCmdProcess) {
this.buildCmdProcess.kill(2);
this.buildCmdProcess = null;
}
Comment on lines +83 to +86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If new changes are detected, kill the current process running the build allowing the Promise to resolve, allowing the next item in the queue to be processed.

Consideration: An API for the BatchFunctionRunner to clear the queue before queuing a new build. There could be a potential issue where we have 3 builds queued, we detect changes, and we only cancel the first build, but there are still builds queued.
However, I haven't been able to produce this scenario in testing.

// Queue a build
runner.enqueue(changedProjects, changedFiles);
}
});
);
}
}

function sleep(time: number) {
return new Promise((resolve) => setTimeout(resolve, time));
}
async function createFileWatcher(
changeHandler: () => Promise<void>,
onClose: () => void
) {
const runner = new BatchFunctionRunner(changeHandler);
return daemonClient.registerFileWatcher(
{
watchProjects: 'all',
},
(err, { changedProjects, changedFiles }) => {
if (err === 'closed') {
onClose();
}
// Queue a build
runner.enqueue(changedProjects, changedFiles);
}
);
}