Repository navigation
fix: clean the files writeToDisk writes when output.clean is set - #2497
Conversation
With writeToDisk as a function, webpack writes to memory and cleans only that, while the middleware copies assets to disk, so the disk was never cleaned. webpack's CleanPlugin is now applied a second time to a view of the compiler whose outputFileSystem is the real one, in standalone and plugin mode. writeToDisk: true already worked: the real file system is the output file system then. Also notes, as TODOs, that the standalone (non-plugin) mode goes in the next major release. Closes #861 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
🦋 Changeset detectedLatest commit: 8709d95 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughWhen disk files are written, the middleware now applies webpack’s Severity of issue fixed: Medium Merge Risk:
|
There was a problem hiding this comment.
Actionable comments posted: 1

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Organization UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
756a672b-3d70-4afb-bf0a-3ed4556d6014
Files selected for processing: 4-
.changeset/write-to-disk-output-clean.mdREADME.mdsrc/index.jstest/write-to-disk-clean.test.js
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
writeToDisk: true made the real file system webpack's output only when no outputFileSystem was given; with one, it stayed webpack's and nothing was written to disk at all. Copy assets to disk then too, as a function does, and clean them with output.clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
Probing the rest of
Checked and already correct:
No webpack change is needed:
Generated by Claude Code |
Dirent#parentPath arrived in Node.js 20.12; this package supports 20.9. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

Major · Preserve Rspack compatibility when enabling disk cleaning. · index.js:612
src/index.js:612
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy liftPreserve Rspack compatibility when enabling disk cleaning.
If a Rspack compiler uses
output.cleanwith function-valuedwriteToDisk, this call attempts to constructcompiler.webpack.CleanPlugin. Rspack does not expose that plugin, so middleware setup throws before watching starts. Use a supported cleaning path for Rspack, or handle that compiler explicitly. (rspack.dev)

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Organization UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
1561cb90-81f9-4f94-93d4-1970636cca32
Files selected for processing: 4-
.changeset/write-to-disk-output-clean.mdREADME.mdsrc/index.jstest/write-to-disk-clean.test.js
Files skipped from review as they are similar to previous changes: 1-
- .changeset/write-to-disk-output-clean.md
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
-
Copied files reach the disk beside the bundle, survive rebuilds, are copied again when changed and cleaned once no longer copied, with writeToDisk true and a function, in standalone and plugin mode; a filter leaving them off disk still serves them from memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
Added copy-webpack-plugin coverage (c0fe374,
On Generated by Claude Code |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Rspack cleans natively and exposes no CleanPlugin to apply again, so the middleware failed to start there with output.clean and a writeToDisk function; it now writes to disk without cleaning, as before. Also fix the writeToDisk anchor in the README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
Re the Rspack finding outside the diff ( Fixed in 904f558. Generated by Claude Code |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

Major · Clean disk copies against the assets selected by writeToDisk. · index.js:622-624
src/index.js:622-624
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy liftClean disk copies against the assets selected by
writeToDisk.If
dist/robots.txtexists from an earlier run, and the current build emitsrobots.txtwith a.js-onlywriteToDiskpredicate, the predicate skips the copy. The diskCleanPluginstill seesrobots.txtincompilation.assetsand keeps the old disk file. This leaves an excluded asset on disk despiteoutput.clean. Give the disk cleaner the filtered asset set without changing the compilation seen by webpack’s normal cleaner. (raw.githubusercontent.com)

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Organization UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
f94f076b-01e3-4d77-9904-82c33ec8d38a
Files selected for processing: 3-
README.mdsrc/index.jstest/write-to-disk-clean.test.js
Files skipped from review as they are similar to previous changes: 1-
- README.md
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
-
The disk cleaner saw every asset of the build, so a file the function skips was kept on disk from an earlier session despite output.clean. It now sees only the assets the function writes, while webpack's own cleaner still sees them all, and other plugins' keep hook still applies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
Re the finding outside the diff ( The disk The view's The copy-webpack-plugin Generated by Claude Code |
There was a problem hiding this comment.
Actionable comments posted: 1

Review info
-
Run configuration-
- Source mode: Auto
- Repository: Unavailable
- Branch: Unavailable
- File: No configuration file from this repository applied
- Commit: Unavailable
- Layers: Organization UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
eed3cbd6-c8ed-4d5d-a5a4-12dbd0465060
Files selected for processing: 2-
src/index.jstest/write-to-disk-clean.test.js
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.
-
webpack writes `lazy.js?[contenthash]` as `lazy.js`, but the disk cleaner filtered and tracked the name with the query, so the filter saw the wrong path and the file stayed on disk once the asset was gone. Strip the query or hash as webpack does before filtering and cleaning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
On macOS the watcher may run one more build for the files the test wrote before it started; the rebuild helper took that build's done for the one after the change. Wait for a build started after the change instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
The cause is in the test, not the fix. On macOS the watcher can start one more build for the files the test wrote just before watching began, and the Fixed in 8ad3c99: Generated by Claude Code |
Replace the disk cleaner, which applied CleanPlugin again to views of the compiler and compilation, with webpack's own support for more outputs: where compiler.hooks.additionalOutputs exists, writeToDisk adds the disk as one, so webpack writes links, permissions, times and query-named assets there as to its own output, and output.clean cleans it too. With an older webpack or Rspack the middleware copies to disk as before. Cover output.copy beside copy-webpack-plugin, and a copied link and its permissions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
Reworked in 5ff497e. The disk cleaner no longer re-applies
The clean tests run where the installed webpack has the hook. Against webpack/webpack#22950 linked locally, 28/28 pass in both modes and the full suite passes, apart from 2 tests that fail only when run as root. With webpack 5.112.0 (the current devDependency) they skip, and a fallback test checks today's behavior instead. Once a webpack release carries the hook, the devDependency should be bumped so CI runs the clean tests. Generated by Claude Code |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2497 +/- ##
==========================================
+ Coverage 97.07% 97.14% +0.06%
==========================================
Files 25 25
Lines 2704 2767 +63
==========================================
+ Hits 2625 2688 +63
Misses 79 79 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Give the compiler that hook, so what writeToDisk adds there is checked where the installed webpack has none yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
|
Codecov reports 5 lines that
So I'm not pushing a speculative change for it. If someone can open the Codecov "Indirect changes" view for this PR, the 5 lines it lists would show whether the drop is real (for example an OS-only path) or variance between CI runs. Generated by Claude Code |
Replace the webpack hook with a file system given to the compiler that writes to memory and, for the paths `writeToDisk` takes, to disk, removes files from both and lists both, so `output.clean` cleans the disk on every webpack 5 version and on Rspack. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
A build restarted for a change keeps the startTime it began with, so it could be taken for the build before the change and never awaited. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
Write to disk with `writeToDisk: true` when the compiler has a file system of its own, not only one given in `outputFileSystem`. Give a file system that is the disk itself, Node.js `fs` or webpack's `graceful-fs`, as it is, so nothing is written or removed twice, and keep the methods webpack does without, such as `symlink`, only where the file system has them. Test both modes with a file system of your own on the compiler and in `outputFileSystem`: what is written, served and cleaned on each side, `clean.keep` and `clean.dry`, one lacking optional methods, and a multi-compiler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
memfs lists recursively with the full path on Windows, so the files were looked up under the output directory twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
Cover keeping a filtered-out file's earlier copy on disk with each form of `clean.keep` and with `clean.dry`, a multi-compiler where a path is outside one compiler's output, copied file times, and the build failing when the served file system or the disk cannot be written. Say why a glob `clean.keep` keeps such a copy rather than leave that branch unexplained. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
Repeat every write on disk through one `mirror` instead of a wrapper per method, and remove from both and list both in a few lines. Ask the `keep` hook of `CleanPlugin` too before removing a disk copy of a file `writeToDisk` leaves out, as `output.clean` does, with `clean.keep` where the compiler's `CleanPlugin` has no hooks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UjuMAuk9o6UazjHzcAQCTA
Closes #861.
What I checked on
mainfirst. One project per run:output.clean: true,filename: "main.[contenthash].js", and a stale file left indist/from a "previous session". Each run did a first build, then a source change and a rebuild:writeToDisk: truewriteToDisk: (path) => …wdm(compiler, options)main.*.jswdm(compiler, options, true), host ownscompiler.watch()(as with webpack-dev-server)With a function, webpack writes to memory and cleans only memory, while
assetEmittedcopies each asset to disk, so nothing on disk is ever removed. No hook fixes that:CleanPluginremoves throughcompiler.outputFileSystemand nothing else. WithwriteToDisk: trueand a file system of your own, set on the compiler or given inoutputFileSystem, nothing was written to disk at all.Fix. No webpack change is needed. Unless the served file system is the disk itself, the compiler is given a two-way file system on top of it:
writeFile,symlink,chmod,utimes) go to the served file system, then to disk for the pathswriteToDisktakes, through onemirror;unlinkandrmdirremove from both;readdirlists both, andlstatfalls back to the disk;stat,readFileandreadlinkstay on the served side, so webpack's emit still decides what to write from it alone.So webpack's own
output.cleancleans the disk too, withclean.keep, thekeephook,clean.dryand the hot update grace period. TheassetEmittedhook is gone. This works in plugin and standalone mode, for multi-compilers, on every webpack 5 version and on Rspack (checked with 5.112 and @rspack/core 2.3.2).compiler.outputFileSystemis set once, and onlyNodeEnvironmentPluginassigns it in webpack, before any plugin, so nothing replaces it later.The disk itself is Node.js
fs(Rspack's default) orgraceful-fs(webpack's default). It is given as it is, so nothing is written or removed twice. A file system of your own may lack the methods webpack can do without (lstat,readlink,symlink,chmod,utimes,unlink,rmdir,readdir): those are added only where it has them, and the disk is still written and cleaned.One case
output.cleancannot see: a copy on disk, left by an earlier session, of a file the function now leaves out. webpack counts it as current, so it is removed when written, unlessoutput.cleanwould keep it: thekeephook first, thenclean.keep(a string, RegExp or function; a glob keeps it, as webpack does not expose its matcher), orclean.dry.One gap is webpack's, not this PR's: on webpack 5.112
CleanPlugindoes not strip?query/#hashfrom asset names, solazy.js?[contenthash]is not cleaned once gone, with or without the middleware. That is fixed on webpackmainby webpack/webpack#23219.Next major. As
//TODO comments insrc/index.js: remove the standalone mode in favor of the plugin mode.Tests, in
test/write-to-disk-clean.test.js, against real directories, in both standalone and plugin mode (84 tests):writeToDisk: trueand a function: stale files removed on the first build, the previous bundle on a rebuild;clean.keep,clean.dry, nooutput.clean;output.copy: copied files written, changed, and cleaned once removed; filtered-out ones kept off disk; symlinks, file modes and times kept;outputFileSystem:clean.keep(each form), thekeephook andclean.dryon both sides;fsand webpack's own file system given as they are;Against
main'ssrc/index.js, the new behavior fails and the rest guard what already worked; every line the PR adds is covered. The full suite has 7556 passed. The 2 failures are thewriteToDiskEACCES logging cases, which fail onmaintoo when run as root.npm run lintpasses. webpack-dev-server'swrite-to-disk-cleane2e test passes against this branch as a server of its own and as a plugin. README and patch changeset are updated.Generated by Claude Code