Skip to content

Commit 417a6bf

Browse files
authored
fix(rspack): gate virtual folder behind process pid (#538)
ensures that the node_modules/.virtual folder deletion supports parallel runs
1 parent ca4047e commit 417a6bf

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/rspack/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function getRspackPlugin<UserOptions = Record<string, never>>(
3232
apply(compiler) {
3333
// We need the prefix of virtual modules to be an absolute path so rspack lets us load them (even if it's made up)
3434
// In the loader we strip the made up prefix path again
35-
const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), 'node_modules/.virtual')
35+
const VIRTUAL_MODULE_PREFIX = resolve(compiler.options.context ?? process.cwd(), 'node_modules/.virtual', process.pid.toString())
3636

3737
const meta: UnpluginContextMeta = {
3838
framework: 'rspack',

src/rspack/utils.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,37 @@ export function isVirtualModuleId(encoded: string, plugin: ResolvedUnpluginOptio
1717

1818
export class FakeVirtualModulesPlugin {
1919
name = 'FakeVirtualModulesPlugin'
20-
static counter = 0
20+
static counters: Map<string, number> = new Map<string, number>()
21+
22+
static {
23+
['SIGINT', 'SIGTERM', 'SIGQUIT', 'exit'].forEach((event) => {
24+
process.once(event, () => {
25+
this.counters.forEach((_, dir) => {
26+
fs.rmSync(dir, { recursive: true, force: true })
27+
})
28+
})
29+
})
30+
}
31+
2132
constructor(private plugin: ResolvedUnpluginOptions) {}
2233

2334
apply(compiler: Compiler): void {
24-
FakeVirtualModulesPlugin.counter++
2535
const dir = this.plugin.__virtualModulePrefix
2636
if (!fs.existsSync(dir)) {
2737
fs.mkdirSync(dir, { recursive: true })
2838
}
39+
const counter = FakeVirtualModulesPlugin.counters.get(dir) ?? 0
40+
FakeVirtualModulesPlugin.counters.set(dir, counter + 1)
41+
2942
compiler.hooks.shutdown.tap(this.name, () => {
30-
if (--FakeVirtualModulesPlugin.counter === 0) {
43+
const counter = (FakeVirtualModulesPlugin.counters.get(dir) ?? 1) - 1
44+
if (counter === 0) {
45+
FakeVirtualModulesPlugin.counters.delete(dir)
3146
fs.rmSync(dir, { recursive: true, force: true })
3247
}
48+
else {
49+
FakeVirtualModulesPlugin.counters.set(dir, counter)
50+
}
3351
})
3452
}
3553

0 commit comments

Comments
 (0)