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: should report errors if stats was being accessed after the compiler was closed #8561

Merged
merged 2 commits into from
Nov 28, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

let resolveCompilerStats;
let compilerStats = new Promise((resolve) => {
resolveCompilerStats = resolve
});

class MyPlugin {
apply(compiler) {
compiler.hooks.done.tap("Plugin", stats => {
resolveCompilerStats(stats)
});
}
}

/** @type {import('../../dist').TCompilerCaseConfig} */
module.exports = {
description: "should be called every compilation",
options(context) {
return {
context: context.getSource(),
entry: "./d",
plugins: [new MyPlugin()]
};
},
async build(_, compiler) {
await new Promise(resolve => {
compiler.run((err, stats) => {
compiler.close(() => {
// Should be able to access `Stats` within the same tick of closing.
expect(() => stats.compilation).not.toThrow();
resolve()
})
});
});
},
async check() {
let stats = await compilerStats;
// Should not be able to access `Stats` after the compiler was shutdown.
expect(() => stats.compilation).toThrow("Unable to access `Stats` after the compiler was shutdown")
}
};
5 changes: 4 additions & 1 deletion packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ export class Compilation {
__internal__pushRspackDiagnostic(diagnostic: binding.JsRspackDiagnostic): void;
// @internal
__internal__setAssetSource(filename: string, source: Source): void;
// (undocumented)
get __internal__shutdown(): boolean;
set __internal__shutdown(shutdown: boolean);
// @internal
__internal_getInner(): binding.JsCompilation;
// (undocumented)
Expand Down Expand Up @@ -10022,7 +10025,7 @@ type StatOptions = {
export class Stats {
constructor(compilation: Compilation);
// (undocumented)
compilation: Compilation;
get compilation(): Compilation;
// (undocumented)
get endTime(): number | undefined;
// (undocumented)
Expand Down
10 changes: 10 additions & 0 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export type NormalizedStatsOptions = KnownNormalizedStatsOptions &

export class Compilation {
#inner: JsCompilation;
#shutdown: boolean;

hooks: Readonly<{
processAssets: liteTapable.AsyncSeriesHook<Assets>;
Expand Down Expand Up @@ -272,6 +273,7 @@ export class Compilation {

constructor(compiler: Compiler, inner: JsCompilation) {
this.#inner = inner;
this.#shutdown = false;
this.#customModules = {};

const processAssetsHook = new liteTapable.AsyncSeriesHook<Assets>([
Expand Down Expand Up @@ -1228,6 +1230,14 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
return this.#inner;
}

get __internal__shutdown() {
return this.#shutdown;
}

set __internal__shutdown(shutdown) {
this.#shutdown = shutdown;
}

seal() {}
unseal() {}

Expand Down
7 changes: 6 additions & 1 deletion packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ class Compiler {

this.hooks.shutdown.tap("rspack:cleanup", () => {
if (!this.running) {
this.#instance = undefined;
// Delayed rspack cleanup to the next tick.
// This supports calls to `fn rspack` to do something with `Stats` within the same tick.
process.nextTick(() => {
this.#instance = undefined;
this.#compilation && (this.#compilation.__internal__shutdown = true);
});
}
});
}
Expand Down
13 changes: 11 additions & 2 deletions packages/rspack/src/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ export type {

export class Stats {
#inner: binding.JsStats;
compilation: Compilation;
#compilation: Compilation;
#innerMap: WeakMap<Compilation, binding.JsStats>;

constructor(compilation: Compilation) {
this.#inner = compilation.__internal_getInner().getStats();
this.compilation = compilation;
this.#compilation = compilation;
this.#innerMap = new WeakMap([[this.compilation, this.#inner]]);
}

Expand All @@ -42,6 +42,15 @@ export class Stats {
return inner;
}

get compilation() {
if (this.#compilation.__internal__shutdown) {
throw new Error(
"Unable to access `Stats` after the compiler was shutdown"
);
}
return this.#compilation;
}

get hash() {
return this.compilation.hash;
}
Expand Down
Loading