Skip to content

Rspack persistent cache bug repro - v2 - multiple CLI calls - #2

Open
slorber wants to merge 2 commits into
mainfrom
slorber/repro-code-single-cli-calls
Open

slorber wants to merge 2 commits into
mainfrom
slorber/repro-code-single-cli-calls

Conversation

@slorber

@slorber slorber commented Mar 6, 2025

Copy link
Copy Markdown
Owner

This is a follow-up of v1 repro here: #1

This time, we are executing one compiler.run() call per CLI invocation
(instead of calling compiler.run() multiple times in a single Node CLI script execution)

So technically, the Node script now just does a bundler compilation, and nothing else.

Let's consider this scenario:

# Cleanup caches
rm -rf node_modules/.cache

# 1st run - Codegen + Bundle
echo "export const dynamic = 42" > ./src/dynamicFile.js
node test

# 2nd run - Codegen (with === content) + Bundle => loader should not re-run
# Unfortunately, it re-runs for Rspack
echo "export const dynamic = 42" > ./src/dynamicFile.js
node test

# 3rd run - Codegen (with !== content) + Bundle => loader should re-run !
echo "export const dynamic = 82" > ./src/dynamicFile.js
node test

Webpack behavior

As expected the first compilation is slow:

Bundle App start - WEBPACK
- Loading src/entry.js
- Loading src/dynamicFile.js
- Loading src/file.js
Bundle App: 2.143s

However the 2nd compilation is fast, because dynamicFile.js content didn't change:

Bundle App start - WEBPACK
Bundle App: 35.386ms

On 3rd compilation, the content of the file changed, so only that file recompiles, as expected:

Bundle App start - WEBPACK
- Loading src/dynamicFile.js
Bundle App: 1.068s

Rspack behavior

As expected the first compilation is slow:

Bundle App start - RSPACK
- Loading src/entry.js
- Loading src/file.js
- Loading src/dynamicFile.js
Bundle App: 2.030s

The 2nd compilation is faster, but ⚠️ not as fast as webpack.

⚠️ The dynamicFile.js recompiles because the file what "rewritten", even if it's the exact same content.

Bundle App start - RSPACK
- Loading src/dynamicFile.js
Bundle App: 1.026s

On 3rd compilation, the content of the file changed, so only that file recompiles, as expected:

Bundle App start - RSPACK
- Loading src/dynamicFile.js
Bundle App: 1.024s

How to fix that?

On the 2nd CLI run, I'd like the Rspack compilation to be as fast as the Webpack one.

The constraints I have:

  • I cannot modify the shell script
  • I can only modify the node script that runs the bundler

Considering this is possible with Webpack, I assume it should also be possible to solve this for Rspack.

I can eventually pass extra compiler.run() methods like modifiedFiles, but to be honest I have no idea what values to provide in these options, and a concrete example would be useful.

Quoting your comment: web-infra-dev/rspack#9413 (comment)

For performance reasons, rspack uses modifiedFiles & removeFiles as the condition for cache invalidation, which saves additional file reading, hash calculation and other overhead.

I understand that Rspack might prefer to not hash all file content by default for performance reasons. At the same time, if I have to do it myself in userland, this is really unergonomic because the dynamic files of the previous compilation are not persisted in my scenario: each CLI call is a different CI run.

Providing myself an exhaustive modifiedFiles attribute to Rspack means that I would need to build my own persistent cache system on top of the existing Rspack persistent cache. This would be much simpler if it was built-in directly into Rspack, exactly like Webpack does.

I don't mind if the calculation of hashes is an opt-in boolean option, and accept the perf tradeoff.

Of course, we can also provide a configuration option to automatically calculate them when ModifiedFiles and removedFiles are not provided, but I guess users will be more inclined to pass them when they find the performance degradation.

If it's possible to provide that configuration option, I think that would be super useful to us.

This way, we can probably use

  • modifiedFiles in dev (when using watchers) to improve perf by not computing file hashes
  • using the new option in prod builds to compute based on file hash

Does it make sense?


If you can edit my Node.js script in a way that makes the 2nd Rspack compilation fast like Webpack (not recompiling dynamicFile.js), then the problem would be solved. Do you see any solution I don't see?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant