Repository navigation
fix: sanitize chunk_name when preserveModulesRoot prefix is stripped in preserveModules mode - #8762
sapphi-red with Copilot wants to merge 12 commits into
Conversation
✅ Deploy Preview for rolldown-rs canceled.
|
… chunk_name When preserve_modules_root is set and a module's absolute path starts with it, use sanitized_absolute_filename instead of absolute_chunk_file_name when computing the relative chunk_name portion. This ensures sanitizeFileName is applied to the chunk names exposed through PreRenderedChunk.name and entryFileNames hooks. Add test: sanitize-filename-with-preserve-modules-root verifies that a module named '+module.js' inside the preserveModulesRoot directory is emitted as '_module.js' (with '+' sanitized to '_'). Co-authored-by: sapphi-red <[email protected]>
Co-authored-by: sapphi-red <[email protected]>
…e before filename generation Two related fixes for the preserve_modules + preserveModulesRoot + custom sanitizeFileName case: 1. In the chunk name computation (generate_stage/mod.rs), sanitize preserve_modules_root before using its length to slice sanitized_absolute_filename. A custom sanitizeFileName that maps '+' to '__' makes the sanitized root longer than the original, so using the original length slices at the wrong byte offset. 2. Move chunk.name assignment to before generate_preliminary_filename. This ensures get_preserve_modules_chunk_name (in chunk/mod.rs) uses self.name directly rather than trying to re-derive the relative path by comparing the sanitized absolute chunk_filename against the original (unsanitized) preserve_modules_root — a comparison that fails whenever the custom sanitizer changes character lengths. Add test fixture: sanitize-filename-preserve-modules-root-custom-sanitizer - preserveModulesRoot: 'src/+libs' (contains '+') - custom sanitizeFileName: replaces '+' with '__' (length increase) - asserts output chunks are 'index.js' and 'helper.js' (correct prefix stripped) Co-authored-by: sapphi-red <[email protected]>
…ne pass Refactor the chunk name computation for preserveModules mode to: 1. Derive the relative path from the non-sanitized absolute_chunk_file_name and preserve_modules_root (so byte lengths always match for the slice) 2. Append the extension using the same non-sanitized values 3. Call sanitize_filename once on the complete relative name at the end This avoids the previous complexity of sanitizing preserve_modules_root separately and using its sanitized length for slicing, which was fragile and hard to reason about. Co-authored-by: sapphi-red <[email protected]>
Co-authored-by: 翠 <[email protected]> Signed-off-by: 翠 <[email protected]>
Signed-off-by: 翠 <[email protected]>
|
@copilot ls-lint and the tests are failing |
…and add fixture paths to ls-lint ignore - Fix ls-lint failures by adding the two new test fixture paths (which intentionally have `+` in filenames) to .ls-lint.json ignore list - Fix test failure by restoring chunk.name = Some(...) assignment to BEFORE generate_preliminary_filename so that get_preserve_modules_chunk_name can short-circuit via self.name, avoiding a broken starts_with comparison between the sanitized absolute path and the unsanitized preserve_modules_root Co-authored-by: sapphi-red <[email protected]>
Fixed in daca5da:
|
|
@copilot Instead of moving the assignment of |
…e and sanitize there Instead of moving chunk.name assignment before generate_preliminary_filename (which was needed so get_preserve_modules_chunk_name could short-circuit), pass the non-sanitized absolute chunk file name to generate_preliminary_filename. Then get_preserve_modules_chunk_name receives a non-sanitized path and can correctly do starts_with(non-sanitized preserve_modules_root). Sanitize the result inside generate_preliminary_filename when preserve_modules is true. This keeps chunk.name assignment deferred (after generate_preliminary_filename) as intended. Co-authored-by: sapphi-red <[email protected]>
Done in 7d97c9e. Now storing the non-sanitized |
|
@copilot we should only call |
…ary_filename for preserve_modules to avoid double sanitize_filename call The previous implementation called sanitize_filename twice for preserve_modules chunks: 1. In mod.rs to produce chunk_name for PreRenderedChunk.name 2. In chunk/mod.rs after get_preserve_modules_chunk_name for template [name] substitution Both calls produced the same sanitized relative name for common-extension modules. Now generate_preliminary_filename uses rollup_pre_rendered_chunk.name directly (already sanitized from call 1) when options.preserve_modules is true, eliminating the duplicate call. Co-authored-by: sapphi-red <[email protected]>
Fixed in 0427cee. |
…e path (#11087) Fixes #8761 With `preserveModules`, the chunk name was built by mixing raw and sanitized paths: - The generate stage stripped the raw `preserveModulesRoot` from the raw id, appended the raw extension and never sanitized the result. So `chunk.name` (what an `entryFileNames` function gets, and `[name]` in `sourcemapFileNames`) kept `?`, `&` and `=` from ids like `TestComp.vue?vue&type=script&setup=true&lang.ts`. On Windows the write then fails with os error 123. - `get_preserve_modules_chunk_name` computed `[name]` again from the sanitized id but compared it with the raw root and the raw input base. A root such as `lib+x` was ignored, a project whose common folder contains a sanitized character (for example `src+x`) failed with `Invalid substitution "../src_x/dep"`, and a sanitizer that changes length failed the same way. Now the generate stage builds the relative path from the raw id, root and input base, then calls `sanitizeFileName` at the end. `get_preserve_modules_chunk_name` uses that value for module chunks. Common chunks keep the `_virtual/` path as before. This follows sapphi-red's review on #8762, #8817 and #9289. Behavior changes: - A custom `sanitizeFileName` now receives the relative path (including `_virtual/` for virtual modules) instead of the absolute id. For a non-JS extension such as `.vue` it is also called once with the extension. Rollup passes the relative name to it as well (`assignFacadeName`). - Vite lib mode builds file names from `chunk.name`, so names under `preserveModulesRoot` with sanitized characters (`+page`, `$lib`) now get `_` there too, as `[name]` patterns and Rollup already did. - With the default sanitizer, output otherwise only changes in the cases above, which were broken. Tests (each fails on main): - `sanitize-filename-query` (from #9289) and `sanitize-filename-query-string` (from #8817): query ids with and without `preserveModulesRoot`, including `sourcemapFileNames`. - `sanitize-filename-with-preserve-modules-root` and `sanitize-filename-preserve-modules-root-custom-sanitizer` (from #8762 and #8817, adapted, and no longer skipped on Windows). - `sanitize-filename-root-collision`: `foo+bar` and `foo#bar` sanitize to the same name, only the one under the root is stripped. - Rust fixture `sanitize_filename_input_base`: modules under `src+x` without a root. No existing snapshot changed. Tested on Windows with the Rust workspace tests and the node test suite. Sanitizing only `chunk_name` (the first version of #9289) fixes the report but not the root and input base cases from #8817 that the review on #9289 asked for. Thanks to @deltama37 (#9289) and @younggglcy (#8817), whose tests are included here. --------- Co-authored-by: waltu <[email protected]> Co-authored-by: IWANABETHATGUY <[email protected]>
When
preserveModules: trueandpreserveModulesRootis set,sanitizeFileNamewas not applied to chunk names for modules whose absolute path starts with the root. The unsanitized path was used when computing the relativechunk_name, causing special characters (e.g.+,?,&) to leak intoPreRenderedChunk.nameand anyentryFileNamesfunction output.Root cause
In
generate_chunk_name_and_preliminary_filenames, the relative chunk name was computed from the already-sanitized absolute filename rather than the original unsanitized one. This meantpreserve_modules_root.len()(from the original unsanitized root) was applied to the sanitized string, which is only safe when the sanitizer is length-preserving. A customsanitizeFileNamethat changes character counts (e.g. replacing+with__) would slice at the wrong position, producing a corrupted chunk name.Changes
crates/rolldown/src/stages/generate_stage/mod.rsabsolute_chunk_file_nameandpreserve_modules_root), so byte lengths always match for the slice.sanitize_filenameonce on the complete relative name (including extension) at the end.absolute_chunk_file_nameaschunk_filenametogenerate_preliminary_filename, so thatget_preserve_modules_chunk_namereceives the non-sanitized absolute path and can correctly match it against the non-sanitizedpreserve_modules_rootviastarts_with.chunk.nameassignment deferred (aftergenerate_preliminary_filename) to preserve the invariant that only user-provided entry chunks have a name at that point.crates/rolldown_common/src/chunk/mod.rsgenerate_preliminary_filename, afterget_preserve_modules_chunk_namecomputes the relative name from the non-sanitized absolute path, calloptions.sanitize_filenameon the result whenpreserve_modulesis enabled, so the final name substituted into the filename template is always sanitized.packages/rolldown/tests/fixtures/topics/preserve-modules/sanitize-filename-with-preserve-modules-root/— new test fixture:src/+module.jsimported by a named entry withpreserveModulesRoot: 'src'; asserts the output chunk is emitted as_module.js, not+module.js.packages/rolldown/tests/fixtures/topics/preserve-modules/sanitize-filename-preserve-modules-root-custom-sanitizer/— new test fixture:preserveModulesRoot: 'src/+libs'with a customsanitizeFileNamethat replaces+with__(increasing length); asserts output chunks areindex.jsandhelper.jswith no leftover root prefix, directly covering the length-mismatch case..ls-lint.json— added the two new fixture directories to the ignore list (they intentionally use+in file/directory names to exercise sanitization).Original prompt
💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.