Skip to content

fix(turbopack): avoid duplicated .js ext of entry asset#119

Merged
xusd320 merged 1 commit intoutoofrom
fix/duplacated-entry-ext
Mar 13, 2026
Merged

fix(turbopack): avoid duplicated .js ext of entry asset#119
xusd320 merged 1 commit intoutoofrom
fix/duplacated-entry-ext

Conversation

@xusd320
Copy link
Copy Markdown

@xusd320 xusd320 commented Mar 13, 2026

This pull request updates the logic in the replace_name_placeholder function to improve how file extensions are handled when replacing the [name] placeholder. The new implementation prevents duplicated file extensions in output filenames, addressing cases where the name already contains an extension and the template also appends the same extension.

Filename placeholder handling improvements:

  • Updated replace_name_placeholder in chunking_context.rs to strip duplicate extensions from the name when the template text after [name] starts with the same extension, preventing outputs like foo.js.js.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refines the logic for replacing the [name] placeholder in filenames within Turbopack. The primary goal is to ensure that output filenames do not end up with redundant file extensions, thereby improving the consistency and correctness of generated asset names.

Highlights

  • Filename Placeholder Handling: Updated the replace_name_placeholder function to prevent duplicated file extensions (e.g., foo.js.js) when the [name] placeholder is replaced and the template already appends the same extension.
Changelog
  • turbopack/crates/turbopack-browser/src/chunking_context.rs
    • Modified the replace_name_placeholder function to include logic that checks for and strips duplicate file extensions from the name argument if the template string immediately following the [name] placeholder already contains that extension.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses an issue with duplicated file extensions by introducing more sophisticated logic in replace_name_placeholder. The implementation is sound. I've added one suggestion to further optimize the new logic by using Cow<str> to avoid unnecessary string allocations, which is a common pattern for this kind of string manipulation in Rust.

Comment on lines +1128 to +1141
.replace_all(s, |caps: &regex::Captures| {
let m = caps.get(0).unwrap();
let after = &s[m.end()..];
// If the name already ends with an extension (e.g. "foo.js") and the template
// text right after [name] starts with that same extension (e.g. ".js"), strip
// the extension from the name to avoid duplication like "foo.js.js".
if let Some(dot_pos) = name.rfind('.') {
let ext = &name[dot_pos..]; // e.g. ".js"
if after.starts_with(ext) {
return name[..dot_pos].to_string();
}
}
name.to_string()
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For performance, you can avoid allocating a new String in the case where the name doesn't need to be modified. By using std::borrow::Cow<str> as the return type of the closure, you can return a borrowed slice (Cow::Borrowed) when no changes are made, and an owned String (Cow::Owned) only when the extension is stripped. This avoids unnecessary allocations.

Suggested change
.replace_all(s, |caps: &regex::Captures| {
let m = caps.get(0).unwrap();
let after = &s[m.end()..];
// If the name already ends with an extension (e.g. "foo.js") and the template
// text right after [name] starts with that same extension (e.g. ".js"), strip
// the extension from the name to avoid duplication like "foo.js.js".
if let Some(dot_pos) = name.rfind('.') {
let ext = &name[dot_pos..]; // e.g. ".js"
if after.starts_with(ext) {
return name[..dot_pos].to_string();
}
}
name.to_string()
})
.replace_all(s, |caps: &regex::Captures| -> std::borrow::Cow<str> {
let m = caps.get(0).unwrap();
let after = &s[m.end()..];
// If the name already ends with an extension (e.g. "foo.js") and the template
// text right after [name] starts with that same extension (e.g. ".js"), strip
// the extension from the name to avoid duplication like "foo.js.js".
if let Some(dot_pos) = name.rfind('.') {
let ext = &name[dot_pos..]; // e.g. ".js"
if after.starts_with(ext) {
return std::borrow::Cow::Owned(name[..dot_pos].to_string());
}
}
std::borrow::Cow::Borrowed(name)
})

@xusd320 xusd320 merged commit abb9695 into utoo Mar 13, 2026
10 of 25 checks passed
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