Skip to content

fix: #29894 addTemplate should consider the entire path#29895

Merged
danielroe merged 1 commit intonuxt:mainfrom
guska8:guska8/issue29894
Nov 17, 2024
Merged

fix: #29894 addTemplate should consider the entire path#29895
danielroe merged 1 commit intonuxt:mainfrom
guska8:guska8/issue29894

Conversation

@guska8
Copy link
Contributor

@guska8 guska8 commented Nov 12, 2024

🔗 Linked issue

resolves #29894

📚 Description

When using addTemplate with the same filename but different subfolders only the last template is considered, the ones before it are silently ignored.

It is a breaking change in nuxt 3 from nuxt 2 because on the previous version all files are created as expected.

Feel free to edit or request changes if needed.


Review PR in StackBlitz Submitted with StackBlitz.

Summary by CodeRabbit

  • Bug Fixes
    • Improved template management by ensuring that duplicates are identified based on destination paths instead of filenames, enhancing the overall template handling process.

@guska8 guska8 requested a review from danielroe as a code owner November 12, 2024 17:55
@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 12, 2024

Walkthrough

The changes in this pull request modify the addTemplate function in packages/kit/src/template.ts. The filtering logic has been updated to remove templates based on their destination path (dst) instead of their filename. This adjustment aims to align the template management logic with expected behavior, particularly addressing an issue where templates with the same filename but different paths were incorrectly filtered out.

Changes

File Path Change Summary
packages/kit/src/template.ts Updated filtering logic in addTemplate to compare dst instead of filename.

Assessment against linked issues

Objective Addressed Explanation
Correct filtering of templates based on destination path instead of filename. (#29894)

🐰 In the garden where templates grow,
A change was made to help them flow.
No more lost paths, just clear sight,
Each template finds its home just right!
With dst in mind, we hop with glee,
A joyful fix for you and me! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
packages/kit/src/template.ts (1)

25-27: Consider adding documentation and warning for this behavior change.

While this change restores the expected Nuxt 2 behavior, it modifies how template deduplication works. Consider:

  1. Adding documentation about this behavior change
  2. Adding a warning when multiple templates with the same filename are detected, to help users understand why their templates are now being processed differently

Here's a suggested enhancement to make the change more transparent:

 // Remove any existing template with the same destination path
+const existingTemplate = nuxt.options.build.templates
+  .find(p => normalizeTemplate(p).filename === template.filename && normalizeTemplate(p).dst !== template.dst)
+if (existingTemplate) {
+  console.warn(`Multiple templates with filename "${template.filename}" detected. ` +
+    `Both templates will be processed as they have different destination paths. ` +
+    `This is a change from previous behavior where only the last template would be kept.`)
+}
 nuxt.options.build.templates = nuxt.options.build.templates
   .filter(p => normalizeTemplate(p).dst !== template.dst)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4ccd1ad and 6d1f319.

📒 Files selected for processing (1)
  • packages/kit/src/template.ts (1 hunks)
🔇 Additional comments (1)
packages/kit/src/template.ts (1)

25-27: LGTM! This change fixes the template filtering issue.

The modification to filter templates based on destination path (dst) instead of filename correctly addresses issue #29894, allowing templates with the same filename to coexist in different subfolders.

Let's verify the impact of this change:

✅ Verification successful

Verified: The change to filter templates by destination path is correct and safe.

The modification aligns with the template system's design where each template should be uniquely identified by its full destination path. Analysis of template usage across the codebase shows no patterns that would be negatively impacted by this change.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any existing templates that might be affected by this change

# Search for addTemplate usage to understand current patterns
rg "addTemplate\(" -A 5 -B 5

# Look for potential template definitions with same filenames
fd -e ts -e js -x sh -c '
  filename=$(basename {})
  count=$(find . -name "$filename" | wc -l)
  if [ "$count" -gt 1 ]; then
    echo "Found $count occurrences of $filename:"
    find . -name "$filename"
  fi
'

Length of output: 350486

@guska8
Copy link
Contributor Author

guska8 commented Nov 12, 2024

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)

packages/kit/src/template.ts (1)> 25-27: Consider adding documentation and warning for this behavior change.

While this change restores the expected Nuxt 2 behavior, it modifies how template deduplication works. Consider:

  1. Adding documentation about this behavior change
  2. Adding a warning when multiple templates with the same filename are detected, to help users understand why their templates are now being processed differently

Here's a suggested enhancement to make the change more transparent:

 // Remove any existing template with the same destination path
+const existingTemplate = nuxt.options.build.templates
+  .find(p => normalizeTemplate(p).filename === template.filename && normalizeTemplate(p).dst !== template.dst)
+if (existingTemplate) {
+  console.warn(`Multiple templates with filename "${template.filename}" detected. ` +
+    `Both templates will be processed as they have different destination paths. ` +
+    `This is a change from previous behavior where only the last template would be kept.`)
+}
 nuxt.options.build.templates = nuxt.options.build.templates
   .filter(p => normalizeTemplate(p).dst !== template.dst)

📜 Review details

I think the code suggestion from coderabbit is irrelevant because the dst variable already includes the filename.

The part about the warning i don't know how you usually do

Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

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

This seems good to me - thank you ❤️

I think there's some more optimisation we could do here for a future PR. (Do we need to normalise each template at every iteration? 🤔)

@danielroe danielroe merged commit d5494c9 into nuxt:main Nov 17, 2024
@github-actions github-actions bot mentioned this pull request Nov 17, 2024
@github-actions github-actions bot mentioned this pull request Nov 17, 2024
@guska8 guska8 deleted the guska8/issue29894 branch November 18, 2024 16:18
@github-actions github-actions bot mentioned this pull request Nov 19, 2024
Kamsou pushed a commit to Kamsou/nuxt that referenced this pull request Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

addTemplate filter ignoring wrong templates - breaking change from nuxt2

2 participants