Skip to content

fix: sanitize chunk_name when preserveModulesRoot prefix is stripped in preserveModules mode - #8762

Closed
sapphi-red with Copilot wants to merge 12 commits into
mainfrom
copilot/fix-sanitize-filename-issue
Closed

sapphi-red with Copilot wants to merge 12 commits into
mainfrom
copilot/fix-sanitize-filename-issue

Conversation

Copilot AI commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

When preserveModules: true and preserveModulesRoot is set, sanitizeFileName was not applied to chunk names for modules whose absolute path starts with the root. The unsanitized path was used when computing the relative chunk_name, causing special characters (e.g. +, ?, &) to leak into PreRenderedChunk.name and any entryFileNames function 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 meant preserve_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 custom sanitizeFileName that 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.rs

    • Compute the relative chunk name entirely from non-sanitized values (absolute_chunk_file_name and preserve_modules_root), so byte lengths always match for the slice.
    • Call sanitize_filename once on the complete relative name (including extension) at the end.
    • Pass the non-sanitized absolute_chunk_file_name as chunk_filename to generate_preliminary_filename, so that get_preserve_modules_chunk_name receives the non-sanitized absolute path and can correctly match it against the non-sanitized preserve_modules_root via starts_with.
    • Keep chunk.name assignment deferred (after generate_preliminary_filename) to preserve the invariant that only user-provided entry chunks have a name at that point.
  • crates/rolldown_common/src/chunk/mod.rs

    • In generate_preliminary_filename, after get_preserve_modules_chunk_name computes the relative name from the non-sanitized absolute path, call options.sanitize_filename on the result when preserve_modules is 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.js imported by a named entry with preserveModulesRoot: '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 custom sanitizeFileName that replaces + with __ (increasing length); asserts output chunks are index.js and helper.js with 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

This section details on the original issue you should resolve

<issue_title>[Bug]: sanitizeFileName is ignored for JS chunks when preserveModules: true with object-form entry</issue_title>
<issue_description>### Reproduction link or steps

https://github.com/ef81sp/rolldown-sanitize-filename-repro

pnpm install
pnpm build

What is expected?

JS chunk filenames should be sanitized (? and & replaced with _):

dist/components/TestComp.vue_vue_type_script_setup_true_lang.js

What is actually happening?

dist/components/TestComp.vue?vue&type=script&setup=true&lang.js

Custom sanitizeFileName in output options is also ignored for these chunks. CSS filenames are correctly sanitized in the same build.

This occurs when entry is object-form ({ index: './src/index.ts' }). With string-form entry ('./src/index.ts'), preserveModules does not preserve the directory structure at all, so the issue does not surface.

System Info

System:
    OS: macOS 26.3.1
    CPU: (8) arm64 Apple M1
    Memory: 102.55 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 25.5.0 - /opt/homebrew/bin/node
    pnpm: 10.28.0
  npmPackages:
    vite: 8.0.0 (rolldown 1.0.0-rc.9)

Any additional comments?

#7554 fixed sanitizeFileName for preserveModules, but only when entry is string-form. With object-form entry, sanitizeFileName is still not applied to JS chunk filenames.

Workaround: using entryFileNames as a function to manually sanitize works.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 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.

@netlify

netlify Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 0427cee
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/69baddc610a82d00080eadb8

Copilot AI and others added 2 commits March 18, 2026 04:33
… 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]>
Copilot AI changed the title [WIP] Fix sanitizeFileName for JS chunks with preserveModules fix: sanitize chunk_name when preserveModulesRoot prefix is stripped in preserveModules mode Mar 18, 2026
Copilot AI requested a review from sapphi-red March 18, 2026 04:35
…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]>
Copilot AI requested a review from sapphi-red March 18, 2026 05:22
…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]>
Copilot AI requested a review from sapphi-red March 18, 2026 06:48
Signed-off-by: 翠 <[email protected]>
@sapphi-red

Copy link
Copy Markdown
Member

@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]>

Copilot AI commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

@copilot ls-lint and the tests are failing

Fixed in daca5da:

  1. ls-lint: Added the two new test fixture paths (which intentionally contain + in filenames/dirs) to the .ls-lint.json ignore list, following the same pattern as path-has-hashtag.

  2. Test failure (../__ libs/helper invalid substitution): The chunk.name = Some(...) assignment was placed after generate_preliminary_filename, so get_preserve_modules_chunk_name couldn't short-circuit via self.name. It then fell back to comparing the sanitized absolute path against the unsanitized preserve_modules_root — which fails when a custom sanitizer changes character count (e.g. +libs → __libs). Restored the assignment to before generate_preliminary_filename.

Copilot AI requested a review from sapphi-red March 18, 2026 15:26
@sapphi-red

Copy link
Copy Markdown
Member

@copilot Instead of moving the assignment of chunk.name, you should pass the non-sanitized chunk name to generate_preliminary_filename.

…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]>

Copilot AI commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Instead of moving the assignment of chunk.name, you should pass the non-sanitized chunk name to generate_preliminary_filename.

Done in 7d97c9e. Now storing the non-sanitized absolute_chunk_file_name as chunk_filename and keeping chunk.name deferred. In generate_preliminary_filename, after get_preserve_modules_chunk_name computes the relative name from the non-sanitized absolute path (so starts_with(preserve_modules_root) works correctly for any sanitizer), options.sanitize_filename is called on the result when preserve_modules is true.

@sapphi-red

Copy link
Copy Markdown
Member

@copilot we should only call options.sanitize_filename.call(chunk_name.as_ref()) once

…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]>

Copilot AI commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

@copilot we should only call options.sanitize_filename.call(chunk_name.as_ref()) once

Fixed in 0427cee. generate_preliminary_filename now uses rollup_pre_rendered_chunk.name directly when options.preserve_modules is true, bypassing the get_preserve_modules_chunk_name + sanitize_filename path. Since rollup_pre_rendered_chunk.name is already the sanitized relative name (computed by the single sanitize_filename call in generate_chunk_name_and_preliminary_filenames), there's now exactly one call to sanitize_filename for preserve_modules chunks.

@sapphi-red sapphi-red closed this Mar 19, 2026
@sapphi-red
sapphi-red deleted the copilot/fix-sanitize-filename-issue branch March 19, 2026 03:55
IWANABETHATGUY added a commit that referenced this pull request Oct 6, 2026
…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]>
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.

[Bug]: sanitizeFileName is ignored for JS chunks when preserveModules: true with object-form entry

2 participants