fix: use sanitized filename for chunk name with preserveModulesRoot#8817
fix: use sanitized filename for chunk name with preserveModulesRoot#8817younggglcy wants to merge 10 commits into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
There was a problem hiding this comment.
Pull request overview
Fixes preserve-modules chunk naming so that when preserveModulesRoot is set, the computed chunk.name (and therefore fileName when entryFileNames is a function) is derived from a sanitized filename, preventing query-string characters from leaking into output filenames (e.g., Vue SFC virtual module ids).
Changes:
- Use
sanitized_absolute_filename(instead of the raw absolute filename) when stripping thepreserveModulesRootprefix during chunk name generation. - Add a new preserve-modules fixture to cover query-string virtual module ids with function-form
entryFileNames. - Add a minimal entry module for the new fixture.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
crates/rolldown/src/stages/generate_stage/mod.rs |
Ensures chunk name prefix-stripping uses the sanitized path so chunk.name/fileName can’t include ?/&. |
packages/rolldown/tests/fixtures/topics/preserve-modules/sanitize-filename-query-string/_config.ts |
Adds regression test that asserts chunk.name and chunk.fileName don’t contain query-string characters under preserveModulesRoot + function entryFileNames. |
packages/rolldown/tests/fixtures/topics/preserve-modules/sanitize-filename-query-string/entry.js |
New fixture entry that imports the virtual module used by the regression test. |
hyfdev
left a comment
There was a problem hiding this comment.
LGTM. Looks like related to preserveModules, so @IWANABETHATGUY cc
5516a49 to
6bfa611
Compare
| if let Some(ref preserve_modules_root) = preserve_modules_root { | ||
| if absolute_chunk_file_name.starts_with(preserve_modules_root.as_str()) { | ||
| absolute_chunk_file_name[preserve_modules_root.len()..] | ||
| if sanitized_absolute_filename.starts_with(preserve_modules_root.as_str()) { |
There was a problem hiding this comment.
I think this will incorrectly match when two directories are sanitized to the same value:
preserve_modules_root: './somewhere/foo+bar'-> (sanitized)./somewhere/foo__bar- chunk 1 filename:
./somewhere/foo+bar/baz.js-> (sanitized)./somewhere/foo__bar/baz.js - chunk 2 filename:
./somewhere/foo#bar/baz.js-> (sanitized)./somewhere/foo__bar/baz.js
…eserveModulesRoot When preserveModulesRoot is set, the chunk name was derived from the unsanitized absolute filename, causing query string characters (? &) to leak into chunk.name and subsequently into fileName when entryFileNames is a function. This affected Vue SFC virtual modules in Vite lib mode. Closes rolldown#8761
…ized filenames When a custom sanitizeFileName transforms characters in the root path (e.g. '+' → '__'), the starts_with check against the sanitized absolute filename would fail, producing invalid relative paths like '../__libs/helper'.
Derive preserve-modules names from the original path and reuse the computed pre-rendered name when rendering filenames. This prevents sanitized path prefixes from flattening sibling directories onto the same preserveModulesRoot output. Made-with: Cursor
d4be8b2 to
d6431c0
Compare
Align the issue 7146 Rust snapshot with the corrected preserveModules output name so JSON inputs keep their .json segment before the generated .js extension. Made-with: Cursor
| if let Some(ref preserve_modules_root) = options.preserve_modules_root { | ||
| if chunk_name.starts_with(preserve_modules_root) { |
There was a problem hiding this comment.
I think this has the same problem with #8817 (comment)
|
Thanks a lot for the patient reviews here. This needs a broader |
Summary
preserveModulesRootis set, the chunk name was derived from the unsanitized absolute filename, causing query string characters (?,&) to leak intochunk.nameand subsequently intofileNamewhenentryFileNamesis a functionComp.vue?vue&type=script&setup=true&lang.ts) in Vite lib mode, producing invalid filenames likeComp.vue?vue&type=script&setup=true&lang.jssanitized_absolute_filename) instead of the raw one (absolute_chunk_file_name) when stripping thepreserveModulesRootprefixCloses #8761
Test plan
sanitize-filename-query-stringthat reproduces the bug withpreserveModulesRoot+ function-formentryFileNames+ query string module IDssanitize-filenametest still passes