Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/core/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,20 @@ const onCompileDone = ({
}
});

compiler.hooks.invalid.tap('rsbuild:done', () => {
const resetDoneState = () => {
if (compilerDone) {
compilerDone = false;
doneCompilers--;
}
});
};

compiler.hooks.invalid.tap('rsbuild:done', resetDoneState);
// A watch compilation can restart without another invalid hook.
// Reset before asynchronous watchRun hooks can delay the new compilation.
compiler.hooks.watchRun.tap(
{ name: 'rsbuild:done', stage: -Infinity },
resetDoneState,
);
Comment on lines +351 to +354

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Cancel pending all-done decisions on restart

When one child's done handler has already captured lastCompilerDone = true and is awaiting an asynchronous onAfterEnvironmentCompile, another child can enter this watchRun and decrement doneCompilers, but the pending handler still invokes onDone using its captured value. Consequently, in a multi-environment dev/watch build with an async after-environment hook, onAfterDevCompile or onAfterBuild can still fire after a compiler has restarted but before its replacement stats are ready. Track and invalidate the pending completion generation, and cover this interleaving through the real watch lifecycle rather than only manually invoking hooks.

AGENTS.md reference: AGENTS.md:L6-L6

Useful? React with 👍 / 👎.

@chenjiahan chenjiahan Sep 24, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It also occurs before this PR. I'll consider addressing it in a separate follow-up, keeping this PR focused on resetting completion state when a compiler restarts without another invalid hook.

}
} else {
compiler.hooks.done.tapPromise('rsbuild:done', async (stats) => {
Expand Down
43 changes: 42 additions & 1 deletion packages/core/tests/hooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { rspack } from '@rspack/core';
import { createRsbuild } from '../src';
import { createEnvironmentAsyncHook, initHooks } from '../src/hooks';
import {
createEnvironmentAsyncHook,
initHooks,
registerDevHook,
} from '../src/hooks';
import type { InternalContext, Rspack } from '../src/types';

describe('initHooks', () => {
test('should initialize hooks correctly', async () => {
Expand Down Expand Up @@ -72,3 +78,38 @@ describe('onExit hook', () => {
expect(onExit).toHaveBeenCalledTimes(1);
});
});

test('should wait for a restarted compiler before onAfterDevCompile', async () => {
const compiler = rspack([{ name: 'a' }, { name: 'b' }]);
const [a, b] = compiler.compilers;
const hooks = initHooks();
const onAfterDevCompile = rstest.fn();

hooks.onAfterDevCompile.tap(onAfterDevCompile);

registerDevHook({
compiler,
bundlerConfigs: [{}, {}],
MultiStatsCtor: rspack.MultiStats,
context: {
hooks,
environmentList: [{ name: 'a' }, { name: 'b' }],
environments: {},
buildState: { time: {} },
} as unknown as InternalContext,
});

const stats = {} as Rspack.Stats;
await a.hooks.done.promise(stats);

// A restarts without invalid; B finishes while A's watchRun is pending.
a.hooks.watchRun.tapPromise({ name: 'test', stage: -1 }, async () => {
await b.hooks.done.promise(stats);
expect(onAfterDevCompile).not.toHaveBeenCalled();
});

await a.hooks.watchRun.promise(a);
await a.hooks.done.promise(stats);

expect(onAfterDevCompile).toHaveBeenCalledTimes(1);
});
Loading
⌂ 换网址